smack 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 ggironda
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = smack
2
+
3
+ Hi. This is dependency injection for the rest of us. See smack_test.rb for examples.
4
+
5
+ == FAQ
6
+
7
+ Q: COOL IS THIS LIKE ONE OF THOSE JAVA DI FRAMEWORKS?
8
+ A: No. This is an entire 15 lines of dumb code.
9
+
10
+ Q: WHY ARE YOU DOING DI IN RUBY ARE YOU A MANIAC?
11
+ A: DI is a fine pattern and is sometimes necessary. It's just simpler to do in Ruby, hence the 15 lines of code. This just gives you nice syntax for it.
12
+
13
+ Q: WHY ARE ALL YOUR PROJECT NAMES SO TERRIBLE?
14
+ A: Marketing isn't really my strong point.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 ggironda. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "smack"
8
+ gem.summary = %Q{DI for dummies}
9
+ gem.description = %Q{Needle is too complex. Here's DI for dummies.}
10
+ gem.email = "gabriel.gironda@gmail.com"
11
+ gem.homepage = "http://github.com/gabrielg/smack"
12
+ gem.authors = ["ggironda"]
13
+ gem.add_development_dependency "riot"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "smack #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,15 @@
1
+ module Smack
2
+ def self.init; Object.instance_eval { include Smack }; end
3
+
4
+ def smack_inject(substitutions)
5
+ (class << self; self; end).instance_eval { @smack_subs = (@smack_subs || {}).merge!(substitutions) }
6
+ end
7
+
8
+ def smack_get(klass_or_mod)
9
+ [self, *(class << self; self; end).ancestors].any? { |mod| (found_mod = mod.smack_fetch(klass_or_mod)) ? (break(found_mod)) : nil } || klass_or_mod
10
+ end
11
+
12
+ def smack_fetch(klass_or_mod)
13
+ (class << self; self; end).instance_eval { @smack_subs ? @smack_subs[klass_or_mod] : nil }
14
+ end
15
+ end # Smack
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{smack}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["ggironda"]
12
+ s.date = %q{2009-10-08}
13
+ s.description = %q{Needle is too complex. Here's DI for dummies.}
14
+ s.email = %q{gabriel.gironda@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/smack.rb",
27
+ "smack.gemspec",
28
+ "test/smack_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/gabrielg/smack}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{DI for dummies}
36
+ s.test_files = [
37
+ "test/smack_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<riot>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<riot>, [">= 0"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<riot>, [">= 0"])
52
+ end
53
+ end
@@ -0,0 +1,188 @@
1
+ require 'test_helper'
2
+ Smack.init
3
+
4
+ class Useless
5
+ def write_a_song
6
+ smack_get(Junkie).new.write_a_song
7
+ end
8
+ end
9
+
10
+ class Junkie
11
+
12
+ def write_a_song
13
+ "i'm a regular junkie, not a talented one"
14
+ end
15
+
16
+ end
17
+
18
+ class LayneStaley < Junkie
19
+
20
+ def write_a_song
21
+ "THEM BONES"
22
+ end
23
+
24
+ end
25
+
26
+ class SidVicious < Junkie
27
+
28
+ def write_a_song
29
+ "i'm a terrible bass player, i can't write songs"
30
+ end
31
+
32
+ end
33
+
34
+ class AlJourgensen < Junkie
35
+
36
+ def write_a_song
37
+ "i'm a burnout who writes terrible thrash metal now"
38
+ end
39
+
40
+ end
41
+
42
+ class Band
43
+ end
44
+
45
+ class AliceInChains < Band
46
+ end
47
+
48
+ module BandManager
49
+ smack_inject Junkie => LayneStaley
50
+
51
+ def get_a_song
52
+ smack_get(Junkie).new.write_a_song
53
+ end
54
+
55
+ end
56
+
57
+ module RecordCompany
58
+ include BandManager
59
+ smack_inject Junkie => SidVicious
60
+ end
61
+
62
+ module CreativeConglomerate
63
+ include RecordCompany
64
+ smack_inject Band => AliceInChains
65
+
66
+ def get_a_band
67
+ smack_get(Band).new
68
+ end
69
+
70
+ def sell_a_song
71
+ "BUY BUY BUY " + smack_get(Junkie).new.write_a_song
72
+ end
73
+ end
74
+
75
+ class Interscope
76
+ include RecordCompany
77
+ end
78
+
79
+ class InterscopeSubsidiary < Interscope
80
+ end
81
+
82
+ class AnotherInterscopeSubsidiary < Interscope
83
+ smack_inject Junkie => LayneStaley
84
+ end
85
+
86
+ context "Smack" do
87
+
88
+ context "a module with a Smack substition" do
89
+ setup do
90
+ Object.new.extend(BandManager)
91
+ end
92
+
93
+ should "use the substituted in class" do
94
+ topic.get_a_song
95
+ end.equals("THEM BONES")
96
+ end # a module with a Smack substition
97
+
98
+ context "a module including another module with a Smack substitution and overriding it" do
99
+ setup do
100
+ Object.new.extend(RecordCompany)
101
+ end
102
+
103
+ should "use the override" do
104
+ topic.get_a_song
105
+ end.equals("i'm a terrible bass player, i can't write songs")
106
+
107
+ should "not stomp on the included module's substitutions" do
108
+ Object.new.extend(BandManager).get_a_song
109
+ end.equals("THEM BONES")
110
+ end # a module including another module with a Smack substitution and overriding it
111
+
112
+ context "an instance of an object with its own Smack injections" do
113
+
114
+ setup do
115
+ obj = Object.new.extend(BandManager)
116
+ obj.smack_inject(Junkie => AlJourgensen)
117
+ obj
118
+ end
119
+
120
+ should "use the subbed in Smack injection for that instance" do
121
+ topic.get_a_song
122
+ end.equals("i'm a burnout who writes terrible thrash metal now")
123
+
124
+ should "not stomp on the original substitution" do
125
+ Object.new.extend(BandManager).get_a_song
126
+ end.equals("THEM BONES")
127
+
128
+ end # an instance of an object with its own Smack injections
129
+
130
+ context "a class including a module with a Smack injection" do
131
+ setup do
132
+ Interscope.new
133
+ end
134
+
135
+ should "use the smack injection of the module" do
136
+ topic.get_a_song
137
+ end.equals("i'm a terrible bass player, i can't write songs")
138
+ end # a class including a module with a Smack injection
139
+
140
+ context "a class extending a class that includes a module with a Smack injection" do
141
+ setup do
142
+ InterscopeSubsidiary.new
143
+ end
144
+
145
+ should "use the smack injection of the parent class" do
146
+ topic.get_a_song
147
+ end.equals("i'm a terrible bass player, i can't write songs")
148
+ end # a class extending a class that includes a module with a Smack injection
149
+
150
+ context "a class extending a class that includes a module with a Smack injection and providing its own injection" do
151
+ setup do
152
+ AnotherInterscopeSubsidiary.new
153
+ end
154
+
155
+ should "use its own injection" do
156
+ topic.get_a_song
157
+ end.equals("THEM BONES")
158
+
159
+ should "not stomp on other injections" do
160
+ InterscopeSubsidiary.new.get_a_song
161
+ end.equals("i'm a terrible bass player, i can't write songs")
162
+ end # a class extending a class that includes a module with a Smack injection and providing its own injection
163
+
164
+ context "a module including another module and providing extra substitutions" do
165
+ setup do
166
+ Object.new.extend(CreativeConglomerate)
167
+ end
168
+
169
+ should "use the given substitution" do
170
+ topic.get_a_band.class == AliceInChains
171
+ end
172
+
173
+ should "use inherited substitutions" do
174
+ topic.sell_a_song
175
+ end.equals("BUY BUY BUY i'm a terrible bass player, i can't write songs")
176
+
177
+ end # a module including another module and providing extra substitutions
178
+
179
+ context "a class with no substitutions" do
180
+ setup do
181
+ Useless.new
182
+ end
183
+
184
+ should "just return the value given to smack_get" do
185
+ topic.write_a_song
186
+ end.equals("i'm a regular junkie, not a talented one")
187
+ end # a class with no substitutions
188
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'riot'
3
+ require 'pathname'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'smack'
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smack
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - ggironda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-08 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: riot
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Needle is too complex. Here's DI for dummies.
26
+ email: gabriel.gironda@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/smack.rb
42
+ - smack.gemspec
43
+ - test/smack_test.rb
44
+ - test/test_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/gabrielg/smack
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: DI for dummies
73
+ test_files:
74
+ - test/smack_test.rb
75
+ - test/test_helper.rb