interchangeable 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/README.md +41 -1
- data/lib/interchangeable/version.rb +1 -1
- data/lib/interchangeable.rb +5 -2
- data/spec/interchangeable_spec.rb +42 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac7c4148785998ed665d7ca26b07171dbbc83aac
|
4
|
+
data.tar.gz: d26b48301c864f75206661fe235f00c96a7482f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0da66a26a9c9b051cf21fb9566200fe5c54b09d66377977e31b3d5f20671ef78886b430c927884cdf00a571ad5843cffd5725981957d5b2559be8b8eff638ea2
|
7
|
+
data.tar.gz: ab5c714337289c4f5a03184991dfc264b30878993913670e6b00539734ba51dd8c9bd139f5d3940e307fee53482cffae9843c15edba091bf031a4679a065b034
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@ can become
|
|
16
16
|
|
17
17
|
```ruby
|
18
18
|
class MyApi
|
19
|
-
|
19
|
+
interchangeable_method :api_key
|
20
20
|
end
|
21
21
|
```
|
22
22
|
|
@@ -24,8 +24,48 @@ Elsewhere in your application, you can define the method like so:
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
Interchangeable.define MyApi, :api_key { 'anything' }
|
27
|
+
|
28
|
+
MyApi.new.api_key # 'anything'
|
29
|
+
|
27
30
|
```
|
28
31
|
|
32
|
+
You can also provide a default implementation that can be overridden elsewhere:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class MyApi
|
36
|
+
interchangeable_method(:api_key) { 'abc' }
|
37
|
+
end
|
38
|
+
|
39
|
+
MyApi.new.api_key # 'abc'
|
40
|
+
```
|
41
|
+
|
42
|
+
**But why bother doing this?**
|
43
|
+
|
44
|
+
Interchangeable will provide you a list of the methods you have defined, as well as some helpful information.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class MyApi
|
48
|
+
interchangeable_describe "this is an apple"
|
49
|
+
interchangeable_method :apple
|
50
|
+
|
51
|
+
interchangeable_describe "this is an orange"
|
52
|
+
interchangeable_method(:orange) { 'orange' }
|
53
|
+
|
54
|
+
interchangeable_describe "this is a banana"
|
55
|
+
interchangeable_method :banana
|
56
|
+
end
|
57
|
+
|
58
|
+
Interchangeable.define(MyApi, :banana) { 'banana' }
|
59
|
+
|
60
|
+
|
61
|
+
Interchangeable.methods # [<target=MyApi, method_name=:apple, implemented=false, default=false, description="this is an apple">,
|
62
|
+
# <target=MyApi, method_name=:orange, implemented=true, default=true, description="this is an orange">,
|
63
|
+
# <target=MyApi, method_name=:banana, implemented=true, default=false, description="this is a banana">]
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
This information can be helpful if you have a reusable system with many interchangeable parts.
|
68
|
+
|
29
69
|
## Installation
|
30
70
|
|
31
71
|
Add this line to your application's Gemfile:
|
data/lib/interchangeable.rb
CHANGED
@@ -29,10 +29,13 @@ module Interchangeable
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def define the_class, method_name, &block
|
32
|
-
the_class.
|
32
|
+
entry = Interchangeable.methods.select { |x| x.target == the_class && x.method_name && method_name }.first
|
33
|
+
unless entry
|
34
|
+
entry = Interchangeable.methods.select { |x| x.target == the_class.singleton_class && x.method_name && method_name }.first
|
35
|
+
end
|
36
|
+
entry.target.instance_eval do
|
33
37
|
define_method method_name, &block
|
34
38
|
end
|
35
|
-
entry = Interchangeable.methods.select { |x| x.target == the_class && x.method_name && method_name }.first
|
36
39
|
entry.implemented = true
|
37
40
|
entry.default = false
|
38
41
|
end
|
@@ -13,7 +13,7 @@ describe Interchangeable do
|
|
13
13
|
["Yawn", "applesauce", "make sure this works"],
|
14
14
|
].map { |x| Struct.new(:class_name, :method_name, :description).new(*x) }.each do |example|
|
15
15
|
|
16
|
-
describe "noting the method on a class" do
|
16
|
+
describe "noting the instance method on a class" do
|
17
17
|
|
18
18
|
before do
|
19
19
|
eval("class #{example.class_name}
|
@@ -166,6 +166,47 @@ describe Interchangeable do
|
|
166
166
|
|
167
167
|
end
|
168
168
|
|
169
|
+
describe "noting the class-level method on a class" do
|
170
|
+
|
171
|
+
before do
|
172
|
+
eval("class #{example.class_name}
|
173
|
+
class << self
|
174
|
+
interchangeable_describe \"#{example.description}\"
|
175
|
+
interchangeable_method :#{example.method_name}
|
176
|
+
end
|
177
|
+
end")
|
178
|
+
end
|
179
|
+
|
180
|
+
describe "methods" do
|
181
|
+
|
182
|
+
it "should create an entry" do
|
183
|
+
Interchangeable.methods.count.must_equal 1
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should return the target as a singleton class" do
|
187
|
+
Interchangeable.methods.first.target.must_equal eval(example.class_name).singleton_class
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
describe "defining the method later" do
|
193
|
+
|
194
|
+
let(:the_return_value) { Object.new }
|
195
|
+
|
196
|
+
before do
|
197
|
+
object = the_return_value
|
198
|
+
Interchangeable.define(eval(example.class_name), eval(":#{example.method_name}")) { object }
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should stamp the method on the object" do
|
202
|
+
instance = eval(example.class_name)
|
203
|
+
instance.send(example.method_name.to_sym).must_be_same_as the_return_value
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
|
169
210
|
end
|
170
211
|
|
171
212
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interchangeable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darren Cauthon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|