helium-dependency 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: e3c477fdd6eae79003d31bd5f809f152e600f490c0924c77359c63084f39a637
4
- data.tar.gz: a1c782280618b56c55bbbea4a134d51e34649e434fccfc8ff6f1924b2a3a06c2
3
+ metadata.gz: e4bd58694ef42a91e52faa8df95f2e5a07ec62d243810dbb838119c4565dacc6
4
+ data.tar.gz: 6aa2e549751835d4e9da275724003657c49f96124e34f3ca565e39a6d62fc1be
5
5
  SHA512:
6
- metadata.gz: 697acb06244340a05a6d4b09386e5a6fd3690e0cf7a0e3c6ac77ea59b62e02c5c0a2ff06caaaa1e3f7752c4308afbfbf11d151baf5cb18b3aae10ad461fc0ad1
7
- data.tar.gz: fa339c5751f62dec3d0c7217123d528a1d45397c558cbc7010cef58fd008f92899f81c7484ac97ee3fb518d230b25811a26ebd77f2ca74a80cd742d061d51cb4
6
+ metadata.gz: 94ec932a3e80a1114a5a95460a059ed2dbb35d6676ce5c19c0d08ab7470b74a805989be6c02c79cd315f8cdad36594f0b65cd8b9debf9f094b3575f9f321d0d0
7
+ data.tar.gz: 29e6060e937216a3985be5a8400ac9a9aed828bc1dfe9825eee6aff198ce06e3eaa24ae5db5eca968e5c721fad49add9952168613876797881972eaf33cbabfb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- helium-dependency (0.1.0)
4
+ helium-dependency (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Helium::Dependency
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/helium/dependency`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Simple dependency inkjection for Ruby object.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,34 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ Simply include `Helium::Dependency` and then define the list of dependencies, together with a block to create default injection.
24
+ Injected value are already available when the default block is evaluated, so dependencies can reference each other.
25
+
26
+ ```
27
+ class Bike
28
+ include Helium::Dependency
29
+
30
+ dependency(:wheel_size) { 26 }
31
+ dependency(:wheels, public: true) { Array.new(2) { Wheel.new(size: wheel_size) } }
32
+ end
33
+
34
+ Bike.new(wheel_size: 20).wheels #=> [#Wheel(@size: 20), #Wheel(@size: 20)]
35
+ ```
36
+
37
+ Injection happens in the class `new` method before `initialize` is called, so you can define your own initialization and use dependencies inside it.
38
+
39
+ ```
40
+ class NamedBike < Bike
41
+ def initialize(name)
42
+ @name = "#{name} (#{wheels.map(&:size).join(', ')})"
43
+ end
44
+
45
+ attr_reader :name
46
+ end
47
+
48
+ NamedBike.new("Steven").name #=> "Steven (26, 26)"
49
+ NamedBike.new("Lil Steven", wheel_size: 16).name #=> "Lil Steven (16, 16)"
50
+ ```
26
51
 
27
52
  ## Development
28
53
 
@@ -6,18 +6,37 @@ module Helium
6
6
 
7
7
  def self.included(mod)
8
8
  mod.extend ClassMethods
9
- mod.prepend Initialization
10
9
  end
11
10
 
12
11
  module ClassMethods
13
- def dependency(name, &default_block)
12
+ def new(*args, **kwargs, &block)
13
+ instance = allocate
14
+
15
+ deps = {}
16
+ dependencies.each do |dep|
17
+ next unless kwargs.key?(dep)
18
+ deps[dep] = kwargs.delete(dep)
19
+ end
20
+
21
+ instance.instance_variable_set(:@deps, deps)
22
+
23
+ if kwargs.any?
24
+ instance.send :initialize, *args, **kwargs, &block
25
+ else
26
+ instance.send :initialize, *args, &block
27
+ end
28
+
29
+ instance
30
+ end
31
+
32
+ def dependency(name, public: false, &default_block)
14
33
  dependencies << name
15
34
 
16
35
  define_method(name) do
17
36
  @deps[name] ||= instance_exec(&default_block)
18
37
  end
19
38
 
20
- private name
39
+ private(name) unless public
21
40
  end
22
41
 
23
42
  def dependencies
@@ -25,22 +44,5 @@ module Helium
25
44
  end
26
45
  end
27
46
 
28
- module Initialization
29
- def initialize(*args, **kwargs, &block)
30
- @deps = {}
31
-
32
- self.class.dependencies.each do |dep|
33
- next unless kwargs.key?(dep)
34
-
35
- @deps[dep] = kwargs.delete(dep)
36
- end
37
-
38
- if kwargs.any?
39
- super *args, **kwargs, &block
40
- else
41
- super *args, &block
42
- end
43
- end
44
- end
45
47
  end
46
48
  end
@@ -1,5 +1,5 @@
1
1
  module Helium
2
2
  module Dependency
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helium-dependency
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stanislaw Klajn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-08 00:00:00.000000000 Z
11
+ date: 2021-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler