dry-container 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3dc623285b4e64816e0bea0c4668ab012291e234
4
+ data.tar.gz: 8fa2abd26fa7064d7765cc7cc27a75e01190888b
5
+ SHA512:
6
+ metadata.gz: c2dfecb1010dc1a4325ca8dd6842d157a127427895802fe133b41c6bb7ff2605a059be69a78a7d5bd7a0c11e96202d2ad7f42c39a0017cedb29b5c6d68fe8d31
7
+ data.tar.gz: 006d27b613370ba76c9d0301286cf7038c8141afe7d3f4c5f835245c1e68ae76aff2b6a70e82c0b5452ad79e6623b166e5b0e2880c147afdf8b495ce9c6a81c2
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ coverage
3
+ /.bundle
4
+ vendor/bundle
5
+ bin/
6
+ tmp/
7
+ .idea/
8
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --order random
@@ -0,0 +1,12 @@
1
+ # Generated by `rubocop --auto-gen-config`
2
+ inherit_from: .rubocop_todo.yml
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Lint/HandleExceptions:
11
+ Exclude:
12
+ - rakelib/*.rake
@@ -0,0 +1,11 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-06-10 01:03:27 +0100 using RuboCop version 0.26.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 1
9
+ # Configuration parameters: CountComments.
10
+ Metrics/MethodLength:
11
+ Max: 14
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ bundler_args: --without console
5
+ script:
6
+ - bundle exec rspec
7
+ - bundle exec rubocop
8
+ rvm:
9
+ - 2.0
10
+ - 2.1
11
+ - 2.2
12
+ - rbx-2
13
+ - jruby
14
+ - ruby-head
15
+ - jruby-head
16
+ env:
17
+ global:
18
+ - JRUBY_OPTS='--dev -J-Xmx1024M'
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: ruby-head
22
+ - rvm: jruby-head
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :tools do
6
+ gem 'rubocop'
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'guard-rubocop'
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Ruby Object Mapper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ s
@@ -0,0 +1,75 @@
1
+ # dry-container <a href="https://gitter.im/dryrb/chat" target="_blank">![Join the chat at https://gitter.im/dryrb/chat](https://badges.gitter.im/Join%20Chat.svg)</a>
2
+
3
+ <a href="https://rubygems.org/gems/dry-container" target="_blank">![Gem Version](https://badge.fury.io/rb/dry-container.svg)</a>
4
+ <a href="https://travis-ci.org/dryrb/dry-container" target="_blank">![Build Status](https://travis-ci.org/dryrb/dry-container.svg?branch=master)</a>
5
+ <a href="http://inch-ci.org/github/dryrb/dry-container" target="_blank">![Inline docs](http://inch-ci.org/github/dryrb/dry-container.svg?branch=master&style=flat)</a>
6
+
7
+ A simple IoC container implemented in Ruby
8
+
9
+ ## Synopsis
10
+
11
+ ```ruby
12
+ User = Struct.new(:name, :email)
13
+
14
+ data_store = ThreadSafe::Cache.new.tap do |ds|
15
+ ds[:users] = ThreadSafe::Array.new
16
+ end
17
+
18
+ # Initialize container
19
+ container = Dry::Container.new
20
+
21
+ # Register an item with the container to be resolved later
22
+ container.register(:data_store, data_store)
23
+ container.register(:user_repository, -> { container.resolve(:data_store)[:users] })
24
+
25
+ # Resolve an item from the container
26
+ container.resolve(:user_repository) << User.new('Jack', 'jack@dry-container.com')
27
+ # You can also resolve with []
28
+ container[:user_repository] << User.new('Jill', 'jill@dry-container.com')
29
+ # => [
30
+ # #<struct User name="Jack", email="jack@dry-container.com">,
31
+ # #<struct User name="Jill", email="jill@dry-container.com">
32
+ # ]
33
+
34
+ # If you wish to register an item that responds to call but don't want it to be
35
+ # called when resolved, you can use the options hash
36
+ container.register(:proc, -> { :result }, call: false)
37
+ container.resolve(:proc)
38
+ # => #<Proc:0x007fa75e652c98@(irb):25 (lambda)>
39
+
40
+ # You can also register using a block
41
+ container.register(:item) do
42
+ :result
43
+ end
44
+ container.resolve(:item)
45
+ # => :result
46
+
47
+ container.register(:block, call: false) do
48
+ :result
49
+ end
50
+ container.resolve(:block)
51
+ # => #<Proc:0x007fa75e6830f0@(irb):36>
52
+ ```
53
+
54
+ You can also get container behaviour at both the class and instance level via the mixin:
55
+
56
+ ```ruby
57
+ class Container
58
+ extend Dry::Container::Mixin
59
+ end
60
+ Container.register(:item, :my_item)
61
+ Container.resolve(:item)
62
+ # => :my_item
63
+
64
+ class ContainerObject
65
+ include Dry::Container::Mixin
66
+ end
67
+ container = ContainerObject.new
68
+ container.register(:item, :my_item)
69
+ container.resolve(:item)
70
+ # => :my_item
71
+ ```
72
+
73
+ ## License
74
+
75
+ See `LICENSE` file.
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
5
+
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+
9
+ task default: :spec
10
+
11
+ desc 'Run all specs in spec directory'
12
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ require File.expand_path('../lib/dry/container/version', __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'dry-container'
6
+ spec.version = Dry::Container::VERSION
7
+ spec.authors = ['Andy Holland']
8
+ spec.email = ['andyholland1991@aol.com']
9
+ spec.summary = 'A simple container intended for use as an IoC container'
10
+ spec.homepage = 'https://github.com/dryrb/dry-container'
11
+ spec.license = 'MIT'
12
+
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_runtime_dependency 'thread_safe'
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ end
@@ -0,0 +1,29 @@
1
+ require 'thread_safe'
2
+ require 'dry/container/error'
3
+ require 'dry/container/item'
4
+ require 'dry/container/mixin'
5
+ require 'dry/container/version'
6
+
7
+ module Dry
8
+ # Inversion of Control (IoC) container
9
+ #
10
+ # @example
11
+ #
12
+ # container = Dry::Container.new
13
+ # container.register(:item, 'item')
14
+ # container.resolve(:item)
15
+ # => 'item'
16
+ #
17
+ # container.register(:item1, -> { 'item' })
18
+ # container.resolve(:item1)
19
+ # => 'item'
20
+ #
21
+ # container.register(:item2, -> { 'item' }, call: false)
22
+ # container.resolve(:item2)
23
+ # => #<Proc:0x007f33b169e998@(irb):10 (lambda)>
24
+ #
25
+ # @api public
26
+ class Container
27
+ include Mixin
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ module Dry
2
+ class Container
3
+ # @api public
4
+ class Error < StandardError; end
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ module Dry
2
+ class Container
3
+ # Container class
4
+ #
5
+ # @private
6
+ class Item
7
+ attr_reader :item, :options
8
+
9
+ def initialize(item, options = {})
10
+ @item = item
11
+ @options = {
12
+ call: item.is_a?(::Proc)
13
+ }.merge(options)
14
+ end
15
+
16
+ def call
17
+ if options[:call] == true
18
+ item.call
19
+ else
20
+ item
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,111 @@
1
+ module Dry
2
+ class Container
3
+ # Mixin to expose Inversion of Control (IoC) container behaviour
4
+ #
5
+ # @example
6
+ #
7
+ # class MyClass
8
+ # extend Dry::Container::Mixin
9
+ # end
10
+ #
11
+ # MyClass.register(:item, 'item')
12
+ # MyClass.resolve(:item)
13
+ # => 'item'
14
+ #
15
+ # class MyObject
16
+ # include Dry::Container::Mixin
17
+ # end
18
+ #
19
+ # container = MyObject.new
20
+ # container.register(:item, 'item')
21
+ # container.resolve(:item)
22
+ # => 'item'
23
+ #
24
+ #
25
+ # @api public
26
+ module Mixin
27
+ # @private
28
+ def self.extended(base)
29
+ base.instance_variable_set(:@_mutex, Mutex.new)
30
+ end
31
+ # @private
32
+ def self.included(base)
33
+ base.send(:define_method, :initialize) do |*args, &block|
34
+ @_mutex = Mutex.new
35
+ super(*args, &block)
36
+ end
37
+ end
38
+ # Register an item with the container to be resolved later
39
+ #
40
+ # @param [Mixed] key
41
+ # The key to register the container item with (used to resolve)
42
+ # @param [Mixed] contents
43
+ # The item to register with the container (if no block given)
44
+ # @param [Hash] options
45
+ # @option options [Symbol] :call
46
+ # Whether the item should be called when resolved
47
+ # @yield
48
+ # If a block is given, contents will be ignored and the block
49
+ # will be registered instead
50
+ #
51
+ # @raise [Dry::Conainer::Error]
52
+ # If an item is already registered with the given key
53
+ #
54
+ # @return [Dry::Container] self
55
+ #
56
+ # @api public
57
+ def register(key, contents = nil, options = {}, &block)
58
+ if block_given?
59
+ item = block
60
+ options = contents if contents.is_a?(::Hash)
61
+ else
62
+ item = contents
63
+ end
64
+
65
+ if _container.key?(key)
66
+ fail Error, "There is already an item registered with the key #{key.inspect}"
67
+ else
68
+ _container[key] = Item.new(item, options)
69
+ end
70
+
71
+ self
72
+ end
73
+ # Resolve an item from the container
74
+ #
75
+ # @param [Mixed] key
76
+ # The key for the item you wish to resolve
77
+ #
78
+ # @raise [Dry::Conainer::Error]
79
+ # If the given key is not registered with the container
80
+ #
81
+ # @return [Mixed]
82
+ #
83
+ # @api public
84
+ def resolve(key)
85
+ item = _container.fetch(key) do
86
+ fail Error, "Nothing registered with the key #{key.inspect}"
87
+ end
88
+
89
+ item.call
90
+ end
91
+ # Resolve an item from the container
92
+ #
93
+ # @param [Mixed] key
94
+ # The key for the item you wish to resolve
95
+ #
96
+ # @return [Mixed]
97
+ #
98
+ # @api public
99
+ def [](key)
100
+ resolve(key)
101
+ end
102
+
103
+ private
104
+
105
+ # @private
106
+ def _container
107
+ @_mutex.synchronize { @_container ||= ThreadSafe::Cache.new }
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,6 @@
1
+ module Dry
2
+ class Container
3
+ # @api public
4
+ VERSION = '0.1.0'.freeze
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'rubocop/rake_task'
3
+
4
+ Rake::Task[:default].enhance [:rubocop]
5
+
6
+ RuboCop::RakeTask.new do |task|
7
+ task.options << '--display-cop-names'
8
+ end
9
+
10
+ namespace :rubocop do
11
+ desc 'Generate a configuration file acting as a TODO list.'
12
+ task :auto_gen_config do
13
+ exec 'bundle exec rubocop --auto-gen-config'
14
+ end
15
+ end
16
+
17
+ rescue LoadError
18
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.describe Dry::Container do
2
+ let(:container) { Dry::Container.new }
3
+
4
+ it_behaves_like 'a container'
5
+ end
@@ -0,0 +1,17 @@
1
+ RSpec.describe Dry::Container::Mixin do
2
+ describe 'extended' do
3
+ let(:container) do
4
+ Class.new { extend Dry::Container::Mixin }
5
+ end
6
+
7
+ it_behaves_like 'a container'
8
+ end
9
+
10
+ describe 'included' do
11
+ let(:container) do
12
+ Class.new { include Dry::Container::Mixin }.new
13
+ end
14
+
15
+ it_behaves_like 'a container'
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dry/container'
4
+
5
+ Dir[Pathname(__FILE__).dirname.join('support/**/*.rb').to_s].each do |file|
6
+ require file
7
+ end
@@ -0,0 +1,76 @@
1
+ shared_examples 'a container' do
2
+ describe 'registering a block' do
3
+ context 'without options' do
4
+ it 'registers and resolves an object' do
5
+ container.register(:item) { 'item' }
6
+
7
+ expect(container.resolve(:item)).to eq('item')
8
+ end
9
+ end
10
+
11
+ context 'with option call: false' do
12
+ it 'registers and resolves a proc' do
13
+ container.register(:item, call: false) { 'item' }
14
+
15
+ expect(container.resolve(:item).call).to eq('item')
16
+ expect(container[:item].call).to eq('item')
17
+ end
18
+ end
19
+ end
20
+
21
+ describe 'registering a proc' do
22
+ context 'without options' do
23
+ it 'registers and resolves an object' do
24
+ container.register(:item, proc { 'item' })
25
+
26
+ expect(container.resolve(:item)).to eq('item')
27
+ expect(container[:item]).to eq('item')
28
+ end
29
+ end
30
+
31
+ context 'with option call: false' do
32
+ it 'registers and resolves a proc' do
33
+ container.register(:item, proc { 'item' }, call: false)
34
+
35
+ expect(container.resolve(:item).call).to eq('item')
36
+ expect(container[:item].call).to eq('item')
37
+ end
38
+ end
39
+ end
40
+
41
+ describe 'registering an object' do
42
+ context 'without options' do
43
+ it 'registers and resolves the object' do
44
+ item = 'item'
45
+ container.register(:item, item)
46
+
47
+ expect(container.resolve(:item)).to be(item)
48
+ expect(container[:item]).to be(item)
49
+ end
50
+ end
51
+
52
+ context 'with option call: false' do
53
+ it 'registers and resolves an object' do
54
+ item = -> { 'test' }
55
+ container.register(:item, item, call: false)
56
+
57
+ expect(container.resolve(:item)).to eq(item)
58
+ expect(container[:item]).to eq(item)
59
+ end
60
+ end
61
+ end
62
+
63
+ describe 'registering with the same key multiple times' do
64
+ it do
65
+ container.register(:item, proc { 'item' })
66
+
67
+ expect { container.register(:item, proc { 'item' }) }.to raise_error(Dry::Container::Error)
68
+ end
69
+ end
70
+
71
+ describe 'resolving with a key that has not been registered' do
72
+ it do
73
+ expect { container.resolve(:item) }.to raise_error(Dry::Container::Error)
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-container
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andy Holland
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thread_safe
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - andyholland1991@aol.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .rubocop.yml
79
+ - .rubocop_todo.yml
80
+ - .travis.yml
81
+ - Gemfile
82
+ - LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - dry-container.gemspec
86
+ - lib/dry/container.rb
87
+ - lib/dry/container/error.rb
88
+ - lib/dry/container/item.rb
89
+ - lib/dry/container/mixin.rb
90
+ - lib/dry/container/version.rb
91
+ - rakelib/rubocop.rake
92
+ - spec/integration/container_spec.rb
93
+ - spec/integration/mixin_spec.rb
94
+ - spec/spec_helper.rb
95
+ - spec/support/shared_examples/container.rb
96
+ homepage: https://github.com/dryrb/dry-container
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.6
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: A simple container intended for use as an IoC container
120
+ test_files:
121
+ - spec/integration/container_spec.rb
122
+ - spec/integration/mixin_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/support/shared_examples/container.rb