piece 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6a7526736094521b146d63d9483aa5aa2b01a2f
4
- data.tar.gz: 2a9102a633868f9f05258390129cbada6226e82d
3
+ metadata.gz: 4ab088dfe0e66ffc1450fc9eab486b2c10ca6d19
4
+ data.tar.gz: a0ec38dcea8530a04e2088a8364e7a80e2552a06
5
5
  SHA512:
6
- metadata.gz: bdde9cf422140e520832f9ce56e151977d69f570413cd6ca640fcf2888c6cf7780d1527e1be4be4c12e39a5f5441a60284199373e589d7972dc72e57cef1a5c7
7
- data.tar.gz: ae8d17cc64bcfb582a253b7845d1614dc4d97c5194b0626fc5c05a0dd014612863b4370c2be1e761b78319dc4e4e04201b36065612711d75727949315af1c01b
6
+ metadata.gz: d54d5a7e9bcdb09df0264b9bb50bd52cdfc5b0ad5fd8cc7f4096a3b188595671e1bd85d06b39257f8e206a1b3f9181dca9c1ce66b1afaa14b1576b8769513e74
7
+ data.tar.gz: e9ac20ef0b1a3643ddcf861e9a448ebc968d56a78f04b213de6f5eb8d264d3d8bcbb2dc9eb11a08a6964d710cb38c3946ca2c90d6aea33f31db46d416ea0160a
data/.travis.yml CHANGED
@@ -5,12 +5,10 @@ gemfile:
5
5
  - Gemfile
6
6
  language: ruby
7
7
  rvm:
8
- - 1.8.7
9
8
  - 1.9.3
10
9
  - 2.0.0
11
10
  - 2.1
12
11
  - 2.2.1
13
- - jruby-18mode
14
12
  - jruby-19mode
15
13
  - jruby-head
16
14
  - rbx-2
data/TODO CHANGED
@@ -1,4 +1,3 @@
1
1
  * serialize to yaml
2
2
  * save to DB
3
- * print reason: why match? respond true / false
4
3
  * print all matching rules given an action
data/lib/piece/rules.rb CHANGED
@@ -1,7 +1,13 @@
1
1
  require 'piece/expression.tab'
2
2
 
3
3
  module Piece
4
- class InvalidAction < StandardError
4
+ class RulesError < StandardError
5
+ end
6
+
7
+ class InvalidAction < RulesError
8
+ end
9
+
10
+ class UnknownRuleGroupName < RulesError
5
11
  end
6
12
 
7
13
  class Rules
@@ -38,11 +44,13 @@ module Piece
38
44
  end
39
45
 
40
46
  def match?(*action)
41
- !!self[*action]
47
+ self[*action][:match]
42
48
  end
43
49
 
44
50
  def [](*action)
45
- get(@data, action_parts(action))
51
+ ret = []
52
+ m = eval(@data, action_parts(action), ret)
53
+ {:match => m == :match, :reason => ret}
46
54
  end
47
55
 
48
56
  private
@@ -71,41 +79,64 @@ module Piece
71
79
  str =~ /^[\['"](.*)[\]'"]$/ ? $1 : str
72
80
  end
73
81
 
74
- def apply(group, actions)
82
+ def apply(group, actions, backtrace)
75
83
  case group
76
84
  when ExpressionParser::Exp
85
+ validate_rule_names(group)
77
86
  case group.op
78
87
  when '+'
79
- apply(group.left, actions) || apply(group.right, actions)
88
+ sub = []
89
+ if r = apply(group.left, actions, sub)
90
+ backtrace.concat(sub)
91
+ r
92
+ else
93
+ apply(group.right, actions, backtrace)
94
+ end
80
95
  when '-'
81
- apply(group.left, actions) && apply(group.right, actions).nil?
96
+ sub1, sub2 = [], []
97
+ if r = apply(group.left, actions, sub1)
98
+ if apply(group.right, actions, sub2).nil?
99
+ backtrace.concat(sub1)
100
+ r
101
+ else
102
+ backtrace.concat(sub2)
103
+ nil
104
+ end
105
+ else
106
+ backtrace.concat(sub1)
107
+ nil
108
+ end
82
109
  else
83
110
  raise "Unknown operator: #{group.op}"
84
111
  end
85
112
  when ExpressionParser::Id
86
113
  if @data.has_key?(group.val)
87
- get(@data[group.val], actions)
114
+ backtrace << group.val
115
+ eval(@data[group.val], actions, backtrace)
88
116
  else
89
- get(group, actions)
117
+ eval(group, actions, backtrace)
90
118
  end
91
119
  else
92
120
  raise "Unknown type: #{group.inspect}"
93
121
  end
94
122
  end
95
123
 
96
- def get(data, actions)
97
- return data.nil? ? nil : Array(data) if actions.empty?
124
+ def eval(data, actions, backtrace=[])
125
+ return data.nil? ? nil : :match if actions.empty?
98
126
  case data
99
127
  when Hash
100
- get(data[actions.first], actions[1..-1])
128
+ backtrace << actions.first
129
+ eval(data[actions.first], actions[1..-1], backtrace)
101
130
  when Array
102
- get(data.first, actions) || get(data[1..-1], actions)
131
+ backtrace << data
132
+ eval(data.first, actions) || eval(data[1..-1], actions)
103
133
  when NilClass
104
134
  nil
105
135
  when ExpressionParser::Id
106
- _match_?(data.val, actions.first) ? '*' : nil
136
+ _match_?(data.val, actions.first) ? :match : nil
107
137
  when String
108
- apply(ExpressionParser.new.parse(data), actions)
138
+ backtrace << data
139
+ apply(ExpressionParser.new.parse(data), actions, backtrace)
109
140
  else
110
141
  raise "Unknown type: #{group.inspect}"
111
142
  end
@@ -114,5 +145,18 @@ module Piece
114
145
  def _match_?(a, b)
115
146
  a == b || a.nil? || a == '*' || b == '*'
116
147
  end
148
+
149
+ def validate_rule_names(exp)
150
+ case exp
151
+ when ExpressionParser::Exp
152
+ validate_rule_names(exp.left)
153
+ validate_rule_names(exp.right)
154
+ when ExpressionParser::Id
155
+ if exp.val != '*' && !@data.has_key?(exp.val)
156
+ raise UnknownRuleGroupName,
157
+ "Expecting '#{exp.val}' in expression to be a root rule group name(root group names: #{@data.keys.inspect}) you defined. "
158
+ end
159
+ end
160
+ end
117
161
  end
118
162
  end
data/lib/piece/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Piece
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piece
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiao Li
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-05 00:00:00.000000000 Z
11
+ date: 2015-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler