Class: PyrRules::RulesConfig

Inherits:
Object
  • Object
show all
Defined in:
app/rules/pyr_rules/rules_config.rb

Class Method Summary collapse

Class Method Details

.add_rule(rule_hash) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/rules/pyr_rules/rules_config.rb', line 39

def self.add_rule(rule_hash)
  puts "\n\nAdding rule #{rule_hash}"
  event_inclusion = rule_hash.delete(:event_inclusion) rescue nil
  event_exclusion = rule_hash.delete(:event_exclusion) rescue nil
  rule = PyrRules::Rule.where(:name => rule_hash[:name]).first
  puts "   Updating existing rule..." if rule
  action_configs = rule_hash.delete(:action_configs)
  rule ||= PyrRules::Rule.create rule_hash
  # Match regexp for inclusions
  events_to_add = EventConfigLoader.events_list.select{|x| x if x.match event_inclusion} if event_inclusion  
  # Match regexp for exclusions
  events_to_restrict = EventConfigLoader.events_list.select{|x| x if x.match event_exclusion} if event_exclusion
  events_to_add = (events_to_add - events_to_restrict) if events_to_restrict
  rule.events = events_to_add
  puts "   Mapped to events: #{rule.events}"
  action_configs.each do |action_config|
    existing_action = rule.actions.detect{|action| action.title == action_config[:title] rescue false}
    rule.actions.create!(action_config) unless existing_action
  end
  rule.set_default_mappings
  rule.save!
  puts "Rule added\n"
end

.delete_rulesObject



63
64
65
# File 'app/rules/pyr_rules/rules_config.rb', line 63

def self.delete_rules
  PyrRules::Rule.delete_all
end

.dump_events_configObject

User friendly dump of the event configuration



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/rules/pyr_rules/rules_config.rb', line 68

def self.dump_events_config
  events_config.each do |k,v| 
    puts "\n\n\n#{k.to_s.titleize}"
    v.keys.sort.each do |class_name| 
      puts "\n   #{class_name}"
      puts "       - actions"
      v[class_name][:actions].each do |action| 
        puts "           #{action}"
      end
      puts "       - context"
      v[class_name][:context].each do |col_name, col_config| 
        puts "           #{col_name}(#{col_config[:type]})"
      end
    end
  end
  nil
end

.event_config(event_name) ⇒ Object



10
11
12
13
14
15
16
17
# File 'app/rules/pyr_rules/rules_config.rb', line 10

def self.event_config(event_name)
  ec = (events_config[:model_events][event_name] || events_config[:controller_events][event_name])
  unless ec 
    event_name = event_name.match(/(.*)::.*/)[1]  # 
    ec = (events_config[:model_events][event_name] || events_config[:controller_events][event_name])
  end
  ec
end

.eventsObject



19
20
21
# File 'app/rules/pyr_rules/rules_config.rb', line 19

def self.events
  EventConfigLoader.events_list
end

.events_configObject



6
7
8
# File 'app/rules/pyr_rules/rules_config.rb', line 6

def self.events_config
  EventConfigLoader.events_dictionary
end

.events_treeObject

A very nice hierarchical Hash of all the events



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/rules/pyr_rules/rules_config.rb', line 24

def self.events_tree
  tree={}
  events_config.keys.sort.each do |event_type|
    events_config[event_type].keys.sort.each do |class_name|
      insert_here = (tree[event_type] ||= {})
      parts = class_name.split("::")
      parts[0...-1].each do |p|
        insert_here = (insert_here[p] ||= {})
      end
      insert_here[parts.last] = events_config[event_type][class_name]
    end
  end
  tree
end