Class: ReputationSystem::Reputation

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_reputation(reputation_name, target, process) ⇒ Object



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

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



36
37
38
39
# File 'lib/reputation_system/models/reputation.rb', line 36

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.



42
43
44
45
# File 'lib/reputation_system/models/reputation.rb', line 42

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



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

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
    if source.target.respond_to?(process)
      rep.value = source.target.send(process, rep, source, weight)
    else
      raise ArgumentError, "#{process} process is not supported yet"
    end
  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



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

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
      if source.target.respond_to?(process)
        rep.value = source.target.send(process, rep, source, weight, oldValue, newSize)
      else
        raise ArgumentError, "#{process} process is not supported yet"
      end
    end
  end
  save_succeeded = rep.save
  propagate_updated_reputation_value(rep, valueBeforeUpdate) if rep.target
  save_succeeded
end

Instance Method Details

#normalized_valueObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/reputation_system/models/reputation.rb', line 107

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