manioc 0.1.1 → 0.1.2

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: 9c204df74bb5ae2a4c17daecceca875db34d4aa6
4
- data.tar.gz: 4f272011540301a92c4f1ac816c8ec705804e467
3
+ metadata.gz: a438dc71640e6700d5efb85a5973817236dc0990
4
+ data.tar.gz: ce5cae5cc25e2e1edf13c08b4bcaadfd7281c264
5
5
  SHA512:
6
- metadata.gz: 89421ae0da8f6acbbd1d2301491afcb4d50276d0d9a3948525e31dd91cf482897ba8c742877447c6bea10085b4df934b77fb9b7b14ec0de760a62a8cd91979c6
7
- data.tar.gz: 2e2da756fe09a6f7f149113d19a76e5bac022d61616ce54cd0baee2267be846f39828c3d986dc3d5a12444fea16e81c566ec3b3b5087fdcf6eef7df51e87ce53
6
+ metadata.gz: 99c3852f3e9ab5c68c019bc38f50f9aab951b147e600c5b6755bfcddd3b7c09c70e3a5fb90d65291572e8349ed5325dc54b5bc3c2b21597413dd42b26b70eab3
7
+ data.tar.gz: 54741d37eb1808c6785543a0d706494b1307e1780f24bce414f137c5786c0b17e9f8a7fbc3cf1e507a2a7695fbbeab4854a0e683091b95cedaa733e3991a09d1
@@ -16,20 +16,27 @@ module Manioc
16
16
  end
17
17
  end
18
18
 
19
- def initialize **constructors, &block
20
- @constructors, @instances = constructors, {}
19
+ def initialize cache: true, preload: false, constructors: {}, &block
20
+ @constructors = constructors
21
+ @cache = cache ? {} : nil
22
+ @preload = preload
21
23
 
22
24
  register(&block) if block
23
25
  finalize
24
26
  end
25
27
 
26
28
  def with &block
27
- self.class.new @constructors.dup, &block
29
+ self.class.new \
30
+ constructors: @constructors.dup,
31
+ cache: @cache.any?,
32
+ preload: @preload,
33
+ &block
28
34
  end
29
35
 
30
36
  def reset key=nil
31
- keys = key ? [key] : @instances.keys
32
- keys.each { |k| @instances.delete k }
37
+ return unless @cache
38
+ keys = key ? [key] : @cache.keys
39
+ keys.each { |k| @cache.delete k }
33
40
  end
34
41
 
35
42
  private
@@ -39,13 +46,21 @@ module Manioc
39
46
  end
40
47
 
41
48
  def resolve key
42
- @instances[key] ||= instance_exec(&@constructors[key])
49
+ instance_exec(&@constructors[key])
43
50
  end
44
51
 
45
52
  def finalize
46
53
  @constructors.freeze
47
54
  @constructors.each do |key,_|
48
- define_singleton_method(key) { resolve key }
55
+ if @cache
56
+ define_singleton_method(key) { @cache[key] ||= resolve key }
57
+ else
58
+ define_singleton_method(key) { resolve key }
59
+ end
60
+
61
+ if @preload
62
+ public_send key
63
+ end
49
64
  end
50
65
  end
51
66
  end
@@ -1,3 +1,3 @@
1
1
  module Manioc
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manioc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Dabbs