Class: RulesEngine::Discovery

Inherits:
Object
  • Object
show all
Defined in:
lib/rules_engine/discovery.rb

Class Method Summary collapse

Class Method Details

.discover!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rules_engine/discovery.rb', line 19

def discover!
  @rule_classes = []
  @rule_groups = {}
  
  Dir.glob("#{rules_path}/**/*.rb").each do |rule| 
    Kernel.load rule

    rule_class = RulesEngine::Rule.const_get(File.basename(rule, '.rb').classify)
    @rule_classes << rule_class
    
    group = rule_class.options[:group]
    @rule_groups[group] = [] unless @rule_groups.include?(group)
    @rule_groups[group] << rule_class
  end  
  
  @rule_classes.sort! { |left, right| return left.to_s < right.to_s}
  @rule_groups.each { |group, rule_classes| rule_classes.sort!  { |left, right| return left.to_s < right.to_s} }
  @rule_groups.each { |key, value| value.sort }
end

.rule_class(rule_class_name) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/rules_engine/discovery.rb', line 49

def rule_class(rule_class_name)
  throw Exception.new("RulesEngine::Discovery.discover! required in environment.rb") if @rule_classes.nil?
  @rule_classes.each do |rule|
    return rule if rule.rule_class_name == rule_class_name
  end
  nil
end

.rule_classesObject



39
40
41
42
# File 'lib/rules_engine/discovery.rb', line 39

def rule_classes
  throw Exception.new("RulesEngine::Discovery.discover! required in environment.rb") if @rule_classes.nil?
  @rule_classes
end

.rule_groupsObject



44
45
46
47
# File 'lib/rules_engine/discovery.rb', line 44

def rule_groups
  throw Exception.new("RulesEngine::Discovery.discover! required in environment.rb") if @rule_classes.nil?
  @rule_groups
end

.rules_pathObject



13
14
15
16
17
# File 'lib/rules_engine/discovery.rb', line 13

def rules_path
  return @rules_path if @rules_path
  return File.expand_path(File.join(Rails.root, '/app/rules')) if defined?(Rails) && Rails.root
  throw Exception.new("Rails.root or rules_path not defined") 
end

.rules_path=(path) ⇒ Object



9
10
11
# File 'lib/rules_engine/discovery.rb', line 9

def rules_path= path
  @rules_path = path
end