Permalink
Browse files

Make code less complex (second attemp)

  • Loading branch information...
1 parent cfb987d commit 96abd19ee8b979dddb3f9f13e65e9d1d5f1523d0 Katsuya Noguchi committed Jul 16, 2012
Showing with 12 additions and 12 deletions.
  1. BIN lib/reputation_system/.network.rb.swp
  2. +12 −12 lib/reputation_system/network.rb
Binary file not shown.
@@ -93,10 +93,7 @@ def get_weight_of_source_from_reputation_name_of_target(target, source_name, rep
source = get_reputation_def(target.class.name, reputation_name)[:source]
if source.is_a?(Array)
source.each do |s|
- scope = target.evaluate_reputation_scope(s[:scope]) if s[:scope]
- of = target.get_attributes_of(s)
- class_name = (of.is_a?(Array) ? of[0] : of).class.name
- srn = get_scoped_reputation_name(class_name, s[:reputation], scope)
+ srn = get_scoped_reputation_name_from_source_def_and_target(s, target)
return s[:weight] if srn.to_sym == source_name.to_sym
end
else
@@ -116,22 +113,18 @@ def data_for_derive_later
def create_scoped_reputation_def(class_name, reputation_name, scope, options)
raise ArgumentError, "#{reputation_name} does not have scope." unless has_scopes?(class_name, reputation_name)
- scope_options = {}
+ scope_options = options.select { |k, v| [:source, :aggregated_by].include? k }
reputation_def = get_reputation_def(class_name, reputation_name)
- if is_primary_reputation?(class_name, reputation_name)
- scope_options[:source] = options[:source]
- else
+ unless is_primary_reputation?(class_name, reputation_name)
scope_options[:source] = []
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|
+ (reputation_def[:source_of] || []).each do |so|
if source_of_defined_for_scope?(so, scope)
scope_options[:source_of] ||= []
scope_options[:source_of].push so
end
- end if source_of
- scope_options[:aggregated_by] = options[:aggregated_by]
+ end
srn = get_scoped_reputation_name(class_name, reputation_name, scope)
network[class_name.to_sym][srn.to_sym] = scope_options
end
@@ -146,6 +139,13 @@ def create_source_reputation_def(source_def, scope)
rep
end
+ def get_scoped_reputation_name_from_source_def_and_target(source_def, target)
+ scope = target.evaluate_reputation_scope(source_def[:scope]) if source_def[:scope]
+ of = target.get_attributes_of(source_def)
+ class_name = (of.is_a?(Array) ? of[0] : of).class.name
+ get_scoped_reputation_name(class_name, source_def[:reputation], scope)
+ 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))

0 comments on commit 96abd19

Please sign in to comment.