strategy_bag 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.travis.yml +9 -0
- data/README.md +5 -0
- data/lib/strategy_bag.rb +22 -4
- data/lib/strategy_bag/version.rb +2 -2
- data/spec/strategic_calc.rb +16 -5
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YWU4ZGEyZmFkZGQ4ZGMyNThlYTBiOWZiNWE4NGZiNDY1YmU2M2IzNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZTkzNjYxMmRhYTU0ODM2ZDdhNzNhZDI2YjgyMWUyNjk5ODVhY2NhZQ==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZDk2NGE5ZWI2MThmMGY1ZThlMzExYWJlYTczM2I3NWQ2MjNjOTNiZDdkMjZk
|
10
|
+
NDIxNmZiYmVjZTlmOWI0N2Q0MzNiNWFmZmM1OWQxYjRlNWMwZDUwYmZmOTJh
|
11
|
+
MzZmZGMyNDE3NDU3Nzg2MzMyNjgwZjFkNjE5NWJhZjhkMWRjZmE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NWEyOTIxNzBmN2FlYjkwNjk4MTFhNmI1MTRlODM5N2FmMmRmNjlhNmZiMTg3
|
14
|
+
MzRkNzFhMmRjM2UxMGM3MmQwNzQ3NDFlMmEwYzg3MDcyMDI5YTg1NTdmMTdl
|
15
|
+
NjE5ZWZjNWVhMjQ3ZTc2NDNlYmEyMjc1Y2MyZDJkY2I0OWVhNzQ=
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# StrategyBag
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/strategy_bag.png)](http://badge.fury.io/rb/strategy_bag)
|
4
|
+
[![Build Status](https://travis-ci.org/bagilevi/strategy_bag.png)](https://travis-ci.org/bagilevi/strategy_bag)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/bagilevi/strategy_bag.png)](https://codeclimate.com/github/bagilevi/strategy_bag)
|
6
|
+
|
7
|
+
|
3
8
|
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.
|
4
9
|
|
5
10
|
## Installation
|
data/lib/strategy_bag.rb
CHANGED
@@ -1,13 +1,21 @@
|
|
1
1
|
class StrategyBag
|
2
2
|
|
3
3
|
def initialize(*args)
|
4
|
+
interface, *param_values = args
|
5
|
+
if param_values.size != self.class.param_names.size
|
6
|
+
raise ArgumentError, "expected params: interface, #{self.class.param_names.map(&:to_s).join(', ')}, but only #{args.size} params given"
|
7
|
+
end
|
4
8
|
@evaluator = Evaluator.new
|
9
|
+
@evaluator.interface = interface
|
5
10
|
self.class.param_names.each_with_index do |name, index|
|
6
|
-
@evaluator.define_value name,
|
11
|
+
@evaluator.define_value name, param_values[index]
|
7
12
|
end
|
8
13
|
end
|
9
14
|
|
10
15
|
def run
|
16
|
+
self.class.derived_attributes.each do |name, block|
|
17
|
+
@evaluator.define_func name, block
|
18
|
+
end
|
11
19
|
self.class.conditions.each do |positive_name, negative_name, p|
|
12
20
|
@evaluator.define_func positive_name, p
|
13
21
|
@evaluator.define_negative negative_name, positive_name
|
@@ -27,6 +35,8 @@ class StrategyBag
|
|
27
35
|
|
28
36
|
|
29
37
|
class Evaluator
|
38
|
+
attr_accessor :interface
|
39
|
+
|
30
40
|
def initialize
|
31
41
|
@table = {}
|
32
42
|
end
|
@@ -49,10 +59,12 @@ class StrategyBag
|
|
49
59
|
a.all? { |c| send(c) }
|
50
60
|
end
|
51
61
|
|
52
|
-
def method_missing(m, *args, &
|
62
|
+
def method_missing(m, *args, &block)
|
53
63
|
#puts "missing: #{m}"
|
54
64
|
if @table.has_key?(m.to_sym) && args.empty?
|
55
65
|
@table[m.to_sym]
|
66
|
+
elsif interface.respond_to?(m)
|
67
|
+
interface.public_send(m, *args, &block)
|
56
68
|
else
|
57
69
|
super
|
58
70
|
end
|
@@ -60,7 +72,7 @@ class StrategyBag
|
|
60
72
|
end
|
61
73
|
|
62
74
|
module ClassDSL
|
63
|
-
attr_accessor :param_names, :conditions, :strategies, :default_strategy
|
75
|
+
attr_accessor :param_names, :conditions, :strategies, :default_strategy, :derived_attributes
|
64
76
|
|
65
77
|
def params(*param_names)
|
66
78
|
self.param_names = param_names
|
@@ -72,7 +84,7 @@ class StrategyBag
|
|
72
84
|
self.conditions << [positive_name, negative_name, p]
|
73
85
|
end
|
74
86
|
|
75
|
-
def strategy(&block)
|
87
|
+
def strategy(title = nil, &block)
|
76
88
|
self.strategies ||= []
|
77
89
|
strategy = Strategy.new
|
78
90
|
strategy.instance_eval(&block)
|
@@ -82,6 +94,12 @@ class StrategyBag
|
|
82
94
|
def default(&block)
|
83
95
|
self.default_strategy = block
|
84
96
|
end
|
97
|
+
|
98
|
+
def derive(name, &block)
|
99
|
+
self.derived_attributes ||= []
|
100
|
+
self.derived_attributes << [name, block]
|
101
|
+
end
|
102
|
+
|
85
103
|
end
|
86
104
|
|
87
105
|
extend ClassDSL
|
data/lib/strategy_bag/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0
|
1
|
+
class StrategyBag
|
2
|
+
VERSION = "0.1.0"
|
3
3
|
end
|
data/spec/strategic_calc.rb
CHANGED
@@ -4,19 +4,30 @@ module StrategicCalc
|
|
4
4
|
extend self
|
5
5
|
|
6
6
|
def calc(a, b, c)
|
7
|
-
Solver.new(a, b, c).run
|
7
|
+
Solver.new(self, a, b, c).run
|
8
|
+
end
|
9
|
+
|
10
|
+
def cmax
|
11
|
+
20
|
12
|
+
end
|
13
|
+
|
14
|
+
def b_limit
|
15
|
+
10
|
8
16
|
end
|
9
17
|
|
10
18
|
class Solver < StrategyBag
|
11
19
|
params :a, :b, :c
|
12
20
|
|
21
|
+
derive(:bc) { b + c }
|
22
|
+
derive(:b_high) { b > b_limit }
|
23
|
+
|
13
24
|
condition(:a_high?, :a_low?) { a > 10 }
|
14
|
-
condition(:b_high?, :b_low?) {
|
25
|
+
condition(:b_high?, :b_low?) { b_high }
|
15
26
|
condition(:c_high?, :c_low?) { c > 10 }
|
16
|
-
condition(:c_max?, :c_not_max?) { c >
|
17
|
-
condition(:bc_high?, :bc_low?) {
|
27
|
+
condition(:c_max?, :c_not_max?) { c > cmax } # cmax is provided by the interface
|
28
|
+
condition(:bc_high?, :bc_low?) { bc > 10 }
|
18
29
|
|
19
|
-
strategy do
|
30
|
+
strategy "all high" do
|
20
31
|
condition :a_high?
|
21
32
|
condition :b_high?
|
22
33
|
condition :c_high?
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strategy_bag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Levente Bagi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ! '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ! '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Ruby DSL for making conditionals more readable by defining a set of strategies.
|
@@ -61,7 +61,8 @@ executables: []
|
|
61
61
|
extensions: []
|
62
62
|
extra_rdoc_files: []
|
63
63
|
files:
|
64
|
-
-
|
64
|
+
- .gitignore
|
65
|
+
- .travis.yml
|
65
66
|
- Gemfile
|
66
67
|
- LICENSE.txt
|
67
68
|
- README.md
|
@@ -84,17 +85,17 @@ require_paths:
|
|
84
85
|
- lib
|
85
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
86
87
|
requirements:
|
87
|
-
- -
|
88
|
+
- - ! '>='
|
88
89
|
- !ruby/object:Gem::Version
|
89
90
|
version: '0'
|
90
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
92
|
requirements:
|
92
|
-
- -
|
93
|
+
- - ! '>='
|
93
94
|
- !ruby/object:Gem::Version
|
94
95
|
version: '0'
|
95
96
|
requirements: []
|
96
97
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.1.11
|
98
99
|
signing_key:
|
99
100
|
specification_version: 4
|
100
101
|
summary: Ruby DSL for making conditionals more readable
|