Class: Dry::Effects::Providers::Resolve

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/effects/providers/resolve.rb

Constant Summary collapse

Locate =
Effect.new(type: :resolve, name: :locate)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResolve

Returns a new instance of Resolve.



21
22
23
24
# File 'lib/dry/effects/providers/resolve.rb', line 21

def initialize(*)
  super
  @dynamic = EMPTY_HASH
end

Instance Attribute Details

#dynamicObject (readonly)

Returns the value of attribute dynamic.



19
20
21
# File 'lib/dry/effects/providers/resolve.rb', line 19

def dynamic
  @dynamic
end

#parentObject (readonly)

Returns the value of attribute parent.



17
18
19
# File 'lib/dry/effects/providers/resolve.rb', line 17

def parent
  @parent
end

Class Method Details

.handle_method(as: Undefined) ⇒ Object



7
8
9
# File 'lib/dry/effects/providers/resolve.rb', line 7

def self.handle_method(*, as: Undefined, **)
  Undefined.default(as, :provide)
end

Instance Method Details

#call(dynamic = EMPTY_HASH, options = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yield the block with the handler installed

API:

  • private



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dry/effects/providers/resolve.rb', line 47

def call(dynamic = EMPTY_HASH, options = EMPTY_HASH)
  @dynamic = dynamic

  if options.fetch(:overridable, false)
    @parent = ::Dry::Effects.yield(Locate) { nil }
  else
    @parent = nil
  end

  yield
ensure
  @dynamic = EMPTY_HASH
end

#key?(key) ⇒ Boolean

Parameters:

  • Dependency key

Returns:

API:

  • public



75
76
77
# File 'lib/dry/effects/providers/resolve.rb', line 75

def key?(key)
  static.key?(key) || dynamic.key?(key) || parent&.key?(key)
end

#locateProvider

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Locate handler in the stack

Returns:

API:

  • private



42
# File 'lib/dry/effects/providers/resolve.rb', line 42

def locate = self

#provide?(effect) ⇒ Boolean

Parameters:

Returns:

API:

  • public



64
65
66
67
68
69
70
# File 'lib/dry/effects/providers/resolve.rb', line 64

def provide?(effect)
  if super
    !effect.name.equal?(:resolve) || key?(effect.payload[0])
  else
    false
  end
end

#representString

Returns:

API:

  • public



81
82
83
84
# File 'lib/dry/effects/providers/resolve.rb', line 81

def represent
  containers = [represent_container(static), represent_container(dynamic)].compact.join("+")
  "resolve[#{containers.empty? ? "empty" : containers}]"
end

#represent_container(container) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

API:

  • private



88
89
90
91
92
93
94
95
96
97
# File 'lib/dry/effects/providers/resolve.rb', line 88

def represent_container(container)
  case container
  when ::Hash
    container.empty? ? nil : "hash"
  when ::Class
    container.name || container.to_s
  else
    container.to_s
  end
end

#resolve(key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dry/effects/providers/resolve.rb', line 26

def resolve(key)
  if parent&.key?(key)
    parent.resolve(key)
  elsif dynamic.key?(key)
    dynamic[key]
  elsif static.key?(key)
    static[key]
  else
    Instructions.Raise(Errors::ResolutionError.new(key))
  end
end