pharrell 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/lib/pharrell.rb +4 -0
- data/lib/pharrell/config.rb +39 -3
- data/pharrell.gemspec +1 -1
- data/spec/config_spec.rb +12 -0
- data/spec/pharrell_spec.rb +14 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5f574e1ba4e6bbec95f013b9cb5bc11d28c7a330
|
|
4
|
+
data.tar.gz: 0b64c6fd5912373b0ff3815609e8430c715871f4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e24bbe057e2a1722b81d4481f250fa3eb0f30f077f18fa82e5b5696038f01765a7f829fbffa1e7c248f4c2d784d6e262eea043ef756365b45510cb97491477d5
|
|
7
|
+
data.tar.gz: 45482427f1dda47e6911736e58bea8c1954bcf1391fcf9bf8d544c93e5113f228e5be58a664a1be9964ce03592116054e5844acddb8d8fc4383d71dad3f60fbb
|
data/lib/pharrell.rb
CHANGED
data/lib/pharrell/config.rb
CHANGED
|
@@ -4,19 +4,55 @@ module Pharrell
|
|
|
4
4
|
@map = {}
|
|
5
5
|
end
|
|
6
6
|
|
|
7
|
-
def bind(klass,
|
|
8
|
-
|
|
7
|
+
def bind(klass, arg = nil, &blk)
|
|
8
|
+
if blk
|
|
9
|
+
options = arg.kind_of?(Hash) ? arg : {}
|
|
10
|
+
@map[klass] = ObjectGenerator.new(options, &blk)
|
|
11
|
+
else
|
|
12
|
+
@map[klass] = arg
|
|
13
|
+
end
|
|
9
14
|
end
|
|
10
15
|
|
|
11
16
|
def instance_for(klass)
|
|
12
17
|
instance_or_class = @map[klass]
|
|
13
18
|
|
|
14
|
-
if
|
|
19
|
+
if instance_or_class.is_a? Class
|
|
15
20
|
instance_or_class.new
|
|
21
|
+
elsif instance_or_class.is_a? ObjectGenerator
|
|
22
|
+
instance_or_class.fetch
|
|
16
23
|
else
|
|
17
24
|
instance_or_class
|
|
18
25
|
end
|
|
19
26
|
end
|
|
27
|
+
|
|
28
|
+
def rebuild!
|
|
29
|
+
@map.each do |key, value|
|
|
30
|
+
if value.is_a? ObjectGenerator
|
|
31
|
+
value.invalidate!
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
class ObjectGenerator
|
|
39
|
+
def initialize(options = {}, &blk)
|
|
40
|
+
@blk = blk
|
|
41
|
+
@options = options
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def fetch
|
|
45
|
+
if @options[:cache]
|
|
46
|
+
@value ||= @blk.call
|
|
47
|
+
else
|
|
48
|
+
@blk.call
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def invalidate!
|
|
53
|
+
@value = nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
20
56
|
end
|
|
21
57
|
end
|
|
22
58
|
|
data/pharrell.gemspec
CHANGED
data/spec/config_spec.rb
CHANGED
|
@@ -16,5 +16,17 @@ describe "Pharrell::Config" do
|
|
|
16
16
|
config.bind(String, my_class)
|
|
17
17
|
assert_equal(config.instance_for(String).class, my_class)
|
|
18
18
|
end
|
|
19
|
+
|
|
20
|
+
it "allows a lazy instance to be passed as a block" do
|
|
21
|
+
config = Pharrell::Config.new
|
|
22
|
+
config.bind(Object) { Object.new }
|
|
23
|
+
assert(config.instance_for(Object) != config.instance_for(Object))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "can cache block generated instances" do
|
|
27
|
+
config = Pharrell::Config.new
|
|
28
|
+
config.bind(Object, :cache => true) { Object.new }
|
|
29
|
+
assert_equal(config.instance_for(Object), config.instance_for(Object))
|
|
30
|
+
end
|
|
19
31
|
end
|
|
20
32
|
end
|
data/spec/pharrell_spec.rb
CHANGED
|
@@ -19,4 +19,18 @@ describe "Pharrell" do
|
|
|
19
19
|
assert_equal(Pharrell.instance_for(Time), Time.at(0))
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
describe ".rebuild!" do
|
|
24
|
+
it "forces all lazy values to be rebuilt" do
|
|
25
|
+
Pharrell.config(:main) do |config|
|
|
26
|
+
config.bind(Object, :cache => true) { Object.new }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
Pharrell.use_config(:main)
|
|
30
|
+
|
|
31
|
+
original = Pharrell.instance_for(Object)
|
|
32
|
+
Pharrell.rebuild!
|
|
33
|
+
assert(Pharrell.instance_for(Object) != original)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
22
36
|
end
|