Class: Dry::Core::Container::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/core/container/item.rb,
lib/dry/core/container/item/factory.rb,
lib/dry/core/container/item/callable.rb,
lib/dry/core/container/item/memoizable.rb

Overview

Base class to abstract Memoizable and Callable implementations

Direct Known Subclasses

Callable, Memoizable

Defined Under Namespace

Classes: Callable, Factory, Memoizable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, options = {}) ⇒ Item

Returns a new instance of Item.



18
19
20
21
22
23
# File 'lib/dry/core/container/item.rb', line 18

def initialize(item, options = {})
  @item = item
  @options = {
    call: item.is_a?(::Proc) && item.parameters.empty?
  }.merge(options)
end

Instance Attribute Details

#itemMixed (readonly)

Returns the item to be solved later.

Returns:

  • (Mixed)

    the item to be solved later



12
13
14
# File 'lib/dry/core/container/item.rb', line 12

def item
  @item
end

#optionsHash (readonly)

Returns the options to memoize, call or no.

Returns:

  • (Hash)

    the options to memoize, call or no.



15
16
17
# File 'lib/dry/core/container/item.rb', line 15

def options
  @options
end

Instance Method Details

#callObject

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/dry/core/container/item.rb', line 26

def call
  raise NotImplementedError
end

#callable?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dry/core/container/item.rb', line 36

def callable?
  options[:call]
end

#map(func) ⇒ Object

Build a new item with transformation applied



43
44
45
46
47
48
49
# File 'lib/dry/core/container/item.rb', line 43

def map(func)
  if callable?
    self.class.new(-> { func.(item.call) }, options)
  else
    self.class.new(func.(item), options)
  end
end

#value?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/dry/core/container/item.rb', line 31

def value?
  !callable?
end