-
Notifications
You must be signed in to change notification settings - Fork 4
/
pt_rules.rb
66 lines (57 loc) · 1.72 KB
/
pt_rules.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#=======================================================================================================================
class PtRules
attr_accessor :rules
def initialize
@rules = Array.new
end
#Methods for maintaining the rules array
def addRule(value, tag=nil)
#Gotta have a rule value, but tag is optional.
rule = Hash.new
rule[:value] = value
if not tag.nil? then
rule[:tag] = tag
end
#Add rule to rules array.
@rules << rule
end
def deleteRule(value) #No tag passed in, we remove with 'value' match.
#Regardless of tag, tour rules Array and remove.
@rules.each do |r|
if r[:value] == value then
@rules.delete(r)
end
end
end
#Methods for getting the rules in the structure you want ==================================================
def getJSON
rulesPayload = Hash.new
rulesPayload[:rules] = @rules
rulesPayload.to_json
end
def getArray
@rules
end
def getHash
@rules
end
#Methods for loading rules from files ==============================================================================
def loadRulesYAML(file)
#Open file and parse, looking for rule/tag pairs
ruleset = YAML.load_file(file)
rules = ruleset["rules"]
rules.each do |rule|
#p rule
@rules << rule
end
end
def loadRulesJSON(file)
#Open file and parse
contents = File.read(file)
ruleset = JSON.parse(contents)
rules = ruleset["rules"]
rules.each do |rule|
@rules << rule
end
end
end