hero 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -7
- data/lib/hero/formula.rb +13 -9
- data/spec/formula_spec.rb +32 -3
- metadata +2 -2
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Hero
|
2
2
|
|
3
|
-
![Hero GEM](http://hopsoft.github.com/hero/images/hero.jpg)
|
3
|
+
![Hero GEM](http://hopsoft.github.com/hero/images/hero.jpg)
|
4
4
|
|
5
5
|
## Its a bird, its a plane, its... its... my Hero
|
6
6
|
|
@@ -10,20 +10,20 @@
|
|
10
10
|
|
11
11
|
---
|
12
12
|
|
13
|
-
I've seen my share of poor app structure.
|
13
|
+
I've seen my share of poor app structure.
|
14
14
|
Hell, I wrote most of it.
|
15
15
|
Whether is fat controllers, giant models with mystery callbacks, or a junk drawer lib directory.
|
16
16
|
|
17
17
|
The question remains. **Where do I put my business logic?**
|
18
18
|
|
19
|
-
Finally... an answer that might even make DHH proud.
|
19
|
+
Finally... an answer that might even make DHH proud.
|
20
20
|
One that evolved from the real world with concrete use cases and actual production code.
|
21
21
|
|
22
22
|
## Why Hero?
|
23
23
|
|
24
24
|
* It matches the mental map of your business requirements
|
25
25
|
* It produces testable components
|
26
|
-
* It easily handles changing requirements
|
26
|
+
* It easily handles changing requirements
|
27
27
|
* It reduces the ramp up time for new team members
|
28
28
|
|
29
29
|
## Process Modeling
|
@@ -34,7 +34,7 @@ Things start simply enough but eventually edge cases force *gotchas* into
|
|
34
34
|
various libs, modules, and classes. Before you know you it,
|
35
35
|
you have a lump of spaghetti that's difficult to maintain and even harder to improve.
|
36
36
|
|
37
|
-
Hero provides a simple pattern that encourages you to
|
37
|
+
Hero provides a simple pattern that encourages you to
|
38
38
|
<a href="http://en.wikipedia.org/wiki/Decomposition_(computer_science)">decompose</a>
|
39
39
|
these processes into managable chunks.
|
40
40
|
|
@@ -87,11 +87,11 @@ Hero::Formula[:gather_news].add_step :email do |context, options|
|
|
87
87
|
end
|
88
88
|
```
|
89
89
|
|
90
|
-
This looks surprising similar to the requirements.
|
90
|
+
This looks surprising similar to the requirements.
|
91
91
|
In fact we can publish the specification directly from Hero.
|
92
92
|
|
93
93
|
```ruby
|
94
|
-
|
94
|
+
Hero::Formula[:gather_news].print
|
95
95
|
|
96
96
|
# => gather_news
|
97
97
|
# 1. hacker_news
|
data/lib/hero/formula.rb
CHANGED
@@ -40,11 +40,9 @@ module Hero
|
|
40
40
|
# Indicates the total number of registered formulas.
|
41
41
|
def_delegator :formulas, :length, :count
|
42
42
|
|
43
|
-
#
|
44
|
-
def
|
45
|
-
|
46
|
-
each { |name, formula| value << formula.to_s }
|
47
|
-
value.join("\n\n")
|
43
|
+
# Prints/puts a string representation of all registered formulas.
|
44
|
+
def print
|
45
|
+
formulas.values.each(&:print)
|
48
46
|
end
|
49
47
|
|
50
48
|
# Removes all registered formulas.
|
@@ -66,7 +64,13 @@ module Hero
|
|
66
64
|
# @return Hero::Formula
|
67
65
|
def register(name)
|
68
66
|
observer = Hero::Observer.new(name)
|
69
|
-
|
67
|
+
formula_class_name = name.to_s.strip.gsub(/\s/, "_").gsub(/[^a-z_]/i, "").split(/_/).map(&:capitalize).join
|
68
|
+
formula_class = Class.new(Hero::Formula)
|
69
|
+
if Hero::Formula.const_defined?(formula_class_name)
|
70
|
+
Hero::Formula.send(:remove_const, formula_class_name)
|
71
|
+
end
|
72
|
+
Hero::Formula.const_set(formula_class_name, formula_class)
|
73
|
+
formula = formula_class.instance
|
70
74
|
formula.add_observer(observer)
|
71
75
|
formula.instance_eval do
|
72
76
|
@name = name
|
@@ -136,18 +140,18 @@ module Hero
|
|
136
140
|
|
137
141
|
alias :run :notify
|
138
142
|
|
139
|
-
#
|
143
|
+
# Prints/puts a String representation of the formula.
|
140
144
|
# @example
|
141
145
|
# Hero::Formula[:example].add_step(:one) {}
|
142
146
|
# Hero::Formula[:example].add_step(:two) {}
|
143
147
|
# Hero::Formula[:example].add_step(:three) {}
|
144
148
|
# Hero::Formula[:example].to_s # => "example\n 1. one\n 2. two\n 3. three"
|
145
|
-
def
|
149
|
+
def print
|
146
150
|
value = [name]
|
147
151
|
steps.each_with_index do |step, index|
|
148
152
|
value << "#{(index + 1).to_s.rjust(3)}. #{step.first}"
|
149
153
|
end
|
150
|
-
value.join("\n")
|
154
|
+
puts value.join("\n")
|
151
155
|
end
|
152
156
|
|
153
157
|
end
|
data/spec/formula_spec.rb
CHANGED
@@ -8,6 +8,18 @@ describe Hero::Formula do
|
|
8
8
|
Hero::Formula.reset
|
9
9
|
end
|
10
10
|
|
11
|
+
it "should create a named class" do
|
12
|
+
Hero::Formula[:my_formula]
|
13
|
+
assert Hero::Formula.const_defined?("MyFormula")
|
14
|
+
assert_equal Hero::Formula[:my_formula].class.name, "Hero::Formula::MyFormula"
|
15
|
+
assert Hero::Formula[:my_formula].is_a? Hero::Formula::MyFormula
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should safely create a named class" do
|
19
|
+
Hero::Formula["A long and cr@zy f0rmul@ name ~12$%"]
|
20
|
+
assert Hero::Formula.const_defined?("ALongAndCrzyFrmulName")
|
21
|
+
end
|
22
|
+
|
11
23
|
it "should support reset" do
|
12
24
|
Hero::Formula.register(:test_formula)
|
13
25
|
assert_equal Hero::Formula.count, 1
|
@@ -53,8 +65,16 @@ describe Hero::Formula do
|
|
53
65
|
Hero::Formula[:second].add_step(:three) {}
|
54
66
|
Hero::Formula[:second].add_step(:four) {}
|
55
67
|
|
56
|
-
|
57
|
-
|
68
|
+
begin
|
69
|
+
out = StringIO.new
|
70
|
+
$stdout = out
|
71
|
+
Hero::Formula.print
|
72
|
+
expected = "first\n 1. one\n 2. two\n 3. three\n 4. four\nsecond\n 1. one\n 2. two\n 3. three\n 4. four\n"
|
73
|
+
out.rewind
|
74
|
+
assert_equal out.readlines.join, expected
|
75
|
+
ensure
|
76
|
+
$stdout = STDOUT
|
77
|
+
end
|
58
78
|
end
|
59
79
|
|
60
80
|
describe "a registered formula" do
|
@@ -112,7 +132,16 @@ describe Hero::Formula do
|
|
112
132
|
Hero::Formula[:test_formula].add_step(:three) {}
|
113
133
|
Hero::Formula[:test_formula].add_step(:four) {}
|
114
134
|
expected = "test_formula 1. one 2. two 3. three 4. four"
|
115
|
-
|
135
|
+
begin
|
136
|
+
out = StringIO.new
|
137
|
+
$stdout = out
|
138
|
+
Hero::Formula[:test_formula].print
|
139
|
+
expected = "test_formula\n 1. one\n 2. two\n 3. three\n 4. four\n"
|
140
|
+
out.rewind
|
141
|
+
assert_equal out.readlines.join, expected
|
142
|
+
ensure
|
143
|
+
$stdout = STDOUT
|
144
|
+
end
|
116
145
|
end
|
117
146
|
|
118
147
|
it "should support logging" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hero
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
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-08-
|
12
|
+
date: 2012-08-28 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! ' Simplify your apps with Hero.
|
15
15
|
|