roda-container 0.0.1 → 0.0.3

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +15 -1
  3. data/lib/roda/plugins/container.rb +29 -12
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5365074c2dcadd60d7bd776e6a7875b6139e5ea4
4
- data.tar.gz: fc953960aaafd78ac65dae44fe7f46315bb8540d
3
+ metadata.gz: 15f3136a230d1b934b51c0ca18f2c772378deb5f
4
+ data.tar.gz: 2ff4e2383eb0e47f4000b97912cc0a7d195b82b6
5
5
  SHA512:
6
- metadata.gz: 3e5cf15a1e0c8f773f910041f0689f255c99ed142bfd926e29a8f5d71dc46dfea6424769d5b3e12e16182cb9b1cbf20bc62200dfba6fe8b5a1949f53dcadeff4
7
- data.tar.gz: a0aeb38b04c25be7f612b7f76f774ccc421df0d43cc671118347dcc09f831b0e0827c41e779ed487c1272538df28891a04600e9d452a01e2126590a0fd5d1047
6
+ metadata.gz: 88241b79de3a8dcf2f8a801562923e26b9aa9b509156642776eccf280aab2fe2880f5c6c29fca44100149cc1ed425721d9e9ab6cc37cc0b4d4caf3717ebfb61e
7
+ data.tar.gz: 52f0ace71d379a143625d95bc2d22f9055f4e3b7eceee7eaecf0668750a540460e1d6dad38272f09ce2d0750ab827567d90e541c2bb689c0da27653e857a6ab1
data/README.md CHANGED
@@ -5,7 +5,7 @@ A plugin for Roda which turns your application into a (IoC) container
5
5
  ## Installation
6
6
 
7
7
  ```ruby
8
- gem 'roda-container', '0.0.1'
8
+ gem 'roda-container', '0.0.3'
9
9
  ```
10
10
 
11
11
  ## Usage
@@ -36,6 +36,20 @@ MyApplication.resolve(:person_repository).first
36
36
  # => {:name=>"Gill"}
37
37
  ```
38
38
 
39
+ If you want to register a proc, or anything that responds to call, without calling when resolving it, you can pass the `call: false` option:
40
+
41
+ ```ruby
42
+ class MyApplication < Roda
43
+ plugin :container
44
+ end
45
+
46
+ MyApplication.route do |r|
47
+ # Roda responds to the instance method #call, with the call: false
48
+ # option, calling MyApplication.resolve(:app) will not attempt to call
49
+ # it, without the option, the application would error
50
+ MyApplication.register(:app, self, call: false)
51
+ ```
52
+
39
53
  ## Contributing
40
54
 
41
55
  1. Fork it ( https://github.com/AMHOL/roda-container )
@@ -31,10 +31,25 @@ class Roda
31
31
  # MyApplication.register(:person_repository, -> { PersonRepository.new })
32
32
  # MyApplication.resolve(:person_repository).first
33
33
  module Container
34
+ class Content
35
+ attr_reader :item, :options
36
+
37
+ def initialize(item, options = {})
38
+ @item, @options = item, {
39
+ call: item.is_a?(::Proc)
40
+ }.merge(options)
41
+ end
42
+
43
+ def call
44
+ if options[:call] == true
45
+ item.call
46
+ else
47
+ item
48
+ end
49
+ end
50
+ end
51
+
34
52
  module ClassMethods
35
- # Whether middleware from the current class should be inherited by subclasses.
36
- # True by default, should be set to false when using a design where the parent
37
- # class accepts requests and uses run to dispatch the request to a subclass.
38
53
  attr_reader :container
39
54
  private :container
40
55
 
@@ -48,25 +63,27 @@ class Roda
48
63
  super
49
64
  end
50
65
 
51
- def register(key, contents = nil, &block)
52
- container[key] = block_given? ? block : contents
66
+ def register(key, contents = nil, options = {}, &block)
67
+ if block_given?
68
+ item = block
69
+ options = contents if contents.is_a?(::Hash)
70
+ else
71
+ item = contents
72
+ end
73
+ container[key] = Roda::RodaPlugins::Container::Content.new(item, options)
53
74
  end
54
75
 
55
76
  def resolve(key)
56
- instance = container.fetch(key) do
77
+ content = container.fetch(key) do
57
78
  fail ::Roda::ContainerError, "Nothing registered with the name #{key}"
58
79
  end
59
- instance.respond_to?(:call) ? instance.call : instance
80
+
81
+ content.call
60
82
  end
61
83
 
62
84
  def detach_container
63
85
  @container = container.dup
64
86
  end
65
-
66
- def freeze
67
- container.freeze
68
- super
69
- end
70
87
  end
71
88
  end
72
89
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-container
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Holland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-30 00:00:00.000000000 Z
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler