dep-inject 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: 714cfbce718d38561a3cd9977c26a88a51235795f6b50a81c1673fb2e1be21d6
4
- data.tar.gz: 5454342a77296850b4dd9a0590f0349ce729f99076cf691f6641c16a9c25530e
3
+ metadata.gz: cb06a052f6302e7e6f6ea046eb6e335d4cc48d862cb2a63c22d78d751183fab4
4
+ data.tar.gz: e51e0847f08fa11ffc2be4f137b9db5ad978c688f759323e2e2e05036a9ac820
5
5
  SHA512:
6
- metadata.gz: 5bfb65dcba2603139df734646ba31a1df627071c2bf176a7906fceac88d1514e10896cb1189a7867d6f50344fc69a1f011b38b2608ec2b9661ce06a61585179e
7
- data.tar.gz: '09d03a5158cadadd67feadc2d99ae19a5278e532aba65ad0a854e12b01a0916b2b3400608684741b003113a108a2000ceb0b4ec17badb1307cb271854908ce18'
6
+ metadata.gz: 3fe39a9ae5d4247d2c057b546e3ae82619442c51fe977a32c4b5d1c0a1dd8d4fad1308646ee4245e834ac9e85e53c4a588b80b1228678a452589dc4a8fcfeb94
7
+ data.tar.gz: e03c4819dfd284e84f7c80a4e519e63e07a53299f505ff05f1a57d7724f6d3961defc89af94178abbc6468562f84df05e86be2abddb8855b04b76c9b0ada64be
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Dep-inject
2
2
 
3
- Dep-inject is a simple Ruby gem that allows for clean and flexible dependency injection in Ruby classes. It provides a constructor method (`build`) and enforces a single public method (`execute`) to encourage well-structured use cases and services. The gem raises errors if other public methods are defined, ensuring that classes stay focused on a single responsibility.
3
+ Dep-inject is a simple Ruby gem that allows for clean and flexible dependency injection in Ruby classes. It provides a constructor method (`build`) and enforces a single public method (`execute`) to encourage well-structured use cases and services. The gem raises errors if other public methods are defined, ensuring that classes stay focused on a single responsibility. By injecting dependencies, the design becomes more flexible and maintainable, allowing for easier updates and replacements of components.
4
4
 
5
5
  ## Features
6
6
 
@@ -20,7 +20,7 @@ Then bundle install
20
20
 
21
21
  ## Usage
22
22
 
23
- To use DepInject, include the module in your class, specify your dependencies, and define the execute method. DepInject will handle the injection of the specified dependencies and enforce that only execute is exposed as a public method.
23
+ To use DepInject, include the module in your class, specify your dependencies, and define the execution method. DepInject will handle the injection of the specified dependencies and enforce that only a valid execution method is exposed as a public method. The execution or trigger methods are `execute` and `call`, use one or the other in your class.
24
24
 
25
25
  Dependencies can be any class or object. DepInject will initialize and inject them into your use case at runtime. The dependencies are injected as instance variables with the same name as the keys passed in provide.
26
26
 
@@ -39,6 +39,7 @@ class MockTaskManager
39
39
  end
40
40
  end
41
41
 
42
+ require 'dep_inject'
42
43
  class TaskUseCase
43
44
  include DepInject
44
45
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DepInject
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/dep_inject.rb CHANGED
@@ -5,30 +5,37 @@ require_relative "dep_inject/version"
5
5
  module DepInject
6
6
  class PublicMethodError < StandardError; end
7
7
 
8
+ class DuplicatedExecutionMethodError < StandardError; end
9
+
8
10
  def self.included(base)
9
11
  base.extend(ClassMethods)
10
12
  end
11
13
 
12
14
  module ClassMethods
13
- def provide(dependencies={})
15
+ def provide(dependencies = {})
14
16
  define_singleton_method :build do
15
17
  new(**prepare_dependencies(dependencies))
16
18
  end
17
19
 
18
20
  define_method :initialize do |**injected|
19
21
  injected.each do |name, dependency|
20
- instance_variable_set("@#{name}", dependency)
22
+ instance_variable_set(:"@#{name}", dependency)
21
23
  end
22
24
 
23
- unless self.class.instance_methods.include?(:execute)
24
- raise NotImplementedError, "Class #{self.class} must define an `execute` method"
25
+ has_execute = self.class.instance_methods.include?(:execute)
26
+ has_call = self.class.instance_methods.include?(:call)
27
+ unless has_execute || has_call
28
+ raise NotImplementedError, "Class #{self.class} must define an `execution` method"
25
29
  end
26
30
 
27
- public_methods_except_execute = self.class.public_instance_methods(false) - [:execute]
28
- unless public_methods_except_execute.empty?
29
- raise PublicMethodError, "Class #{self.class} should only define `execute` as a public method. Additional public methods: #{public_methods_except_execute.join(', ')}"
31
+ if has_execute && has_call
32
+ raise DuplicatedExecutionMethodError, "Class #{self.class} should only define one `execution` trigger"
30
33
  end
31
34
 
35
+ public_methods_except_execute = self.class.public_instance_methods(false) - [:execute, :call]
36
+ unless public_methods_except_execute.empty?
37
+ raise PublicMethodError, "Class #{self.class} should only define `execution` trigger as a public method. Additional public methods: #{public_methods_except_execute.join(", ")}"
38
+ end
32
39
  end
33
40
  end
34
41
 
@@ -49,6 +56,5 @@ module DepInject
49
56
  dependency
50
57
  end
51
58
  end
52
-
53
59
  end
54
60
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dep-inject
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
  - christophenonato
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-18 00:00:00.000000000 Z
11
+ date: 2025-01-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: DepInject helps to inject dependencies into your classes, enforcing public
14
14
  method restrictions.