dry-auto_inject 0.1.0 → 0.2.0

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: 811a2b285fae7c657e214c57d682488c2b1d3ed8
4
- data.tar.gz: 4d6bff629910fc0400e2725b401c2f270fe135bc
3
+ metadata.gz: 59c074194d93b68ca4f6f5556cb2e50b6b613bb4
4
+ data.tar.gz: 2441a44b97fba6eceecbe97404f75e52367020eb
5
5
  SHA512:
6
- metadata.gz: 0220dcb80424b49015821e64bbe51245a0a8c5d0cba462fdf1150e37e00e4f92116edbada4b7ec4a18a8f015cd3bcf20c1c586f6f7c36d0f8843fb9b4d30b12e
7
- data.tar.gz: 966a0d328c57c4d72791f30311418892ae69def872d683718bbceae688d52f3afcc76556b60741430fc5dda086659d93bf679eb0ffbf870b48a1846ba2ba0571
6
+ metadata.gz: 7696fa4ae133db3ee5d2e80bbdd9b293eaf9aa5ab4dab1993ef35280ab82262bbfb3778cba933b722043de629919826f27dcc1c39b4db1d610d5d57fd9f7e707
7
+ data.tar.gz: 9d896a2cffcf233531d32b02f9c3d91288db46659236b53538993969427974bda1222cd90cb124de2b5249c5d235330b4c15a2337327bc4f0a47c2d2f5f1fe06
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # v0.2.0 2016-02-09
2
+
3
+ ### Added
4
+
5
+ * Support for hashes as constructor arguments via `Import.hash` interface (solnic)
6
+
7
+ [Compare v0.1.0...v0.2.0](https://github.com/dryrb/dry-auto_inject/compare/v0.1.0...v0.2.0)
8
+
1
9
  # v0.1.0 2015-11-12
2
10
 
3
11
  Changed interface from `Dry::AutoInject.new { container(some_container) }` to
@@ -39,7 +39,27 @@ module Dry
39
39
  #
40
40
  # @api public
41
41
  def self.AutoInject(container)
42
- -> *names { AutoInject.new(names, container) }
42
+ Injection.new(container)
43
+ end
44
+
45
+ class Injection
46
+ attr_reader :container, :options
47
+
48
+ # @api private
49
+ def initialize(container, options = {})
50
+ @container = container
51
+ @options = options
52
+ end
53
+
54
+ # @api public
55
+ def hash
56
+ self.class.new(container, options.merge(type: :hash))
57
+ end
58
+
59
+ # @api public
60
+ def [](*names)
61
+ AutoInject.new(names, container, options)
62
+ end
43
63
  end
44
64
 
45
65
  # @api private
@@ -52,10 +72,16 @@ module Dry
52
72
 
53
73
  attr_reader :ivars
54
74
 
75
+ attr_reader :options
76
+
77
+ attr_reader :type
78
+
55
79
  # @api private
56
- def initialize(names, container)
80
+ def initialize(names, container, options = {})
57
81
  @names = names
58
82
  @container = container
83
+ @options = options
84
+ @type = options.fetch(:type, :args)
59
85
  @ivars = names.map(&:to_s).map { |s| s.split('.').last }.map(&:to_sym)
60
86
  @instance_mod = Module.new
61
87
  define_constructor
@@ -63,6 +89,7 @@ module Dry
63
89
 
64
90
  # @api private
65
91
  def included(klass)
92
+ define_readers
66
93
  define_new_method(klass)
67
94
  define_container(klass)
68
95
 
@@ -90,6 +117,14 @@ module Dry
90
117
 
91
118
  # @api private
92
119
  def define_new_method(klass)
120
+ case type
121
+ when :args then define_new_method_with_args(klass)
122
+ when :hash then define_new_method_with_hash(klass)
123
+ end
124
+ end
125
+
126
+ # @api private
127
+ def define_new_method_with_args(klass)
93
128
  klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
94
129
  def self.new(*args)
95
130
  names = [#{names.map(&:inspect).join(', ')}]
@@ -99,11 +134,30 @@ module Dry
99
134
  RUBY
100
135
  end
101
136
 
137
+ # @api private
138
+ def define_new_method_with_hash(klass)
139
+ klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
140
+ def self.new(options = {})
141
+ names = [#{names.map(&:inspect).join(', ')}]
142
+ deps = names.each_with_object({}) { |k, r|
143
+ r[k.to_s.split('.').last.to_sym] = options[k] || container[k]
144
+ }.merge(options)
145
+ super(deps)
146
+ end
147
+ RUBY
148
+ end
149
+
102
150
  # @api private
103
151
  def define_constructor
104
- instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
105
- attr_reader #{ivars.map { |name| ":#{name}" }.join(', ')}
152
+ case type
153
+ when :args then define_constructor_with_args
154
+ when :hash then define_constructor_with_hash
155
+ end
156
+ end
106
157
 
158
+ # @api private
159
+ def define_constructor_with_args
160
+ instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
107
161
  def initialize(*args)
108
162
  super()
109
163
  #{ivars.map.with_index { |name, i| "@#{name} = args[#{i}]" }.join("\n")}
@@ -111,5 +165,24 @@ module Dry
111
165
  RUBY
112
166
  self
113
167
  end
168
+
169
+ # @api private
170
+ def define_constructor_with_hash
171
+ instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
172
+ def initialize(options)
173
+ super()
174
+ #{ivars.map { |name| "@#{name} = options[:#{name}]" }.join("\n")}
175
+ end
176
+ RUBY
177
+ self
178
+ end
179
+
180
+ # @api private
181
+ def define_readers
182
+ instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
183
+ attr_reader #{ivars.map { |name| ":#{name}" }.join(', ')}
184
+ RUBY
185
+ self
186
+ end
114
187
  end
115
188
  end
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  class AutoInject < Module
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-auto_inject
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-12 00:00:00.000000000 Z
11
+ date: 2016-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,9 +94,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  requirements: []
96
96
  rubyforge_project:
97
- rubygems_version: 2.4.5
97
+ rubygems_version: 2.4.5.1
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Container-agnostic automatic constructor injection
101
101
  test_files: []
102
- has_rdoc: