ruleby 0.8 → 0.9.b1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/dsl/ferrari.rb +57 -81
  2. metadata +5 -5
@@ -52,82 +52,26 @@ module Ruleby
52
52
  return container
53
53
  end
54
54
 
55
- class RulesContainer < Array
56
- def transform_or(parent)
57
- ors = []
58
- others = []
59
- permutations = 1
60
- index = 0
61
- parent.each do |child|
62
- if(child.or?)
63
- permutations *= child.size
64
- ors << child
65
- else
66
- others[index] = child
67
- end
68
- index = index + 1
69
- end
70
- # set parent type to or and clear
71
- parent.kind = :or
72
- parent.clear
73
- indexes = []
74
- # initialize indexes
75
- ors.each do |o|
76
- indexes << 0
77
- end
78
- # create children
79
- (1.upto(permutations)).each do |i|
80
- and_container = Container.new(:and)
81
-
82
- mod = 1
83
- (ors.size - 1).downto(0) do |j|
84
- and_container.insert(0,ors[j][indexes[j]])
85
- if((i % mod) == 0)
86
- indexes[j] = (indexes[j] + 1) % ors[j].size
87
- end
88
- mod *= ors[j].size
89
- end
90
-
91
- others.each_with_index do |other, k|
92
- if others[k] != nil
93
- and_container.insert(k, others[k])
94
- end
95
- end
96
- # add child to parent
97
- parent.push(and_container)
98
- end
99
- parent.uniq!
100
- end
101
-
102
- def handle_branching(container)
55
+ class RulesContainer < Array
56
+ def handle_branching
103
57
  ands = []
104
- container.each do |x|
105
- if x.or?
106
- x.each do |branch|
107
- ands << branch
58
+ each do |x|
59
+ f = x.flatten_patterns
60
+ if f.or?
61
+ f.each do |o|
62
+ ands << o
108
63
  end
109
- elsif x.and?
110
- ands << x
111
64
  else
112
- new_and = Container.new(:and)
113
- new_and << x
114
- ands << new_and
115
- end
65
+ ands << f
66
+ end
116
67
  end
117
- return ands
68
+ ands
118
69
  end
119
70
 
120
- def build(name,options,engine,&block)
121
- rules = []
122
- self.each do |x|
123
- x.process_tree do |c|
124
- transform_or(c)
125
- end
71
+ def build(name, options, engine, &block)
72
+ handle_branching.map do |container|
73
+ build_rule(name, container, options, &block)
126
74
  end
127
- handle_branching(self).each do |a|
128
- rules << build_rule(name, a, options, &block)
129
- end
130
- return rules
131
75
  end
132
76
 
133
77
  def build_rule(name, container, options, &block)
@@ -143,8 +87,46 @@ module Ruleby
143
87
  class Container < Array
144
88
  attr_accessor :kind
145
89
 
146
- def initialize(kind)
90
+ def initialize(kind, *vals)
147
91
  @kind = kind
92
+ self.push(*vals)
93
+ end
94
+
95
+ def flatten_patterns
96
+ if or?
97
+ patterns = []
98
+ each do |c|
99
+ c.flatten_patterns.each do |o|
100
+ patterns << o
101
+ end
102
+ end
103
+ Container.new(:or, *patterns)
104
+ elsif and?
105
+ patterns = []
106
+ or_patterns = []
107
+ each do |c|
108
+ child_patterns = c.flatten_patterns
109
+ if child_patterns.or? and child_patterns.size > 1
110
+ child_patterns.each do |o|
111
+ or_patterns << o
112
+ end
113
+ else
114
+ patterns.push(*child_patterns)
115
+ end
116
+ end
117
+ if or_patterns.empty?
118
+ flat = Container.new(:and)
119
+ flat.push(*patterns)
120
+ else
121
+ flat = Container.new(:or)
122
+ or_patterns.each do |op|
123
+ c = Container.new(:and)
124
+ c.push(op, *patterns)
125
+ flat << c
126
+ end
127
+ end
128
+ return flat
129
+ end
148
130
  end
149
131
 
150
132
  def build(builder)
@@ -165,23 +147,17 @@ module Ruleby
165
147
  def and?
166
148
  return kind == :and
167
149
  end
168
-
169
- def process_tree(&block)
170
- has_or_child = false
171
- uniq!
172
- each do |c|
173
- has_or_child = true if (c.process_tree(&block) or c.or?)
174
- end
175
- yield(self) if (has_or_child)
176
- return has_or_child
177
- end
178
150
  end
179
151
 
180
152
  class PatternContainer
181
153
  def initialize(condition)
182
154
  @condition = condition
183
155
  end
184
-
156
+
157
+ def flatten_patterns
158
+ Container.new(:and, self)
159
+ end
160
+
185
161
  def build(builder)
186
162
  builder.when(*@condition)
187
163
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruleby
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: "0.8"
4
+ prerelease: 4
5
+ version: 0.9.b1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joe Kutner
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-06-16 00:00:00 -05:00
14
+ date: 2011-06-24 00:00:00 -05:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
@@ -57,9 +57,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  none: false
59
59
  requirements:
60
- - - ">="
60
+ - - ">"
61
61
  - !ruby/object:Gem::Version
62
- version: "0"
62
+ version: 1.3.1
63
63
  requirements: []
64
64
 
65
65
  rubyforge_project: ruleby