dry-auto_inject 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f2aa9cdc617e2bfdf4a1c13a3cada9ff2df3a402
4
+ data.tar.gz: 4884e6893aa3c5e41de78fcff1548e1cb63bde5d
5
+ SHA512:
6
+ metadata.gz: a05899761a453cd74c5d1fef9ad82a13e7cf5c64c4251314ee0140fedf3ef088cb3714d07aae53598a6a5e23843953c9c22ac020964f40416bcb68bfd417ae9b
7
+ data.tar.gz: dc5c7e8e5b0d4112ef032d5b7ea9b0a8fc1fe6223e29e3c741c13a0260ec312686a1a21b6fdab45f199fe52c7c56c8b99182f4d34f54cf1866c5f3fec6f85041
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /vendor/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ -r ./spec/spec_helper.rb
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ # Generated by `rubocop --auto-gen-config`
2
+ inherit_from: .rubocop_todo.yml
3
+
4
+ Metrics/LineLength:
5
+ Max: 110
6
+
7
+ Lint/HandleExceptions:
8
+ Exclude:
9
+ - rakelib/*.rake
10
+
11
+ Style/LambdaCall:
12
+ EnforcedStyle: braces
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/FileName:
18
+ Exclude:
19
+ - lib/dry-pipeline.rb
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,6 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-08-19 22:11:28 +0100 using RuboCop version 0.32.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.
data/.travis.yml ADDED
@@ -0,0 +1,29 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ bundler_args: --without console
5
+ script:
6
+ - bundle exec rake spec
7
+ rvm:
8
+ - 2.0
9
+ - 2.1
10
+ - 2.2
11
+ - rbx-2
12
+ - jruby
13
+ - ruby-head
14
+ - jruby-head
15
+ env:
16
+ global:
17
+ - JRUBY_OPTS='--dev -J-Xmx1024M'
18
+ matrix:
19
+ allow_failures:
20
+ - rvm: ruby-head
21
+ - rvm: jruby-head
22
+ notifications:
23
+ email: false
24
+ webhooks:
25
+ urls:
26
+ - https://webhooks.gitter.im/e/19098b4253a72c9796db
27
+ on_success: change # options: [always|never|change] default: always
28
+ on_failure: always # options: [always|never|change] default: always
29
+ on_start: false # default: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ v0.0.1 2015-08-20
2
+
3
+ First public release \o/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dry-container.gemspec
4
+ gemspec
5
+
6
+ group :tools do
7
+ gem 'byebug', platforms: :mri
8
+ gem 'rubocop'
9
+ gem 'guard'
10
+ gem 'guard-rspec'
11
+ gem 'guard-rubocop'
12
+ end
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Dry::AutoInject <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-auto_inject" target="_blank">![Gem Version](https://badge.fury.io/rb/dry-auto_inject.svg)</a>
4
+ <a href="https://travis-ci.org/dryrb/dry-auto_inject" target="_blank">![Build Status](https://travis-ci.org/dryrb/dry-auto_inject.svg?branch=master)</a>
5
+ <a href="https://gemnasium.com/dryrb/dry-auto_inject" target="_blank">![Dependency Status](https://gemnasium.com/dryrb/dry-auto_inject.svg)</a>
6
+ <a href="https://codeclimate.com/github/dryrb/dry-auto_inject" target="_blank">![Code Climate](https://codeclimate.com/github/dryrb/dry-auto_inject/badges/gpa.svg)</a>
7
+ <a href="http://inch-ci.org/github/dryrb/dry-auto_inject" target="_blank">![Documentation Status](http://inch-ci.org/github/dryrb/dry-auto_inject.svg?branch=master&style=flat)</a>
8
+
9
+ A simple extensions which allows you to automatically inject dependencies to your
10
+ object constructors from a configured container.
11
+
12
+ It does 3 things:
13
+
14
+ - Defines a constructor which accepts dependencies
15
+ - Defines attribute readers for dependencies
16
+ - Injects dependencies automatically to the constructor with overridden `.new`
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'dry-auto_inject'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ ```sh
29
+ $ bundle
30
+ ```
31
+
32
+ Or install it yourself as:
33
+ ```sh
34
+ $ gem install dry-auto_inject
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ You can use `AutoInject` with any container that responds to `[]`. In this example
40
+ we're going to use `dry-container`:
41
+
42
+ ```ruby
43
+ # set up your container
44
+ my_container = Dry::Container.new
45
+
46
+ my_container.register(:data_store, -> { DataStore.new })
47
+ my_container.register(:user_repository, -> { container[:data_store][:users] })
48
+ my_container.register(:persist_user, -> { PersistUser.new })
49
+
50
+ # set up your auto-injection module
51
+
52
+ AutoInject = Dry::AutoInject.new { container(my_container) }
53
+
54
+ # then simply include it in your class providing which dependencies should be
55
+ # injected automatically from the configure container
56
+ class PersistUser
57
+ include AutoInject[:user_repository]
58
+
59
+ def call(user)
60
+ user_repository << user
61
+ end
62
+ end
63
+
64
+ persist_user = my_container[:persist_user]
65
+
66
+ persist_user.call(name: 'Jane')
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dryrb/dry-auto_inject.
78
+
data/Rakefile ADDED
@@ -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)
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'dry/pipeline'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dry/auto_inject/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dry-auto_inject'
8
+ spec.version = Dry::AutoInject::VERSION.dup
9
+ spec.authors = ['Piotr Solnica']
10
+ spec.email = ['piotr.solnica@gmail.com']
11
+
12
+ spec.summary = 'Container-agnostic automatic constructor injection'
13
+ spec.homepage = 'https://github.com/dryrb/dry-auto_inject'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ end
@@ -0,0 +1 @@
1
+ require 'dry/auto_inject'
@@ -0,0 +1,63 @@
1
+ require 'dry/auto_inject/version'
2
+ require 'dry/auto_inject/injection'
3
+
4
+ module Dry
5
+ # Main DSL class
6
+ #
7
+ # @api public
8
+ class AutoInject
9
+ # @attr_reader [Injection] injection The configured injection module
10
+ #
11
+ # @api private
12
+ attr_reader :injection
13
+
14
+ # Configure an auto-injection module
15
+ #
16
+ # @example
17
+ # # set up your container
18
+ # my_container = Dry::Container.new
19
+ #
20
+ # my_container.register(:data_store, -> { DataStore.new })
21
+ # my_container.register(:user_repository, -> { container[:data_store][:users] })
22
+ # my_container.register(:persist_user, -> { PersistUser.new })
23
+ #
24
+ # # set up your auto-injection module
25
+ #
26
+ # AutoInject = Dry::AutoInject.new { container(my_container) }
27
+ #
28
+ # # then simply include it in your class providing which dependencies should be
29
+ # # injected automatically from the configure container
30
+ # class PersistUser
31
+ # include AutoInject[:user_repository]
32
+ #
33
+ # def call(user)
34
+ # user_repository << user
35
+ # end
36
+ # end
37
+ #
38
+ # persist_user = my_container[:persist_user]
39
+ #
40
+ # persist_user.call(name: 'Jane')
41
+ #
42
+ # @return [Dry::AutoInject::Injection]
43
+ #
44
+ # @api public
45
+ def self.new(&block)
46
+ dsl = super(&block)
47
+ dsl.injection
48
+ end
49
+
50
+ # @api private
51
+ def initialize(&block)
52
+ instance_exec(&block)
53
+ @injection = -> *names { Injection.new(names, @container) }
54
+ end
55
+
56
+ # Set up the container for the injection module
57
+ #
58
+ # @api public
59
+ def container(container)
60
+ @container = container
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,71 @@
1
+ module Dry
2
+ # @api private
3
+ class Injection < Module
4
+ attr_reader :names
5
+
6
+ attr_reader :container
7
+
8
+ attr_reader :instance_mod
9
+
10
+ attr_reader :ivars
11
+
12
+ # @api private
13
+ def initialize(names, container)
14
+ @names = names
15
+ @container = container
16
+ @ivars = names.map(&:to_s).map { |s| s.split('.').last }.map(&:to_sym)
17
+ @instance_mod = Module.new
18
+ define_constructor
19
+ end
20
+
21
+ # @api private
22
+ def included(klass)
23
+ define_new_method(klass)
24
+ define_container(klass)
25
+
26
+ klass.send(:include, instance_mod)
27
+
28
+ super
29
+ end
30
+
31
+ private
32
+
33
+ # @api private
34
+ def define_container(klass)
35
+ klass.instance_variable_set('@container', container)
36
+
37
+ klass.class_eval do
38
+ def self.container
39
+ if superclass.respond_to?(:container)
40
+ superclass.container
41
+ else
42
+ @container
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ # @api private
49
+ def define_new_method(klass)
50
+ klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
51
+ def self.new(*args)
52
+ names = [#{names.map(&:inspect).join(', ')}]
53
+ deps = names.map.with_index { |_, i| args[i] || container[names[i]] }
54
+ super(*deps)
55
+ end
56
+ RUBY
57
+ end
58
+
59
+ # @api private
60
+ def define_constructor
61
+ instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
62
+ attr_reader #{ivars.map { |name| ":#{name}" }.join(', ')}
63
+
64
+ def initialize(*args)
65
+ #{ivars.map.with_index { |name, i| "@#{name} = args[#{i}]" }.join("\n")}
66
+ end
67
+ RUBY
68
+ self
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ module Dry
2
+ class AutoInject
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ 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
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-auto_inject
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Solnica
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rake
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: rspec
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
+ description:
56
+ email:
57
+ - piotr.solnica@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".rubocop_todo.yml"
66
+ - ".travis.yml"
67
+ - CHANGELOG.md
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - dry-auto_inject.gemspec
74
+ - lib/dry-auto_inject.rb
75
+ - lib/dry/auto_inject.rb
76
+ - lib/dry/auto_inject/injection.rb
77
+ - lib/dry/auto_inject/version.rb
78
+ - rakelib/rubocop.rake
79
+ homepage: https://github.com/dryrb/dry-auto_inject
80
+ licenses: []
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Container-agnostic automatic constructor injection
102
+ test_files: []