Class: ReputationSystem::Reputation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/reputation_system/models/reputation.rb

Constant Summary collapse

VALID_PROCESSES =
['sum', 'average', 'product']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_reputation(reputation_name, target, process) ⇒ Object



49
50
51
52
53
54
# File 'lib/reputation_system/models/reputation.rb', line 49

def self.create_reputation(reputation_name, target, process)
  create_options = {:reputation_name => reputation_name.to_s, :target_id => target.id,
                    :target_type => target.class.name, :aggregated_by => process.to_s}
  rep = create(create_options)
  initialize_reputation_value(rep, target, process)
end

.find_by_reputation_name_and_target(reputation_name, target) ⇒ Object



38
39
40
41
# File 'lib/reputation_system/models/reputation.rb', line 38

def self.find_by_reputation_name_and_target(reputation_name, target)
  target_type = get_target_type_for_sti(target, reputation_name)
  ReputationSystem::Reputation.find_by_reputation_name_and_target_id_and_target_type(reputation_name.to_s, target.id, target_type)
end

.find_or_create_reputation(reputation_name, target, process) ⇒ Object

All external access to reputation should use this since they are created lazily.



44
45
46
47
# File 'lib/reputation_system/models/reputation.rb', line 44

def self.find_or_create_reputation(reputation_name, target, process)
  rep = find_by_reputation_name_and_target(reputation_name, target)
  rep ? rep : create_reputation(reputation_name, target, process)
end

.update_reputation_value_with_new_source(rep, source, weight, process) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/reputation_system/models/reputation.rb', line 56

def self.update_reputation_value_with_new_source(rep, source, weight, process)
  weight ||= 1 # weight is 1 by default.
  size = rep.received_messages.size
  valueBeforeUpdate = size > 0 ? rep.value : nil
  newValue = source.value
  case process.to_sym
  when :sum
    rep.value += (newValue * weight)
  when :average
    rep.value = (rep.value * size + newValue * weight) / (size + 1)
  when :product
    rep.value *= (newValue * weight)
  else
    raise ArgumentError, "#{process} process is not supported yet"
  end
  save_succeeded = rep.save
  ReputationSystem::ReputationMessage.add_reputation_message_if_not_exist(source, rep)
  propagate_updated_reputation_value(rep, valueBeforeUpdate) if rep.target
  save_succeeded
end

.update_reputation_value_with_updated_source(rep, source, oldValue, newSize, weight, process) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/reputation_system/models/reputation.rb', line 77

def self.update_reputation_value_with_updated_source(rep, source, oldValue, newSize, weight, process)
  weight ||= 1 # weight is 1 by default.
  oldSize = rep.received_messages.size
  valueBeforeUpdate = oldSize > 0 ? rep.value : nil
  newValue = source.value
  if newSize == 0
    rep.value = process.to_sym == :product ? 1 : 0
  else
    case process.to_sym
    when :sum
      rep.value += (newValue - oldValue) * weight
    when :average
      rep.value = (rep.value * oldSize + (newValue - oldValue) * weight) / newSize
    when :product
      rep.value = (rep.value * newValue) / oldValue
    else
      raise ArgumentError, "#{process} process is not supported yet"
    end
  end
  save_succeeded = rep.save
  propagate_updated_reputation_value(rep, valueBeforeUpdate) if rep.target
  save_succeeded
end

Instance Method Details

#normalized_valueObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/reputation_system/models/reputation.rb', line 101

def normalized_value
  if self.active == 1 || self.active == true
    max = ReputationSystem::Reputation.max(self.reputation_name, self.target_type)
    min = ReputationSystem::Reputation.min(self.reputation_name, self.target_type)
    if max && min
      range = max - min
      range == 0 ? 0 : (self.value - min) / range
    else
      0
    end
  else
    0
  end
end