interjectable 0.0.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +6 -1
- data/lib/interjectable.rb +33 -1
- data/lib/interjectable/version.rb +1 -1
- data/spec/interjectable_spec.rb +41 -3
- 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: 65cb14e1a285fd271b95c26e0e21cfa7aa161cdf
|
4
|
+
data.tar.gz: 1d453d0ce9c8d3415e4944ff1d5888df77dcb828
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0eb41e6294a1bb6f80f58a135fae07f2adaaf161c76277e2499ae6f73784b66fd831a72c983c59082dadb0ef0ec596738bc0fac09e5472d38877e2baa2f95d1
|
7
|
+
data.tar.gz: 14e02cf2190d7bfbc77cfbfe96edee7280d52fe82c94ea67dc6a6ae178580125f8fb62e1e3c8b3b036ac62a029c6482f68eb9e0e5ff579ddaad4da3b64d62e98
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -12,14 +12,19 @@ gem 'interjectable'
|
|
12
12
|
|
13
13
|
## Usage
|
14
14
|
|
15
|
-
Interjectable has one module (`Interjectable`) and
|
15
|
+
Interjectable has one module (`Interjectable`) and two methods. Use them like so!
|
16
16
|
|
17
17
|
```ruby
|
18
18
|
class MyClass
|
19
19
|
extend Interjectable
|
20
20
|
|
21
|
+
# defines helper methods on instances that memoize values per instance
|
21
22
|
inject(:dependency) { SomeOtherClass.new }
|
22
23
|
inject(:other_dependency) { AnotherClass.new }
|
24
|
+
|
25
|
+
# defines helper methods on instances that memoize values statically,
|
26
|
+
# shared across all instances
|
27
|
+
inject_static(:shared_value) { ENV["SOME_VALUE"] }
|
23
28
|
end
|
24
29
|
```
|
25
30
|
|
data/lib/interjectable.rb
CHANGED
@@ -5,11 +5,18 @@ module Interjectable
|
|
5
5
|
super
|
6
6
|
end
|
7
7
|
|
8
|
+
# Defines a helper methods on instances that memoize values per-instance.
|
9
|
+
# Similar to writing
|
10
|
+
#
|
11
|
+
# attr_writer :dependency
|
12
|
+
#
|
13
|
+
# def dependency
|
14
|
+
# @dependency ||= instance_eval(&default_block)
|
15
|
+
# end
|
8
16
|
def inject(dependency, &default_block)
|
9
17
|
attr_writer dependency
|
10
18
|
|
11
19
|
define_method(dependency) do
|
12
|
-
# @dependency ||= instance_eval(&default_block)
|
13
20
|
ivar_name = "@#{dependency}"
|
14
21
|
if instance_variable_defined?(ivar_name)
|
15
22
|
instance_variable_get(ivar_name)
|
@@ -18,4 +25,29 @@ module Interjectable
|
|
18
25
|
end
|
19
26
|
end
|
20
27
|
end
|
28
|
+
|
29
|
+
# Defines helper methods on instances that memoize values per-class.
|
30
|
+
# (shared across all instances of a class, including instances of subclasses)
|
31
|
+
# Similar to writing
|
32
|
+
#
|
33
|
+
# cattr_writer :dependency
|
34
|
+
#
|
35
|
+
# def dependency
|
36
|
+
# @@dependency ||= instance_eval(&default_block)
|
37
|
+
# end
|
38
|
+
def inject_static(dependency, &default_block)
|
39
|
+
cvar_name = "@@#{dependency}"
|
40
|
+
|
41
|
+
define_method("#{dependency}=") do |value|
|
42
|
+
self.class.class_variable_set(cvar_name, value)
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method(dependency) do
|
46
|
+
if self.class.class_variable_defined?(cvar_name)
|
47
|
+
self.class.class_variable_get(cvar_name)
|
48
|
+
else
|
49
|
+
self.class.class_variable_set(cvar_name, instance_eval(&default_block))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
21
53
|
end
|
data/spec/interjectable_spec.rb
CHANGED
@@ -29,11 +29,11 @@ describe Interjectable do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it "calls dependency block once, even with a falsy value" do
|
32
|
-
|
32
|
+
count = 0;
|
33
|
+
klass.inject(:some_falsy_dependency) { count += 1; nil }
|
33
34
|
|
34
|
-
instance.instance_variable_set("@count", 0)
|
35
35
|
2.times { instance.some_falsy_dependency.should be_nil }
|
36
|
-
|
36
|
+
count.should == 1
|
37
37
|
end
|
38
38
|
|
39
39
|
context "with a dependency on another class" do
|
@@ -50,5 +50,43 @@ describe Interjectable do
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
|
+
|
54
|
+
describe "#inject_static" do
|
55
|
+
let(:other_instance) { klass.new }
|
56
|
+
|
57
|
+
before do
|
58
|
+
klass.inject_static(:static_dependency) { :some_value }
|
59
|
+
end
|
60
|
+
|
61
|
+
it "adds an instance method and setter" do
|
62
|
+
instance.static_dependency = 'aaa'
|
63
|
+
instance.static_dependency.should == 'aaa'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "shares a value across all instances of a class" do
|
67
|
+
instance.static_dependency = 'bbb'
|
68
|
+
other_instance.static_dependency.should == 'bbb'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "calls its dependency block once across all instances" do
|
72
|
+
count = 0;
|
73
|
+
klass.inject_static(:falsy_static_dependency) { count += 1; nil }
|
74
|
+
|
75
|
+
instance.falsy_static_dependency.should be_nil
|
76
|
+
other_instance.falsy_static_dependency.should be_nil
|
77
|
+
|
78
|
+
count.should == 1
|
79
|
+
end
|
80
|
+
|
81
|
+
context "with a subclas" do
|
82
|
+
let(:subclass) { Class.new(klass) }
|
83
|
+
let(:subclass_instance) { subclass.new }
|
84
|
+
|
85
|
+
it "shares its values with its superclass" do
|
86
|
+
instance.static_dependency = 'ccc'
|
87
|
+
subclass_instance.static_dependency.should == 'ccc'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
53
91
|
end
|
54
92
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interjectable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Margolis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|