Permalink
Browse files

Make code less complex

  • Loading branch information...
1 parent 5c43450 commit cfb987d3e959ef1515ff7cf80c12446b64aaa3b7 Katsuya Noguchi committed Jul 15, 2012
Showing with 73 additions and 43 deletions.
  1. +27 −22 lib/models/rs_reputation.rb
  2. +46 −21 lib/reputation_system/network.rb
View
49 lib/models/rs_reputation.rb
@@ -125,29 +125,34 @@ def self.initialize_reputation_value(receiver, target, process)
# Propagates updated reputation value to the reputations whose source is the updated reputation.
def self.propagate_updated_reputation_value(sender, oldValue)
- sender_name = sender.reputation_name.to_sym
- receiver_defs = ReputationSystem::Network.get_reputation_def(sender.target.class.name, sender_name)[:source_of]
- if receiver_defs
- receiver_defs.each do |rd|
- receiver_targets = sender.target.get_attributes_of(rd)
- receiver_targets.each do |rt|
- scope = sender.target.evaluate_reputation_scope(rd[:scope])
- srn = ReputationSystem::Network.get_scoped_reputation_name(rt.class.name, rd[:reputation], scope)
- process = ReputationSystem::Network.get_reputation_def(rt.class.name, srn)[:aggregated_by]
- rep = find_by_reputation_name_and_target(srn, rt)
- if rep
- weight = ReputationSystem::Network.get_weight_of_source_from_reputation_name_of_target(rt, sender_name, srn)
- unless oldValue
- update_reputation_value_with_new_source(rep, sender, weight, process)
- else
- update_reputation_value_with_updated_source(rep, sender, oldValue, weight, process)
- end
- # If r is new then value update will be done when it is initialized.
- else
- create_reputation(srn, rt, process)
- end
- end
+ receiver_defs = ReputationSystem::Network.get_reputation_def(sender.target.class.name, sender.reputation_name)[:source_of]
+ receiver_defs.each do |rd|
+ targets = sender.target.get_attributes_of(rd)
+ targets.each do |target|
+ scope = sender.target.evaluate_reputation_scope(rd[:scope])
+ send_reputation_message_to_receiver(rd[:reputation], sender, target, scope, oldValue)
end
+ end if receiver_defs
+ end
+
+ def self.send_reputation_message_to_receiver(reputation_name, sender, target, scope, oldValue)
+ srn = ReputationSystem::Network.get_scoped_reputation_name(target.class.name, reputation_name, scope)
+ process = ReputationSystem::Network.get_reputation_def(target.class.name, srn)[:aggregated_by]
+ receiver = find_by_reputation_name_and_target(srn, target)
+ if receiver
+ weight = ReputationSystem::Network.get_weight_of_source_from_reputation_name_of_target(target, sender.reputation_name, srn)
+ update_reputation_value(receiver, sender, weight, process, oldValue)
+ # If r is new then value update will be done when it is initialized.
+ else
+ create_reputation(srn, target, process)
+ end
+ end
+
+ def self.update_reputation_value(receiver, sender, weight, process, oldValue)
+ unless oldValue
+ update_reputation_value_with_new_source(receiver, sender, weight, process)
+ else
+ update_reputation_value_with_updated_source(receiver, sender, oldValue, weight, process)
end
end
View
67 lib/reputation_system/network.rb
@@ -95,11 +95,13 @@ def get_weight_of_source_from_reputation_name_of_target(target, source_name, rep
source.each do |s|
scope = target.evaluate_reputation_scope(s[:scope]) if s[:scope]
of = target.get_attributes_of(s)
- srn = get_scoped_reputation_name((of.is_a?(Array) ? of[0] : of ).class.name, s[:reputation], scope)
- source = s if srn.to_sym == source_name.to_sym
+ class_name = (of.is_a?(Array) ? of[0] : of).class.name
+ srn = get_scoped_reputation_name(class_name, s[:reputation], scope)
+ return s[:weight] if srn.to_sym == source_name.to_sym
end
+ else
+ source[:weight]
end
- source[:weight]
end
protected
@@ -120,19 +122,11 @@ def create_scoped_reputation_def(class_name, reputation_name, scope, options)
scope_options[:source] = options[:source]
else
scope_options[:source] = []
- reputation_def[:source].each do |s|
- rep = {}
- rep[:reputation] = s[:reputation]
- # Passing "this" is not pretty but in some case "instance_exec" method
- # does not give right context for some reason.
- # This could be ruby bug. Needs further investigation.
- rep[:of] = lambda { |this| instance_exec(this, scope.to_s, &s[:of]) } if s[:of].is_a? Proc
- scope_options[:source].push rep
- end
+ reputation_def[:source].each { |sd| scope_options[:source].push create_source_reputation_def(sd, scope) }
end
source_of = reputation_def[:source_of]
source_of.each do |so|
- if so[:defined_for_scope].nil? || (so[:defined_for_scope] && so[:defined_for_scope].include?(scope.to_sym))
+ if source_of_defined_for_scope?(so, scope)
scope_options[:source_of] ||= []
scope_options[:source_of].push so
end
@@ -142,6 +136,21 @@ def create_scoped_reputation_def(class_name, reputation_name, scope, options)
network[class_name.to_sym][srn.to_sym] = scope_options
end
+ def create_source_reputation_def(source_def, scope)
+ rep = {}
+ rep[:reputation] = source_def[:reputation]
+ # Passing "this" is not pretty but in some case "instance_exec" method
+ # does not give right context for some reason.
+ # This could be ruby bug. Needs further investigation.
+ rep[:of] = lambda { |this| instance_exec(this, scope.to_s, &source_def[:of]) } if source_def[:of].is_a? Proc
+ rep
+ end
+
+ def source_of_defined_for_scope?(source_of_def, scope)
+ defined_for_scope = source_of_def[:defined_for_scope]
+ defined_for_scope.nil? || (defined_for_scope && defined_for_scope.include?(scope.to_sym))
+ end
+
def construct_scoped_reputation_options(class_name, reputation_name, options)
scopes = get_reputation_def(class_name, reputation_name)[:scopes]
scopes.each do |scope|
@@ -150,20 +159,36 @@ def construct_scoped_reputation_options(class_name, reputation_name, options)
end
def derive_source_of_from_source(class_name, reputation_name, source, src_class_name)
- if source[:of] && source[:of].is_a?(Symbol) && source[:of] != :self
- klass = src_class_name.to_s.constantize
- of_value = class_name.tableize
- of_value = of_value.chomp('s') unless klass.instance_methods.include?(of_value.to_s) || klass.instance_methods.include?(of_value.to_sym)
- else
- of_value = "self"
- end
+ of_value = derive_of_value(class_name, source[:of], src_class_name)
reputation_def = get_reputation_def(src_class_name, source[:reputation])
reputation_def[:source_of] ||= []
- unless reputation_def[:source_of].any? {|elem| elem[:reputation] == reputation_name.to_sym}
+ unless source_of_include_reputation?(reputation_def[:source_of], reputation_name)
reputation_def[:source_of] << {:reputation => reputation_name.to_sym, :of => of_value.to_sym}
end
end
+ def derive_of_value(class_name, source_of, src_class_name)
+ if not_source_of_self?(source_of)
+ attr = class_name.tableize
+ class_has_attribute?(src_class_name, attr) ? attr : attr.chomp('s')
+ else
+ "self"
+ end
+ end
+
+ def not_source_of_self?(source_of)
+ source_of && source_of.is_a?(Symbol) && source_of != :self
+ end
+
+ def class_has_attribute?(class_name, attribute)
+ klass = class_name.to_s.constantize
+ klass.instance_methods.include?(attribute.to_s) || klass.instance_methods.include?(attribute.to_sym)
+ end
+
+ def source_of_include_reputation?(source_of, reputation_name)
+ source_of.map { |rep| rep[:reputation] }.include?(reputation_name.to_sym)
+ end
+
def derive_source_of_from_source_later(class_name, reputation_name, source, src_class_name)
reputation = source[:reputation].to_sym
src_class_name = src_class_name.to_sym

0 comments on commit cfb987d

Please sign in to comment.