hero 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/Gemfile.lock +31 -0
- data/lib/hero.rb +45 -9
- data/lib/hero_observer.rb +8 -36
- data/spec/hero_spec.rb +54 -0
- metadata +27 -39
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
coderay (1.0.7)
|
5
|
+
diff-lcs (1.1.3)
|
6
|
+
grumpy_old_man (0.1.2)
|
7
|
+
method_source (0.8)
|
8
|
+
pry (0.9.10)
|
9
|
+
coderay (~> 1.0.5)
|
10
|
+
method_source (~> 0.8)
|
11
|
+
slop (~> 3.3.1)
|
12
|
+
pry_debug (0.0.1)
|
13
|
+
pry (~> 0.9.0)
|
14
|
+
rspec (2.11.0)
|
15
|
+
rspec-core (~> 2.11.0)
|
16
|
+
rspec-expectations (~> 2.11.0)
|
17
|
+
rspec-mocks (~> 2.11.0)
|
18
|
+
rspec-core (2.11.1)
|
19
|
+
rspec-expectations (2.11.2)
|
20
|
+
diff-lcs (~> 1.1.3)
|
21
|
+
rspec-mocks (2.11.2)
|
22
|
+
slop (3.3.2)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
grumpy_old_man
|
29
|
+
pry
|
30
|
+
pry_debug
|
31
|
+
rspec
|
data/lib/hero.rb
CHANGED
@@ -1,16 +1,52 @@
|
|
1
1
|
require 'observer'
|
2
2
|
require 'singleton'
|
3
|
+
require 'forwardable'
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
module Hero
|
6
|
+
class Formula
|
7
|
+
include Observable
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
class << self
|
11
|
+
extend Forwardable
|
12
|
+
def_delegator :formulas, :each, :each
|
13
|
+
def_delegator :formulas, :length, :count
|
14
|
+
|
15
|
+
def reset
|
16
|
+
@formulas = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def [](name)
|
20
|
+
formulas[name]
|
21
|
+
end
|
22
|
+
|
23
|
+
def register(name)
|
24
|
+
observer = Hero::Observer.new
|
25
|
+
formula = Class.new(Hero::Formula).instance
|
26
|
+
formula.add_observer(observer)
|
27
|
+
formula.instance_eval { @observer = observer }
|
28
|
+
formulas[name] = formula
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def formulas
|
34
|
+
@formulas ||= {}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_step(step=nil, &block)
|
39
|
+
step ||= block if block_given?
|
40
|
+
@observer.steps << step
|
41
|
+
end
|
42
|
+
|
43
|
+
def notify(target=nil, options={})
|
44
|
+
changed
|
45
|
+
notify_observers(target, options)
|
46
|
+
end
|
47
|
+
|
48
|
+
alias :run :notify
|
7
49
|
|
8
|
-
def notify(event_name, target=nil, options={})
|
9
|
-
changed
|
10
|
-
notify_observers(event_name, target=nil, options)
|
11
50
|
end
|
12
51
|
end
|
13
52
|
|
14
|
-
InitUser.instance.notify(:before_init, {:name => "foo"})
|
15
|
-
InitUser.instance.notify(:init, {:name => "foo"})
|
16
|
-
InitUser.instance.notify(:save, {:name => "foo"})
|
data/lib/hero_observer.rb
CHANGED
@@ -1,41 +1,13 @@
|
|
1
|
-
|
1
|
+
module Hero
|
2
|
+
class Observer
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# events = {
|
7
|
-
# :create_user => [
|
8
|
-
# FixData,
|
9
|
-
# Validate,
|
10
|
-
# etc...
|
11
|
-
# ],
|
12
|
-
# :checkout => [
|
13
|
-
# Verify,
|
14
|
-
# Charge,
|
15
|
-
# etc...
|
16
|
-
# ]
|
17
|
-
# }
|
18
|
-
#
|
19
|
-
# HeroObserver.new(hero, events)
|
20
|
-
# @param [Hero] hero The Hero object to watch.
|
21
|
-
# @param [Hash] handlers A Hash containing event names and the handlers to run at each event.
|
22
|
-
def initialize(hero, handlers)
|
23
|
-
@handlers = handlers
|
24
|
-
hero.instance.add_observer(self)
|
25
|
-
end
|
4
|
+
def steps
|
5
|
+
@steps ||= []
|
6
|
+
end
|
26
7
|
|
27
|
-
|
28
|
-
|
29
|
-
# @param [String] event_name The name of the event.
|
30
|
-
# @param [Object] target The target of the event.
|
31
|
-
# @param [Hash] options An options Hash that gets forwarded to the rules.
|
32
|
-
def update(event_name, target, options={})
|
33
|
-
handler = @handlers[event_name] || []
|
34
|
-
handler.each do |handler|
|
35
|
-
handler_name = handler.name
|
36
|
-
handler.call(target, options)
|
8
|
+
def update(target, options={})
|
9
|
+
steps.each { |step| step.call(target, options) }
|
37
10
|
end
|
38
|
-
end
|
39
11
|
|
12
|
+
end
|
40
13
|
end
|
41
|
-
|
data/spec/hero_spec.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "pry"
|
2
|
+
require "grumpy_old_man"
|
3
|
+
Dir[File.join(File.dirname(__FILE__), "..", "lib", "*.rb")].each do |file|
|
4
|
+
require file
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Hero::Formula do
|
8
|
+
include GrumpyOldMan
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
Hero::Formula.reset
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should support reset" do
|
15
|
+
Hero::Formula.register(:test_formula)
|
16
|
+
assert_equal Hero::Formula.count, 1
|
17
|
+
Hero::Formula.reset
|
18
|
+
assert_equal Hero::Formula.count, 0
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should support registering a formula" do
|
22
|
+
Hero::Formula.register(:test_formula)
|
23
|
+
assert_equal Hero::Formula.count, 1
|
24
|
+
assert Hero::Formula[:test_formula].is_a? Hero::Formula
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should support registering N number of formulas" do
|
28
|
+
10.times { |i| Hero::Formula.register("example_#{i}") }
|
29
|
+
assert_equal Hero::Formula.count, 10
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "a registered formula" do
|
33
|
+
it "should support adding steps" do
|
34
|
+
Hero::Formula.register(:test_formula)
|
35
|
+
Hero::Formula[:test_formula].add_step { }
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should support notify" do
|
39
|
+
Hero::Formula.register(:test_formula)
|
40
|
+
step_ran = false
|
41
|
+
target = Object.new
|
42
|
+
Hero::Formula[:test_formula].add_step do |t, opts|
|
43
|
+
assert_equal t, target
|
44
|
+
assert_equal opts[:foo], :bar
|
45
|
+
step_ran = true
|
46
|
+
end
|
47
|
+
Hero::Formula[:test_formula].notify(target, :foo => :bar)
|
48
|
+
assert step_ran
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
metadata
CHANGED
@@ -1,65 +1,53 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: hero
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
version: 0.0.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Nathan Hopkins
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2012-08-20 00:00:00 -06:00
|
18
|
-
default_executable:
|
12
|
+
date: 2012-08-22 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
14
|
+
description: ! ' Simplify your apps with Hero.
|
20
15
|
|
21
|
-
|
22
|
-
email:
|
16
|
+
'
|
17
|
+
email:
|
23
18
|
- natehop@gmail.com
|
24
19
|
executables: []
|
25
|
-
|
26
20
|
extensions: []
|
27
|
-
|
28
21
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
22
|
+
files:
|
31
23
|
- lib/hero.rb
|
32
24
|
- lib/hero_observer.rb
|
33
25
|
- Gemfile
|
34
|
-
|
26
|
+
- Gemfile.lock
|
27
|
+
- spec/hero_spec.rb
|
35
28
|
homepage: http://hopsoft.github.com/hero/
|
36
|
-
licenses:
|
29
|
+
licenses:
|
37
30
|
- MIT
|
38
31
|
post_install_message:
|
39
32
|
rdoc_options: []
|
40
|
-
|
41
|
-
require_paths:
|
33
|
+
require_paths:
|
42
34
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
55
|
-
- 0
|
56
|
-
version: "0"
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
57
47
|
requirements: []
|
58
|
-
|
59
48
|
rubyforge_project:
|
60
|
-
rubygems_version: 1.
|
49
|
+
rubygems_version: 1.8.24
|
61
50
|
signing_key:
|
62
51
|
specification_version: 3
|
63
52
|
summary: Business process modeling for the Rubyist
|
64
53
|
test_files: []
|
65
|
-
|