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.
- checksums.yaml +4 -4
- data/README.md +15 -1
- data/lib/roda/plugins/container.rb +29 -12
- 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: 15f3136a230d1b934b51c0ca18f2c772378deb5f
|
4
|
+
data.tar.gz: 2ff4e2383eb0e47f4000b97912cc0a7d195b82b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
-
|
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
|
-
|
77
|
+
content = container.fetch(key) do
|
57
78
|
fail ::Roda::ContainerError, "Nothing registered with the name #{key}"
|
58
79
|
end
|
59
|
-
|
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.
|
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-
|
11
|
+
date: 2015-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|