Class: ReputationRule

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/reputation_rule.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ReputationRule

Returns a new instance of ReputationRule.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/reputation_rule.rb', line 17

def initialize(*args)
  args[0] = {
    :weight => 1,
    :kind => 'singular',
    :function => 'linear',
    :constants => { :m => 1 },
    :aggregate_function => 'linear',
    :aggregate_constants => { :m => 1 }
  }.merge(args.first||{})

  super *args
end

Class Method Details

.value_for(user) ⇒ Object

Return the total score for a certain user

Parameters:

  • (User)


33
34
35
# File 'app/models/reputation_rule.rb', line 33

def self.value_for(user)
  all.inject(0){|total,r| total + r.value_for( user ) }
end

Instance Method Details

#aggregate_functionReputation::Functions::Linear, ...

Return the aggregate function object defined by :aggregate_function and :aggregate_constants



74
75
76
# File 'app/models/reputation_rule.rb', line 74

def aggregate_function
  build_function(super, aggregate_constants)
end

#functionReputation::Functions::Linear, ...

Return the function object defined by :function and :constants



67
68
69
# File 'app/models/reputation_rule.rb', line 67

def function
  build_function(super, constants)
end

#normalized_weightingObject

Lookup the weighting relative to all other rules



39
40
41
# File 'app/models/reputation_rule.rb', line 39

def normalized_weighting
  BigDecimal(weight.to_s) / ReputationRule.sum('weight')
end

#recalculate_intermediate_values_for(user) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/reputation_rule.rb', line 78

def recalculate_intermediate_values_for(user) # :nodoc:
  behaviour = user.behaviours.find_by_rule_id(self.id)
  if behaviour
    case kind
    when 'collection'
      iv = intermediate_values.find_by_user_id_and_name(user.id,kind) || intermediate_values.build(:user => user, :name => kind)
      iv.value += f(behaviour.metric)
      iv.save!
    end
  end
end

#value_for(user) ⇒ Object

Calculate the score for a certain user

Parameters:

  • (User)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/reputation_rule.rb', line 46

def value_for(user)
  behaviour = user.behaviours.find_by_rule_id id
  if behaviour
    case kind
    when 'singular'
      f(behaviour.metric) * normalized_weighting
    when 'collection'
      ivo = intermediate_values.find_by_user_id_and_name(user.id,kind)
      iv = ivo ? ivo.value : 0
      aggregate_f( 
        f(behaviour.metric) + iv
      ) * normalized_weighting
    end
  else
    0
  end
end