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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41a3076d9bda63ccd3f36694165401bf37ef1578
4
- data.tar.gz: d880e8047bffe68e8cbd2422448bf008e64657ca
3
+ metadata.gz: 5f574e1ba4e6bbec95f013b9cb5bc11d28c7a330
4
+ data.tar.gz: 0b64c6fd5912373b0ff3815609e8430c715871f4
5
5
  SHA512:
6
- metadata.gz: 7f28a10fa1b917c8d46ecf4d656d3d257858f39dff0fbe7f658fb95d036e984eff6a7cb9f420cd194f117740d4f8d14bbebc34d75fb89fd5b0c006c0589dcf32
7
- data.tar.gz: abb638b2c1453c0d411d829ac8a3165046dc743187ecd402959e000b49b5206bbc8ee991ac963834d85834683b7de7ab9011b62a8ebc3ae09bb5bd3a9f035f2e
6
+ metadata.gz: e24bbe057e2a1722b81d4481f250fa3eb0f30f077f18fa82e5b5696038f01765a7f829fbffa1e7c248f4c2d784d6e262eea043ef756365b45510cb97491477d5
7
+ data.tar.gz: 45482427f1dda47e6911736e58bea8c1954bcf1391fcf9bf8d544c93e5113f228e5be58a664a1be9964ce03592116054e5844acddb8d8fc4383d71dad3f60fbb
@@ -17,4 +17,8 @@ module Pharrell
17
17
  def self.use_config(name)
18
18
  @@config = name
19
19
  end
20
+
21
+ def self.rebuild!
22
+ @@configs[@@config].rebuild!
23
+ end
20
24
  end
@@ -4,19 +4,55 @@ module Pharrell
4
4
  @map = {}
5
5
  end
6
6
 
7
- def bind(klass, instance_or_class)
8
- @map[klass] = instance_or_class
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 (instance_or_class.is_a? Class)
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
 
@@ -3,7 +3,7 @@ $:.unshift lib unless $:.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "pharrell"
6
- s.version = "0.1.0"
6
+ s.version = "0.2.0"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Callum Stott"]
9
9
  s.email = ["callum@seadowg.com"]
@@ -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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pharrell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Callum Stott