hollywood 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/Gemfile.lock +31 -0
- data/Rakefile +118 -0
- data/hollywood.gemspec +29 -0
- data/lib/hollywood.rb +21 -0
- data/lib/hollywood/version.rb +3 -0
- data/spec/hollywood_spec.rb +63 -0
- data/spec/spec_helper.rb +40 -0
- metadata +77 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hollywood (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
fuubar (0.0.5)
|
11
|
+
rspec (~> 2.0)
|
12
|
+
rspec-instafail (~> 0.1.4)
|
13
|
+
ruby-progressbar (~> 0.0.10)
|
14
|
+
rspec (2.6.0)
|
15
|
+
rspec-core (~> 2.6.0)
|
16
|
+
rspec-expectations (~> 2.6.0)
|
17
|
+
rspec-mocks (~> 2.6.0)
|
18
|
+
rspec-core (2.6.4)
|
19
|
+
rspec-expectations (2.6.0)
|
20
|
+
diff-lcs (~> 1.1.2)
|
21
|
+
rspec-instafail (0.1.8)
|
22
|
+
rspec-mocks (2.6.0)
|
23
|
+
ruby-progressbar (0.0.10)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
fuubar
|
30
|
+
hollywood!
|
31
|
+
rspec
|
data/Rakefile
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
#############################################################################
|
5
|
+
#
|
6
|
+
# Helper functions
|
7
|
+
#
|
8
|
+
#############################################################################
|
9
|
+
|
10
|
+
def name
|
11
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
12
|
+
end
|
13
|
+
|
14
|
+
def version
|
15
|
+
line = File.read("lib/#{name}/version.rb")[/^\s*VERSION\s*=\s*.*/]
|
16
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
17
|
+
end
|
18
|
+
|
19
|
+
def date
|
20
|
+
Date.today.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def rubyforge_project
|
24
|
+
name
|
25
|
+
end
|
26
|
+
|
27
|
+
def gemspec_file
|
28
|
+
"#{name}.gemspec"
|
29
|
+
end
|
30
|
+
|
31
|
+
def gem_file
|
32
|
+
"#{name}-#{version}.gem"
|
33
|
+
end
|
34
|
+
|
35
|
+
def replace_header(head, header_name)
|
36
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
37
|
+
end
|
38
|
+
|
39
|
+
#############################################################################
|
40
|
+
#
|
41
|
+
# Custom tasks
|
42
|
+
#
|
43
|
+
#############################################################################
|
44
|
+
|
45
|
+
default_rspec_opts = %w[--colour --format Fuubar]
|
46
|
+
|
47
|
+
desc "Run all examples"
|
48
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
49
|
+
t.rspec_opts = default_rspec_opts
|
50
|
+
end
|
51
|
+
|
52
|
+
#############################################################################
|
53
|
+
#
|
54
|
+
# Packaging tasks
|
55
|
+
#
|
56
|
+
#############################################################################
|
57
|
+
|
58
|
+
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
|
59
|
+
task :release => :build do
|
60
|
+
unless `git branch` =~ /^\* master$/
|
61
|
+
puts "You must be on the master branch to release!"
|
62
|
+
exit!
|
63
|
+
end
|
64
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
65
|
+
sh "git tag v#{version}"
|
66
|
+
sh "git push origin master"
|
67
|
+
sh "git push origin v#{version}"
|
68
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Build #{gem_file} into the pkg directory"
|
72
|
+
task :build => :gemspec do
|
73
|
+
sh "mkdir -p pkg"
|
74
|
+
sh "gem build #{gemspec_file}"
|
75
|
+
sh "mv #{gem_file} pkg"
|
76
|
+
end
|
77
|
+
|
78
|
+
desc "Generate #{gemspec_file}"
|
79
|
+
task :gemspec => :validate do
|
80
|
+
# read spec file and split out manifest section
|
81
|
+
spec = File.read(gemspec_file)
|
82
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
83
|
+
|
84
|
+
# replace name version and date
|
85
|
+
replace_header(head, :name)
|
86
|
+
replace_header(head, :version)
|
87
|
+
replace_header(head, :date)
|
88
|
+
#comment this out if your rubyforge_project has a different name
|
89
|
+
#replace_header(head, :rubyforge_project)
|
90
|
+
|
91
|
+
# determine file list from git ls-files
|
92
|
+
files = `git ls-files`.
|
93
|
+
split("\n").
|
94
|
+
sort.
|
95
|
+
reject { |file| file =~ /^\./ }.
|
96
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
97
|
+
map { |file| " #{file}" }.
|
98
|
+
join("\n")
|
99
|
+
|
100
|
+
# piece file back together and write
|
101
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
102
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
103
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
104
|
+
puts "Updated #{gemspec_file}"
|
105
|
+
end
|
106
|
+
|
107
|
+
desc "Validate #{gemspec_file}"
|
108
|
+
task :validate do
|
109
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
110
|
+
unless libfiles.empty?
|
111
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
112
|
+
exit!
|
113
|
+
end
|
114
|
+
unless Dir['VERSION*'].empty?
|
115
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
116
|
+
exit!
|
117
|
+
end
|
118
|
+
end
|
data/hollywood.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'hollywood'
|
3
|
+
s.version = '1.0.0'
|
4
|
+
s.date = '2011-09-15'
|
5
|
+
s.platform = Gem::Platform::RUBY
|
6
|
+
s.authors = ["Lee Henson"]
|
7
|
+
s.email = ["lee.m.henson@gmail.com"]
|
8
|
+
s.summary = %q{Don't call us, we'll call you.}
|
9
|
+
s.description = %q{Simple pub/sub callback mixin.}
|
10
|
+
s.homepage = 'http://github.com/leemhenson/hollywood'
|
11
|
+
# = MANIFEST =
|
12
|
+
s.files = %w[
|
13
|
+
Gemfile
|
14
|
+
Gemfile.lock
|
15
|
+
Rakefile
|
16
|
+
hollywood.gemspec
|
17
|
+
lib/hollywood.rb
|
18
|
+
lib/hollywood/version.rb
|
19
|
+
spec/hollywood_spec.rb
|
20
|
+
spec/spec_helper.rb
|
21
|
+
]
|
22
|
+
# = MANIFEST =
|
23
|
+
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
|
27
|
+
s.add_development_dependency "rspec"
|
28
|
+
s.add_development_dependency "fuubar"
|
29
|
+
end
|
data/lib/hollywood.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Hollywood
|
2
|
+
def when(event, &block)
|
3
|
+
event = { event => block } unless event.is_a? Hash
|
4
|
+
new_callbacks = event.map { |event, callback| { :event => event, :callback => callback } }
|
5
|
+
new_callbacks.each { |s| get_callbacks(s[:event]) << s[:callback] }
|
6
|
+
|
7
|
+
self
|
8
|
+
end
|
9
|
+
|
10
|
+
def callback(*args)
|
11
|
+
get_callbacks(args.shift.to_sym).each { |callback| callback.call *(args.take callback.arity) }
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def get_callbacks event
|
18
|
+
@callbacks ||= {}
|
19
|
+
@callbacks[event] ||= []
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Hollywood do
|
4
|
+
let(:actor) { Actor.new }
|
5
|
+
let(:agent) { Agent.new }
|
6
|
+
let(:director) { Director.new }
|
7
|
+
|
8
|
+
subject { actor }
|
9
|
+
|
10
|
+
context "no subscriptions" do
|
11
|
+
it { should_not be_in_a_movie }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "callback subscription" do
|
15
|
+
before do
|
16
|
+
agent.when(:you_got_the_part) do |fee, character|
|
17
|
+
actor.deposit fee
|
18
|
+
actor.learn_lines character
|
19
|
+
end
|
20
|
+
|
21
|
+
director.send_contract_to agent
|
22
|
+
end
|
23
|
+
|
24
|
+
it { should have_learnt_its_lines }
|
25
|
+
its(:character) { should == :deckard }
|
26
|
+
its(:money_in_the_bank) { should == :ninty_percent }
|
27
|
+
end
|
28
|
+
|
29
|
+
context "multiple callbacks" do
|
30
|
+
let(:order) { [] }
|
31
|
+
|
32
|
+
before do
|
33
|
+
agent.when(:you_got_the_part) do |fee, character|
|
34
|
+
order << 1
|
35
|
+
end
|
36
|
+
|
37
|
+
# alternative syntax
|
38
|
+
agent.when(:you_got_the_part => ->(fee, character) { order << 2 })
|
39
|
+
|
40
|
+
director.send_contract_to agent
|
41
|
+
end
|
42
|
+
|
43
|
+
subject { order }
|
44
|
+
|
45
|
+
it { should == [1, 2] }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "callbacks with varying numbers of args" do
|
49
|
+
let(:args_supplied) { [] }
|
50
|
+
|
51
|
+
before do
|
52
|
+
agent.when(:you_got_the_part) { |fee| args_supplied << [fee] }
|
53
|
+
agent.when(:you_got_the_part) { |fee, character| args_supplied << [fee, character] }
|
54
|
+
agent.when(:you_got_the_part) { |fee, character, not_matched| args_supplied << [fee, character, not_matched] }
|
55
|
+
|
56
|
+
director.send_contract_to agent
|
57
|
+
end
|
58
|
+
|
59
|
+
subject { args_supplied }
|
60
|
+
|
61
|
+
it { should == [[:ninty_percent], [:ninty_percent, :deckard], [:ninty_percent, :deckard, nil]] }
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'hollywood'
|
2
|
+
|
3
|
+
class Actor
|
4
|
+
def in_a_movie?
|
5
|
+
false
|
6
|
+
end
|
7
|
+
|
8
|
+
def learnt_lines?
|
9
|
+
@learn_lines || false
|
10
|
+
end
|
11
|
+
|
12
|
+
def learn_lines character
|
13
|
+
@learn_lines = true
|
14
|
+
@character = character
|
15
|
+
end
|
16
|
+
|
17
|
+
def deposit fee
|
18
|
+
@money_in_the_bank = fee
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :character, :money_in_the_bank
|
22
|
+
end
|
23
|
+
|
24
|
+
class Agent
|
25
|
+
include Hollywood
|
26
|
+
|
27
|
+
def take_10_percent
|
28
|
+
callback :you_got_the_part, :ninty_percent, :deckard
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Director
|
33
|
+
def send_contract_to agent
|
34
|
+
agent.take_10_percent
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec::Matchers.define :have_learnt_its_lines do
|
39
|
+
match { |actor| actor.learnt_lines? }
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hollywood
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lee Henson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-15 00:00:00.000000000 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &76525240 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *76525240
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: fuubar
|
28
|
+
requirement: &76525020 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *76525020
|
37
|
+
description: Simple pub/sub callback mixin.
|
38
|
+
email:
|
39
|
+
- lee.m.henson@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- Gemfile
|
45
|
+
- Gemfile.lock
|
46
|
+
- Rakefile
|
47
|
+
- hollywood.gemspec
|
48
|
+
- lib/hollywood.rb
|
49
|
+
- lib/hollywood/version.rb
|
50
|
+
- spec/hollywood_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/leemhenson/hollywood
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.6.2
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Don't call us, we'll call you.
|
77
|
+
test_files: []
|