Class: Hero::Formula
- Inherits:
-
Object
- Object
- Hero::Formula
- Extended by:
- Forwardable
- Includes:
- Observable, Singleton
- Defined in:
- lib/hero/formula.rb
Overview
Represents a business process that can be easily modeled and implemented.
The idea is to encourage implementations that more closely resemble business requirements in order to reduce the dissonance that is typical between business nomenclature and actual implementation.
Additional benefits include:
-
Composable units of code which support changing requirements
-
Testable components
-
Simplified implementation
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The name of this formula.
-
#observer ⇒ Object
readonly
The observer attached to this formula.
Class Method Summary collapse
-
.[](name) ⇒ Object
Returns the named formula.
-
.print ⇒ Object
Prints/puts a string representation of all registered formulas.
-
.register(name) ⇒ Object
Registers a formula an prepares it to receive steps.
-
.reset ⇒ Object
Removes all registered formulas.
Instance Method Summary collapse
-
#notify(context = nil, options = {}) ⇒ Object
(also: #run)
Observable notify implementation.
-
#print ⇒ Object
Prints/puts a String representation of the formula.
Instance Attribute Details
#name ⇒ Object (readonly)
The name of this formula.
97 98 99 |
# File 'lib/hero/formula.rb', line 97 def name @name end |
#observer ⇒ Object (readonly)
The observer attached to this formula.
100 101 102 |
# File 'lib/hero/formula.rb', line 100 def observer @observer end |
Class Method Details
.[](name) ⇒ Object
Implicitly registers the formula if it has not already been registered.
Returns the named formula.
58 59 60 |
# File 'lib/hero/formula.rb', line 58 def [](name) formulas[name] ||= register(name) end |
.print ⇒ Object
Prints/puts a string representation of all registered formulas.
44 45 46 |
# File 'lib/hero/formula.rb', line 44 def print formulas.values.each(&:print) end |
.register(name) ⇒ Object
Registers a formula an prepares it to receive steps.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/hero/formula.rb', line 65 def register(name) observer = Hero::Observer.new(name) formula_class_name = name.to_s.strip.gsub(/\s/, "_").gsub(/[^a-z_]/i, "").split(/_/).map(&:capitalize).join formula_class_name = "HeroFormula#{formula_class_name}" formula_class = Class.new(Hero::Formula) if Object.const_defined?(formula_class_name) Object.send(:remove_const, formula_class_name) end Object.const_set(formula_class_name, formula_class) formula = formula_class.instance formula.add_observer(observer) formula.instance_eval do @name = name @observer = observer end formulas[name] = formula end |
.reset ⇒ Object
Removes all registered formulas.
49 50 51 52 |
# File 'lib/hero/formula.rb', line 49 def reset formulas.values.each { |f| f.delete_observers } @formulas = {} end |
Instance Method Details
#notify(context = nil, options = {}) ⇒ Object Also known as: run
Observable notify implementation. Invokes #update on all observers.
137 138 139 140 |
# File 'lib/hero/formula.rb', line 137 def notify(context=nil, ={}) changed notify_observers(context, ) end |
#print ⇒ Object
Prints/puts a String representation of the formula.
150 151 152 153 154 155 156 |
# File 'lib/hero/formula.rb', line 150 def print value = [name] steps.each_with_index do |step, index| value << "#{(index + 1).to_s.rjust(3)}. #{step.first}" end puts value.join("\n") end |