strategy_bag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,68 @@
1
+ require 'strategy_bag'
2
+
3
+ module StrategicCalc
4
+ extend self
5
+
6
+ def calc(a, b, c)
7
+ Solver.new(a, b, c).run
8
+ end
9
+
10
+ class Solver < StrategyBag
11
+ params :a, :b, :c
12
+
13
+ condition(:a_high?, :a_low?) { a > 10 }
14
+ condition(:b_high?, :b_low?) { b > 10 }
15
+ condition(:c_high?, :c_low?) { c > 10 }
16
+ condition(:c_max?, :c_not_max?) { c > 20 }
17
+ condition(:bc_high?, :bc_low?) { b + c > 10 }
18
+
19
+ strategy do
20
+ condition :a_high?
21
+ condition :b_high?
22
+ condition :c_high?
23
+ action do
24
+ a + b - 2 * c
25
+ end
26
+ end
27
+
28
+ strategy do
29
+ condition :a_high?
30
+ condition :b_high?
31
+ condition :c_low?
32
+ action do
33
+ a + b + c
34
+ end
35
+ end
36
+
37
+ strategy do
38
+ condition :a_high?
39
+ condition :b_low?
40
+ condition :c_max?
41
+ action do
42
+ a + b - c
43
+ end
44
+ end
45
+
46
+ strategy do
47
+ condition :a_high?
48
+ condition :b_low?
49
+ condition :c_not_max?
50
+ action do
51
+ a + b + c
52
+ end
53
+ end
54
+
55
+ strategy do
56
+ condition :a_low?
57
+ condition :bc_high?
58
+ action do
59
+ b + c - a
60
+ end
61
+ end
62
+
63
+ default do
64
+ nil
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'strategic_calc'
2
+ require_relative 'shared_examples_for_calc'
3
+
4
+ describe StrategicCalc do
5
+ include StrategicCalc
6
+
7
+ it_should_behave_like "calc"
8
+ end
9
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'strategy_bag/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "strategy_bag"
8
+ spec.version = StrategyBag::VERSION
9
+ spec.authors = ["Levente Bagi"]
10
+ spec.email = ["bagilevi@gmail.com"]
11
+ spec.summary = %q{Ruby DSL for making conditionals more readable}
12
+ spec.description = %q{Ruby DSL for making conditionals more readable by defining a set of strategies. Each strategy has a set of conditions and an action. The runner will select a strategy that meets the conditions and executes it.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strategy_bag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Levente Bagi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ruby DSL for making conditionals more readable by defining a set of strategies.
56
+ Each strategy has a set of conditions and an action. The runner will select a strategy
57
+ that meets the conditions and executes it.
58
+ email:
59
+ - bagilevi@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/strategy_bag.rb
70
+ - lib/strategy_bag/version.rb
71
+ - spec/calc.rb
72
+ - spec/calc_spec.rb
73
+ - spec/shared_examples_for_calc.rb
74
+ - spec/strategic_calc.rb
75
+ - spec/strategic_calc_spec.rb
76
+ - strategy_bag.gemspec
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Ruby DSL for making conditionals more readable
101
+ test_files:
102
+ - spec/calc.rb
103
+ - spec/calc_spec.rb
104
+ - spec/shared_examples_for_calc.rb
105
+ - spec/strategic_calc.rb
106
+ - spec/strategic_calc_spec.rb