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



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

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:

  • key (Symbol, String)

    Dependency key

Returns:

  • (Boolean)


77
78
79
# File 'lib/dry/effects/providers/resolve.rb', line 77

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:



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

def locate
  self
end

#provide?(effect) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

#representString

Returns:

  • (String)


83
84
85
86
# File 'lib/dry/effects/providers/resolve.rb', line 83

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:

  • (String)


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

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