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



20
21
22
# File 'lib/rules/rule_set.rb', line 20

def self.attributes
  @@attributes
end

.attributize(attributes_hash) ⇒ Object



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

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



16
17
18
# File 'lib/rules/rule_set.rb', line 16

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

Instance Method Details

#attributesObject



32
33
34
35
36
# File 'lib/rules/rule_set.rb', line 32

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)



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

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