Class: Rules::RuleSet

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rules/rule_set.rb

Constant Summary collapse

@@attributes =
Hash.new({})

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributesObject



22
23
24
# File 'lib/rules/rule_set.rb', line 22

def self.attributes
  @@attributes
end

.attributize(attributes_hash) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rules/rule_set.rb', line 26

def self.attributize(attributes_hash)
  mapped_hash = {}
  attributes_hash.each do |k, v|
    mapped_hash[k] = Rules::Parameters::Attribute.new(v.merge(key: k))
  end
  mapped_hash
end

.set_attributes_for(klass, klass_attributes) ⇒ Object



18
19
20
# File 'lib/rules/rule_set.rb', line 18

def self.set_attributes_for(klass, klass_attributes)
  @@attributes[klass] = @@attributes[klass].merge(attributize(klass_attributes))
end

Instance Method Details

#attributesObject



34
35
36
37
38
# File 'lib/rules/rule_set.rb', line 34

def attributes
  source_klass = source ? source.class : source_type.try(:constantize)
  return {} unless source_klass
  self.class.attributes[source_klass]
end

#evaluate(attributes = {}) ⇒ Object

TODO: Arbitrary rule set logic (Treetop)



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rules/rule_set.rb', line 41

def evaluate(attributes = {})
  return true unless rules.any?
  if evaluation_logic == 'any'
    !!rules.detect { |rule| rule.evaluate(attributes) }
  else
    rules.each do |rule|
      return false unless rule.evaluate(attributes)
    end
    true
  end
end