hero 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -87,7 +87,7 @@ This looks surprising similar to the requirements.
87
87
  In fact we can publish the specification directly from Hero.
88
88
 
89
89
  ```ruby
90
- puts Hero::Formula[:gather_news].publish
90
+ puts Hero::Formula[:gather_news].to_s
91
91
 
92
92
  # => gather_news
93
93
  # 1. hacker_news
@@ -163,10 +163,10 @@ We also need an initializer to set the formula up.
163
163
 
164
164
  ```ruby
165
165
  # app/initializer.rb
166
- Hero::Formula[:gather_news].add_step GatherNews::HackerNews.new
167
- Hero::Formula[:gather_news].add_step GatherNews::Reddit.new
168
- Hero::Formula[:gather_news].add_step GatherNews::Google.new
169
- Hero::Formula[:gather_news].add_step GatherNews::Email.new
166
+ Hero::Formula[:gather_news].add_step :hacker_news, GatherNews::HackerNews.new
167
+ Hero::Formula[:gather_news].add_step :reddit, GatherNews::Reddit.new
168
+ Hero::Formula[:gather_news].add_step :google, GatherNews::Google.new
169
+ Hero::Formula[:gather_news].add_step :email, GatherNews::Email.new
170
170
  ```
171
171
 
172
172
  Now we have a well structured application that is ready to grow.
@@ -177,4 +177,31 @@ This is an important point and a powerful concept.
177
177
 
178
178
  ## Deep Cuts
179
179
 
180
- Advanced usage coming soon...
180
+ Logging comes for free if you register a logger with Hero. Here's an example.
181
+
182
+ ```ruby
183
+ Hero.logger = Logger.new(STDOUT)
184
+
185
+ Hero::Formula[:log_example].add_step :first_step do |context, options|
186
+ context << 1
187
+ end
188
+
189
+ Hero::Formula[:log_example].add_step :second_step do |context, options|
190
+ context << 2
191
+ end
192
+
193
+ Hero::Formula[:log_example].add_step :third_step do |context, options|
194
+ context << 3
195
+ end
196
+
197
+ Hero::Formula[:log_example].run([])
198
+
199
+ # The log will now contain the following lines.
200
+ #
201
+ # I, [2012-08-26T11:37:22.267072 #76676] INFO -- : HERO before log_example -> first_step Context: [] Options: {}
202
+ # I, [2012-08-26T11:37:22.267166 #76676] INFO -- : HERO after log_example -> first_step Context: [1] Options: {}
203
+ # I, [2012-08-26T11:37:22.267211 #76676] INFO -- : HERO before log_example -> second_step Context: [1] Options: {}
204
+ # I, [2012-08-26T11:37:22.267248 #76676] INFO -- : HERO after log_example -> second_step Context: [1, 2] Options: {}
205
+ # I, [2012-08-26T11:37:22.267282 #76676] INFO -- : HERO before log_example -> third_step Context: [1, 2] Options: {}
206
+ # I, [2012-08-26T11:37:22.267333 #76676] INFO -- : HERO after log_example -> third_step Context: [1, 2, 3] Options: {}
207
+ ```
data/lib/hero/observer.rb CHANGED
@@ -59,13 +59,25 @@ module Hero
59
59
  # @param optional [Hash] options An option Hash to be passed to each step.
60
60
  def update(context=nil, options={})
61
61
  steps.each do |step|
62
- if Hero.logger
63
- Hero.logger.info "HERO Formula: #{formula_name}, Step: #{step.first}, Context: #{context.inspect}, Options: #{options.inspect}"
64
- end
62
+ log_step(:before, step, context, options)
65
63
  step.last.call(context, options)
64
+ log_step(:after, step, context, options)
66
65
  end
67
66
  end
68
67
 
68
+ private
69
+
70
+ # Logs a step to the registered Hero.logger.
71
+ # @note Users info for the log level.
72
+ # @param [Symbol, String] id The identifier for the step. [:before, :after]
73
+ # @param [Object] step
74
+ # @param [Object] context
75
+ # @param [Object] options
76
+ def log_step(id, step, context, options)
77
+ return unless Hero.logger
78
+ Hero.logger.info "HERO #{id.to_s.ljust(6)} #{formula_name} -> #{step.first} Context: #{context.inspect} Options: #{options.inspect}"
79
+ end
80
+
69
81
  end
70
82
  end
71
83
 
data/spec/formula_spec.rb CHANGED
@@ -124,12 +124,16 @@ describe Hero::Formula do
124
124
  end
125
125
  end
126
126
  Hero.logger = TestLogger.new
127
- Hero::Formula[:test_formula].add_step(:one) {}
128
- Hero::Formula[:test_formula].add_step(:two) {}
129
- Hero::Formula[:test_formula].run(:example, :logging => true)
130
- assert_equal Hero.logger.buffer.length, 2
131
- assert_equal "HERO Formula: test_formula, Step: one, Context: :example, Options: {:logging=>true}", Hero.logger.buffer.first
132
- assert_equal "HERO Formula: test_formula, Step: two, Context: :example, Options: {:logging=>true}", Hero.logger.buffer.last
127
+
128
+ Hero::Formula[:test_formula].add_step(:one) { |list, opts| list << 1; opts[:step] = 1 }
129
+ Hero::Formula[:test_formula].add_step(:two) { |list, opts| list << 2; opts[:step] = 2 }
130
+ list = []
131
+ Hero::Formula[:test_formula].run(list, {})
132
+ assert_equal Hero.logger.buffer.length, 4
133
+ assert_equal "HERO before test_formula -> one Context: [] Options: {}", Hero.logger.buffer[0]
134
+ assert_equal "HERO after test_formula -> one Context: [1] Options: {:step=>1}", Hero.logger.buffer[1]
135
+ assert_equal "HERO before test_formula -> two Context: [1] Options: {:step=>1}", Hero.logger.buffer[2]
136
+ assert_equal "HERO after test_formula -> two Context: [1, 2] Options: {:step=>2}", Hero.logger.buffer[3]
133
137
  end
134
138
 
135
139
  end
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.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: