ruva 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
File without changes
data/.travis.yml CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://secure.travis-ci.org/ssmm/ruva.png)](http://travis-ci.org/ssmm/ruva)
4
4
  [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/ssmm/ruva)
5
5
 
6
- Ruva is a simple utility to write conditions in a human readable manner.
6
+ A library to write plain text conditions.
7
7
 
8
8
  Assume you have an object called `person` with an attribute `age = 23`:
9
9
 
@@ -27,7 +27,7 @@ You can also define more complex conditions:
27
27
  all
28
28
  city matches zurich
29
29
  profession matches /application engineer/
30
- age is greater than or equal to 18
30
+ age is greater than or equal to 18
31
31
 
32
32
  In plain ruby, the above condition translates to
33
33
 
@@ -69,9 +69,9 @@ You may define larger conditions like this:
69
69
  ```ruby
70
70
  person.if "
71
71
  any
72
- name matches lisa or simon or samuel
72
+ name matches daniel or jonas or samuel
73
73
  age is between 18 and 28
74
- city matches zurich or /st. gallen/
74
+ city matches zurich or /new york/
75
75
  " do
76
76
  stuff
77
77
  end
@@ -87,9 +87,9 @@ Currently supported are the keywords `all`, `any` and `none`.
87
87
  You can define the same condition in a file with ".ruva" as extension:
88
88
 
89
89
  any
90
- name matches lisa or simon or samuel
90
+ name matches daniel or jonas or samuel
91
91
  age is between 18 and 28
92
- city matches zurich or /st. gallen/
92
+ city matches zurich or /new york/
93
93
 
94
94
  Then you load it:
95
95
 
data/Rakefile CHANGED
File without changes
File without changes
@@ -3,38 +3,37 @@ class ExpressionLeafSpec < LeafSpec
3
3
  if (args.size == 1)
4
4
  obj = args[0]
5
5
  if (obj.is_a? Hash)
6
- get_hash_value @name.split(".").reverse, obj
7
- else
8
- get_object_value @name.split(".").reverse, obj
6
+ obj = OpenStruct.new obj
9
7
  end
8
+ get_object_value @name.split(".").reverse, obj
10
9
  else
11
10
  raise "Unexpected input"
12
11
  end
13
12
  end
14
-
15
- def get_hash_value key_array, hash
16
- if (key_array.size > 1)
17
- obj = hash[key_array.pop.to_sym]
18
- if (obj.is_a?(Hash))
19
- get_hash_value(key_array, obj)
20
- else
21
- get_object_value(key_array, obj)
22
- end
23
- else
24
- hash[key_array.pop.to_sym]
25
- end
26
- end
13
+
14
+ # def get_hash_value key_array, hash
15
+ # if (key_array.size > 1)
16
+ # obj = hash[key_array.pop.to_sym]
17
+ # if (obj.is_a?(Hash))
18
+ # get_hash_value(key_array, obj)
19
+ # else
20
+ # get_object_value(key_array, obj)
21
+ # end
22
+ # else
23
+ # obj = OpenStruct.new hash
24
+ # obj.instance_eval(key_array.pop)
25
+ # end
26
+ # end
27
27
 
28
28
  def get_object_value key_array, obj
29
29
  if (key_array.size > 1)
30
- obj = obj.send key_array.pop
30
+ obj = obj.instance_eval(key_array.pop)
31
31
  if (obj.is_a?(Hash))
32
- get_hash_value(key_array, obj)
33
- else
34
- get_object_value(key_array, obj)
32
+ obj = OpenStruct.new obj
35
33
  end
34
+ get_object_value(key_array, obj)
36
35
  else
37
- obj.send key_array.pop
36
+ obj.instance_eval(key_array.pop)
38
37
  end
39
38
  end
40
39
 
File without changes
@@ -38,7 +38,11 @@ module IsBetween
38
38
  comparables = [from.value, to.value]
39
39
  spec = ExpressionLeafSpec.new(identifier.value, comparables)
40
40
  spec.set_validator { |value, *args|
41
- value.between? @comparable[0], @comparable[1]
41
+ if value
42
+ value.between? @comparable[0], @comparable[1]
43
+ else
44
+ false
45
+ end
42
46
  }
43
47
  spec.set_reporting { |satisfied, value, *args|
44
48
  comparable_string = "#{@comparable[0]} and #{@comparable[1]}"
File without changes
@@ -17,6 +17,10 @@ grammar RuvaExpression
17
17
  |
18
18
  (identifier is less than comparable) <Less>
19
19
  |
20
+ (identifier is comparable or more) <GreaterOrEqual>
21
+ |
22
+ (identifier is comparable or less) <LessOrEqual>
23
+ |
20
24
  (identifier is comparables:(or_comparables)) <OrIs>
21
25
  |
22
26
  (identifier is comparable) <Is>
@@ -41,7 +45,11 @@ grammar RuvaExpression
41
45
  rule less
42
46
  'less' space
43
47
  end
44
-
48
+
49
+ rule more
50
+ 'more' space
51
+ end
52
+
45
53
  rule than
46
54
  'than' space
47
55
  end
@@ -62,8 +70,8 @@ grammar RuvaExpression
62
70
  'or' space
63
71
  end
64
72
 
65
- rule identifier
66
- ([a-zA-Z.]+ space) <Identifier>
73
+ rule identifier
74
+ ([a-zA-Z0-9_.\[\]]+ space) <Identifier>
67
75
  end
68
76
 
69
77
  rule comparable
File without changes
@@ -18,18 +18,28 @@ class Spec
18
18
  end
19
19
  end
20
20
 
21
+ def report satisfied, *args
22
+ # report = SpecReport.new false, name
23
+ # @specs.each do |spec|
24
+ # subreport = create_subreport spec, *args
25
+ # report.add_subreport subreport
26
+ # if condition_change? subreport.satisfied
27
+ # passed = !initial_condition
28
+ # end
29
+ # end
30
+ # report.satisfied = passed
31
+ # report
32
+ end
33
+
21
34
  def evaluate *args
22
35
  passed = initial_condition
23
- report = SpecReport.new false, name
24
36
  @specs.each do |spec|
25
- subreport = create_subreport spec, *args
26
- report.add_subreport subreport
27
- if condition_change? subreport.satisfied
37
+ result = spec.evaluate *args
38
+ if condition_change? result
28
39
  passed = !initial_condition
29
40
  end
30
41
  end
31
- report.satisfied = passed
32
- report
42
+ passed
33
43
  end
34
44
  end
35
45
 
File without changes
data/lib/ruva/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ruva
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/lib/ruva.rb CHANGED
@@ -8,8 +8,8 @@ class Object
8
8
  raise unless block_given?
9
9
  spec = Ruva.interpret(Ruva.normalize_indentation condition)
10
10
  result = spec.evaluate self
11
- yield if result.satisfied
12
- Ruva::Else.new result.satisfied
11
+ yield if result
12
+ Ruva::Else.new result
13
13
  end
14
14
  end
15
15
 
@@ -60,6 +60,18 @@ module Ruva
60
60
  str.gsub!(/^#{first_indentation}/, "")
61
61
  str
62
62
  end
63
+
64
+ def self.hash_to_ostruct hash
65
+ ostruct = OpenStruct.new
66
+ hash.each do |key, value|
67
+ if value.is_a? Hash
68
+ ostruct.send("#{key}=", hash_to_ostruct(value))
69
+ else
70
+ ostruct.send("#{key}=", value)
71
+ end
72
+ end
73
+ ostruct
74
+ end
63
75
 
64
76
  class Else
65
77
  def initialize satisfied
data/ruva.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
 
8
8
  gem.authors = ["Samuel Mueller"]
9
9
  gem.email = ["mueller.samu@gmail.com"]
10
- gem.description = %q{Ruva is a simple utility to write conditions in a human readable manner.}
10
+ gem.description = %q{A library to write plain text conditions.}
11
11
  gem.summary = gem.description
12
12
  gem.homepage = "https://github.com/ssmm/ruva"
13
13
 
File without changes
@@ -6,16 +6,42 @@ RSpec.configure do |c|
6
6
  end
7
7
 
8
8
  describe Ruva do
9
-
10
- before :all do
11
-
12
- end
13
-
14
- describe "greater/less/equal expressions" do
9
+
10
+ describe "is/between expressions" do
15
11
  it "handles numerics" do
16
12
  test_expression "age is 23", {:age => 23}, true
17
13
  test_expression "age is 23", {:age => 24}, false
14
+ test_expression "age is 23 or 24 or 25", {:age => 24}, true
15
+
16
+ test_expression "age is between 18 and 28", {:age => 23}, true
17
+
18
+ test_expression "age is between 18 and 28", {:age => 28}, true
19
+ test_expression "age is between 18 and 28", {:age => 18}, true
18
20
 
21
+ test_expression "age is between 18 and 28", {:age => 50}, false
22
+ test_expression "age is between 18 and 28", {:age => 17}, false
23
+ end
24
+
25
+ it "handles strings" do
26
+ test_expression "name is samuel", {:name => "samuel"}, true
27
+ test_expression "name is samuel", {:name => "melchior"}, false
28
+ test_expression "name is sam", {:name => "samuel"}, false
29
+
30
+ test_expression "letter is between a and c", {:letter => "b"}, true
31
+ test_expression "letter is between a and c", {:letter => "d"}, false
32
+ test_expression "city is between zue and zur", {:city => "zuf"}, true
33
+ test_expression "city is between zue and zur", {:city => "zua"}, false
34
+ end
35
+
36
+ it "handles dates" do
37
+ test_expression "date is 02.02.2002", {:date => Date.parse("2.2.2002")}, true
38
+ test_expression "date is between 01.01.2000 and 31.12.2000", {:date => Date.parse("1.7.2000")}, true
39
+ test_expression "date is between 01.01.2000 and 31.12.2000", {:date => Date.parse("1.7.2001")}, false
40
+ end
41
+ end
42
+
43
+ describe "greater/less/equal expressions" do
44
+ it "handles numerics" do
19
45
  test_expression "age is greater than 23", {:age => 24}, true
20
46
  test_expression "age is greater than 23", {:age => 23}, false
21
47
 
@@ -27,12 +53,17 @@ describe Ruva do
27
53
 
28
54
  test_expression "age is less than or equal to 23", {:age => 23}, true
29
55
  test_expression "age is less than or equal to 23", {:age => 24}, false
56
+
57
+ test_expression "age is 23 or more", {:age => 23}, true
58
+ test_expression "age is 23 or more", {:age => 22}, false
59
+ test_expression "age is 23 or more", {:age => 24}, true
60
+
61
+ test_expression "age is 23 or less", {:age => 23}, true
62
+ test_expression "age is 23 or less", {:age => 24}, false
63
+ test_expression "age is 23 or less", {:age => 22}, true
30
64
  end
31
65
 
32
- it "handles strings" do
33
- test_expression "name is samuel", {:name => "samuel"}, true
34
- test_expression "name is samuel", {:name => "melchior"}, false
35
-
66
+ it "handles strings" do
36
67
  test_expression "name is greater than samuel", {:name => "simon"}, true
37
68
  test_expression "name is greater than samuel", {:name => "samantha"}, false
38
69
 
@@ -47,19 +78,17 @@ describe Ruva do
47
78
  end
48
79
 
49
80
  it "handes dates" do
50
- #TODO!!
51
- end
52
- end
53
-
54
- describe "between expressions" do
55
- it "handles numerics" do
56
- test_expression "age is between 18 and 28", {:age => 23}, true
57
-
58
- test_expression "age is between 18 and 28", {:age => 28}, true
59
- test_expression "age is between 18 and 28", {:age => 18}, true
60
-
61
- test_expression "age is between 18 and 28", {:age => 50}, false
62
- test_expression "age is between 18 and 28", {:age => 17}, false
81
+ test_expression "date is greater than 01.01.2011", {:date => Date.parse("1.1.2012")}, true
82
+ test_expression "date is greater than 01.01.2011", {:date => Date.parse("1.1.2010")}, false
83
+
84
+ test_expression "date is less than 01.01.2011", {:date => Date.parse("1.1.2010")}, true
85
+ test_expression "date is less than 01.01.2011", {:date => Date.parse("1.1.2012")}, false
86
+
87
+ test_expression "date is greater than or equal to 01.01.2011", {:date => Date.parse("1.1.2012")}, true
88
+ test_expression "date is greater than or equal to 01.01.2011", {:date => Date.parse("1.1.2010")}, false
89
+
90
+ test_expression "date is less than or equal to 01.01.2011", {:date => Date.parse("1.1.2010")}, true
91
+ test_expression "date is less than or equal to 01.01.2011", {:date => Date.parse("1.1.2012")}, false
63
92
  end
64
93
  end
65
94
 
@@ -81,5 +110,46 @@ describe Ruva do
81
110
  test_expression "name matches /regex\\/with escaping/", {:name => "regex/with escaping"}, true
82
111
  end
83
112
  end
84
-
113
+
114
+ describe "misc functions" do
115
+ it "handles deep attributes" do
116
+ obj = {
117
+ :person => {
118
+ :address => {
119
+ :zip => 12345
120
+ }
121
+ }
122
+ }
123
+ test_expression "person.address.zip is 12345", obj, true
124
+
125
+ obj = OpenStruct.new
126
+ person = OpenStruct.new
127
+ person.address = {
128
+ :zip => 12345
129
+ }
130
+ obj.person = person
131
+ test_expression "person.address.zip is 12345", obj, true
132
+ end
133
+
134
+ it "handles arrays" do
135
+ obj = OpenStruct.new
136
+ obj.ary = [10, 11, 12]
137
+ test_expression "ary[1] is 11", obj, true
138
+
139
+ obj = OpenStruct.new
140
+ map = {:ary => [10, 11, 12]}
141
+ obj.map = map
142
+ test_expression "map.ary[2] is 12", obj, true
143
+
144
+ obj = OpenStruct.new
145
+ map = {:list => [
146
+ {:first => 1},
147
+ {:second => 2},
148
+ {:third => 3}
149
+ ]}
150
+ obj.map = map
151
+ puts obj
152
+ test_expression "map.list[1].second is 2", obj, true
153
+ end
154
+ end
85
155
  end
@@ -0,0 +1,23 @@
1
+ require 'ruva'
2
+
3
+ describe Ruva do
4
+ it "turns a hash into an ostruct" do
5
+ person = OpenStruct.new
6
+ person.name = "sam"
7
+ person.age = 23
8
+ hash = {
9
+ :this => {
10
+ :ostruct => person,
11
+ :is => {
12
+ :a => {
13
+ :test => "works!"
14
+ }
15
+ }
16
+ }
17
+ }
18
+ ostruct = Ruva.hash_to_ostruct hash
19
+ ostruct.this.is.a.test.should eq "works!"
20
+ ostruct.this.ostruct.name.should eq "sam"
21
+ ostruct.this.ostruct.age.should eq 23
22
+ end
23
+ end
File without changes
File without changes
File without changes
data/spec/ruva_spec.rb CHANGED
@@ -12,9 +12,8 @@ describe Ruva do
12
12
  input.profession = "application engineer"
13
13
  input.city = "zurich"
14
14
 
15
- result = spec.evaluate(input)
15
+ result = spec.evaluate input
16
16
 
17
- result.satisfied.should be true
18
- #puts result
17
+ result.should be true
19
18
  end
20
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruva
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-21 00:00:00.000000000 Z
12
+ date: 2012-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: citrus
@@ -59,7 +59,7 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '2.6'
62
- description: Ruva is a simple utility to write conditions in a human readable manner.
62
+ description: A library to write plain text conditions.
63
63
  email:
64
64
  - mueller.samu@gmail.com
65
65
  executables: []
@@ -86,6 +86,7 @@ files:
86
86
  - ruva.gemspec
87
87
  - spec/conditions/simple_condition.ruva
88
88
  - spec/expression_spec.rb
89
+ - spec/hash_to_ostruct_spec.rb
89
90
  - spec/helpers/expression_helper.rb
90
91
  - spec/inline_ruva_spec.rb
91
92
  - spec/normalize_indentation_spec.rb
@@ -113,10 +114,11 @@ rubyforge_project:
113
114
  rubygems_version: 1.8.24
114
115
  signing_key:
115
116
  specification_version: 3
116
- summary: Ruva is a simple utility to write conditions in a human readable manner.
117
+ summary: A library to write plain text conditions.
117
118
  test_files:
118
119
  - spec/conditions/simple_condition.ruva
119
120
  - spec/expression_spec.rb
121
+ - spec/hash_to_ostruct_spec.rb
120
122
  - spec/helpers/expression_helper.rb
121
123
  - spec/inline_ruva_spec.rb
122
124
  - spec/normalize_indentation_spec.rb