rulezilla 0.1.4

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.
@@ -0,0 +1,64 @@
1
+ steps_for :rule_steps do
2
+
3
+ step 'this is a "cat"' do
4
+ @records = [{cat?: true}]
5
+ end
6
+
7
+ step 'the :field is :value' do |field, value|
8
+ record ||= {}
9
+ field = field.gsub(/\s/,'_').downcase
10
+
11
+ value = case value
12
+ when 'blank'
13
+ ""
14
+ when /^less than \d+ digits$/i
15
+ value.scan(/^less than \d+ digits$/i).flatten.first.to_i.times.map{Random.rand(10)}.join
16
+ else
17
+ value
18
+ end
19
+
20
+ record[field] = value
21
+ @records = [record]
22
+ end
23
+
24
+ step 'the :field is in:' do |field, values|
25
+ @records = []
26
+ field = field.gsub(/\s/,'_').downcase
27
+ values.raw.flatten.each do |telephone_number|
28
+ @records << { field => telephone_number }
29
+ end
30
+ end
31
+
32
+ step 'this is an animal' do
33
+ @records.each do |record|
34
+ expect(Rulezilla::AnimalRule.apply(record)).to eq true
35
+ end
36
+ end
37
+
38
+ step 'this is not an animal' do
39
+ @records.each do |record|
40
+ expect(Rulezilla::AnimalRule.apply(record)).to eq false
41
+ end
42
+ end
43
+
44
+ step 'none of the above' do
45
+ @records = [{telephone_number: '09827364755'}]
46
+ end
47
+
48
+ step 'the duration is :day days' do |day|
49
+ send 'the duration is :second seconds', day.to_i * 86400
50
+ end
51
+
52
+ step 'the duration is :hour hours' do |hour|
53
+ send 'the duration is :second seconds', hour.to_i * 3600
54
+ end
55
+
56
+ step 'the duration is :minute minute' do |minute|
57
+ send 'the duration is :second seconds', minute.to_i * 60
58
+ end
59
+
60
+ step 'the duration is :second seconds' do |second|
61
+ expect(Rulezilla::DurationRule.apply(@records.first)).to eq second.to_i
62
+ end
63
+
64
+ end
@@ -0,0 +1,119 @@
1
+ step 'the gherkin is:' do |gherkin|
2
+ @rule_klass_name = 'DummyRule'
3
+ @rule_klass = Rulezilla::RuleBuilder.new(@rule_klass_name, gherkin).build
4
+ end
5
+
6
+ step 'the incorrect gherkin is:' do |gherkin|
7
+ @gherkin = gherkin
8
+ end
9
+
10
+ step 'it raises exception :exception' do |exception|
11
+ @record ||= {}
12
+
13
+ result = result == 'nil' ? nil : result
14
+
15
+ expect{ @rule_klass.apply(@record) }.to raise_error{ RuntimeError.new(exception) }
16
+ end
17
+
18
+ step 'the rule class name is :klass_name' do |klass_name|
19
+ @rule_klass_name = klass_name
20
+ end
21
+
22
+ step 'the rule is:' do |rules|
23
+ @rule_klass = Object.const_set(@rule_klass_name || "DummyRule", Class.new)
24
+ @rule_klass.class_eval('include Rulezilla::DSL')
25
+ @rule_klass.class_eval(rules.to_s)
26
+ end
27
+
28
+ step 'our rule is:' do |rules|
29
+ @rule_klass = Object.const_set("DummyRule", Class.new)
30
+ @rule_klass.class_eval('include Rulezilla::DSL')
31
+ @rule_klass.class_eval(rules.to_s)
32
+ end
33
+
34
+ step 'there is a rule called :rule_name:' do |rule_name, rules|
35
+ send 'the rule class name is :klass_name', rule_name
36
+ @rule_klass = Object.const_set(@rule_klass_name || "DummyRule", Class.new)
37
+ @rule_klass.class_eval('include Rulezilla::DSL')
38
+ @rule_klass.class_eval(rules.to_s)
39
+ end
40
+
41
+ step 'the support module called :support_klass_name has definition:' do |support_name, support_definition|
42
+ @support_name = support_name
43
+ @support = Object.const_set(support_name, Module.new)
44
+ @support.class_eval(support_definition)
45
+ end
46
+
47
+ step 'the record has attribute :method and returns :value' do |method, value|
48
+ @record ||= {}
49
+ value = case value
50
+ when 'true'
51
+ true
52
+ when 'false'
53
+ false
54
+ when 'nil'
55
+ nil
56
+ else
57
+ value
58
+ end
59
+ @record[method] = value
60
+ end
61
+
62
+ step 'the record has attribute :attributes' do |attributes|
63
+ @record ||= {}
64
+ attributes = attributes.split(',').map(&:strip)
65
+ attributes.each do |key|
66
+ @record[key] = true
67
+ end
68
+ end
69
+
70
+ step 'the result is :result' do |result|
71
+ @record ||= {}
72
+
73
+ result = case result
74
+ when 'true'
75
+ true
76
+ when 'false'
77
+ false
78
+ when 'nil'
79
+ nil
80
+ else
81
+ result
82
+ end
83
+
84
+ expect(@rule_klass.apply(@record)).to eq result
85
+ end
86
+
87
+ step 'all the outcomes are :outcomes' do |outcomes|
88
+ outcomes = outcomes.split(',').map(&:strip)
89
+ expect(@rule_klass.results).to match_array outcomes
90
+ end
91
+
92
+ step 'all the matching outcomes are :outcomes' do |outcomes|
93
+ outcomes = outcomes.split(',').map(&:strip)
94
+ expect(@rule_klass.all(@record)).to match_array outcomes
95
+ end
96
+
97
+ step ':does_or_does_not raise the exception :exception' do |does_or_does_not, exception|
98
+ if does_or_does_not == 'does'
99
+ expect{ @rule_klass.apply @record }.to raise_error do |exception|
100
+ expect(exception.message).to match /#{exception}/
101
+ end
102
+ else
103
+ expect{ @rule_klass.apply @record }.not_to raise_error
104
+ end
105
+ end
106
+
107
+ step 'it raises exception :exception' do |exception|
108
+ expect {
109
+ @rule_klass_name = 'DummyRule'
110
+ @rule_klass = Rulezilla::RuleBuilder.new(@rule_klass_name, @gherkin).build
111
+ }.to raise_error do |exception|
112
+ expect(exception.message).to match /#{exception}/
113
+ end
114
+ end
115
+
116
+ step 'the trace is :trace' do |trace|
117
+ trace = trace.split('->').map(&:strip)
118
+ expect(@rule_klass.trace(@record).map(&:name)).to eq trace
119
+ end
@@ -0,0 +1,21 @@
1
+ require 'rulezilla'
2
+ require 'pry'
3
+
4
+ Dir.glob("spec/features/step_definitions/**/*steps.rb") { |f| load f, true }
5
+
6
+ Rulezilla.gherkin_rules_path = File.join(Dir.pwd, 'spec/features/gherkin_rules')
7
+
8
+ RSpec.configure do |config|
9
+ config.after(:each) do
10
+ Object.send(:remove_const, ('DummyRule').to_sym) rescue NameError
11
+ Object.send(:remove_const, 'DummyRuleRecord'.to_sym) rescue NameError
12
+ Object.send(:remove_const, 'DummyRuleSupport'.to_sym) rescue NameError
13
+ Object.send(:remove_const, (@rule_klass_name).to_sym) rescue NameError
14
+ Object.send(:remove_const, "#{@rule_klass_name}Record".to_sym) rescue NameError
15
+ Object.send(:remove_const, "#{@support_name}Support".to_sym) rescue NameError
16
+
17
+ Rulezilla.send(:remove_const, (@rule_klass_name || 'DummyRule').to_sym) rescue NameError
18
+ Rulezilla.send(:remove_const, "#{(@rule_klass_name || 'DummyRule')}Record".to_sym) rescue NameError
19
+ Rulezilla.send(:remove_const, "#{(@support_name || 'DummyRule')}Support".to_sym) rescue NameError
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rulezilla
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Peter Wu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gherkin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Rules DSL
28
+ email:
29
+ - peter.wu@simplybusiness.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - README.md
39
+ - lib/rulezilla.rb
40
+ - lib/rulezilla/basic_support.rb
41
+ - lib/rulezilla/dsl.rb
42
+ - lib/rulezilla/node.rb
43
+ - lib/rulezilla/rule_builder.rb
44
+ - lib/rulezilla/rule_builder/gherkin_to_condition_rule.rb
45
+ - lib/rulezilla/rule_builder/gherkin_to_result_rule.rb
46
+ - lib/rulezilla/tree.rb
47
+ - lib/rulezilla/version.rb
48
+ - rulezilla.gemspec
49
+ - spec/features/default_support_methods.feature
50
+ - spec/features/gherkin_dsl_framework.feature
51
+ - spec/features/gherkin_rules/animal_rule.feature
52
+ - spec/features/gherkin_rules/duration_rule.feature
53
+ - spec/features/rulezilla_dsl_framwork.feature
54
+ - spec/features/step_definitions/rule_steps.rb
55
+ - spec/features/step_definitions/rulezilla_dsl_framework_steps.rb
56
+ - spec/spec_helper.rb
57
+ homepage: https://github.com/simplybusiness/rulezilla
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.14
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Rules DSL
81
+ test_files: []
82
+ has_rdoc: