claus 0.2.0 → 0.3.0

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.
Files changed (4) hide show
  1. data/README.md +19 -13
  2. data/lib/claus.rb +13 -8
  3. data/test/test_claus.rb +8 -0
  4. metadata +1 -1
data/README.md CHANGED
@@ -6,19 +6,25 @@ Simple rule expression using a combination of Array and Hash.
6
6
 
7
7
  ```ruby
8
8
 
9
- require 'claus'
10
- claus = Claus.new(foo: 1, bar: 2)
11
- claus.match(foo: 1, bar: 2) #=> true
12
- claus.match(foo: 1, bar: 3) #=> false
13
- claus.match(foo: 1, bar: 2, baz: 3) #=> true
14
-
15
- claus = Claus.new(foo: 1, bar: 1..2)
16
- claus.match(foo: 1, bar: 2) #=> true
17
-
18
- claus = Claus.new([{foo:1}, {bar:2}])
19
- claus.match?(foo: 1) #=> true
20
- claus.match?(bar: 2) #=> true
21
- claus.match?(baz: 3) #=> false
9
+ require 'claus'
10
+ claus = Claus.new(foo: 1, bar: 2)
11
+ claus.match(foo: 1, bar: 2) #=> true
12
+ claus.match(foo: 1, bar: 3) #=> false
13
+ claus.match(foo: 1, bar: 2, baz: 3) #=> true
14
+
15
+ claus = Claus.new(foo: 1, bar: 1..2)
16
+ claus.match(foo: 1, bar: 2) #=> true
17
+
18
+ claus = Claus.new([{foo:1}, {bar:2}])
19
+ claus.match?(foo: 1) #=> true
20
+ claus.match?(bar: 2) #=> true
21
+ claus.match?(baz: 3) #=> false
22
+
23
+ # Chaining AST nodes - same as above.
24
+ claus = Claus.new [{foo: 1}, Claus.new(bar: 2)]
25
+ claus.match?(foo: 1) #=> true
26
+ claus.match?(bar: 2) #=> true
27
+ claus.match?(baz: 3) #=> false
22
28
  ```
23
29
 
24
30
  # License
@@ -15,6 +15,8 @@
15
15
  # claus.match?(baz: 3) #=> false
16
16
  #
17
17
  class Claus
18
+ attr_reader :ast
19
+
18
20
  def initialize expression
19
21
  @ast = compile(expression)
20
22
  end
@@ -26,9 +28,10 @@ class Claus
26
28
  protected
27
29
  def compile expression
28
30
  case expression
29
- when Hash then AST::Hash.new(expression)
30
- when Array then AST::List.new(expression)
31
- else raise ArgumentError, "invalid expression at #{expression}"
31
+ when Hash then AST::Hash.new(expression)
32
+ when Array then AST::List.new(expression)
33
+ when AST::Node then expression
34
+ else raise ArgumentError, "invalid expression at #{expression}"
32
35
  end
33
36
  end
34
37
 
@@ -56,7 +59,8 @@ class Claus
56
59
  when ::Hash then h[k] = Hash.new(v)
57
60
  when ::Array then h[k] = List.new(v)
58
61
  when ::Range then h[k] = List.new(v)
59
- else h[k] = Node.new(v)
62
+ when Claus then h[k] = v.ast
63
+ else h[k] = v.kind_of?(Node) ? v : Node.new(v)
60
64
  end
61
65
  end
62
66
  end
@@ -74,10 +78,11 @@ class Claus
74
78
  def compile expression
75
79
  expression.map do |v|
76
80
  case v
77
- when ::Hash then Hash.new(v)
78
- when ::Array then List.new(v)
79
- when ::Range then List.new(v)
80
- else Node.new(v)
81
+ when ::Hash then Hash.new(v)
82
+ when ::Array then List.new(v)
83
+ when ::Range then List.new(v)
84
+ when Claus then v.ast
85
+ else v.kind_of?(Node) ? v : Node.new(v)
81
86
  end
82
87
  end
83
88
  end
@@ -48,4 +48,12 @@ describe 'claus' do
48
48
  assert_equal false, claus.match?(foo: 1)
49
49
  assert_equal false, claus.match?(foo: 2)
50
50
  end
51
+
52
+ it 'should allow ast chaining' do
53
+ claus = Claus.new([Claus.new(foo: 1), Claus.new(bar: 2).ast])
54
+ assert_equal true, claus.match?(foo: 1)
55
+ assert_equal true, claus.match?(bar: 2)
56
+ assert_equal false, claus.match?(foo: 2)
57
+ assert_equal false, claus.match?(bar: 1)
58
+ end
51
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: claus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: