Class: Dry::Core::Container::Item
- Inherits:
-
Object
- Object
- Dry::Core::Container::Item
- 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
Defined Under Namespace
Classes: Callable, Factory, Memoizable
Constant Summary collapse
- NO_OPTIONS =
{}.freeze
Instance Attribute Summary collapse
-
#item ⇒ Mixed
readonly
The item to be solved later.
-
#options ⇒ Hash
readonly
The options to memoize, call or no.
Instance Method Summary collapse
- #call ⇒ Object
- #callable? ⇒ Boolean
-
#initialize(item, options = NO_OPTIONS) ⇒ Item
constructor
A new instance of Item.
-
#map(func) ⇒ Object
Build a new item with transformation applied.
- #value? ⇒ Boolean
Constructor Details
#initialize(item, options = NO_OPTIONS) ⇒ Item
Returns a new instance of Item.
20 21 22 23 24 25 26 |
# File 'lib/dry/core/container/item.rb', line 20 def initialize(item, = NO_OPTIONS) @item = item @options = { call: item.is_a?(::Proc) && item.parameters.empty?, ** } end |
Instance Attribute Details
#item ⇒ Mixed (readonly)
Returns the item to be solved later.
14 15 16 |
# File 'lib/dry/core/container/item.rb', line 14 def item @item end |
#options ⇒ Hash (readonly)
Returns the options to memoize, call or no.
17 18 19 |
# File 'lib/dry/core/container/item.rb', line 17 def @options end |
Instance Method Details
#call ⇒ Object
29 30 31 |
# File 'lib/dry/core/container/item.rb', line 29 def call raise ::NotImplementedError end |
#callable? ⇒ Boolean
39 40 41 |
# File 'lib/dry/core/container/item.rb', line 39 def callable? [:call] end |
#map(func) ⇒ Object
Build a new item with transformation applied
46 47 48 49 50 51 52 |
# File 'lib/dry/core/container/item.rb', line 46 def map(func) if callable? self.class.new(-> { func.(item.call) }, ) else self.class.new(func.(item), ) end end |
#value? ⇒ Boolean
34 35 36 |
# File 'lib/dry/core/container/item.rb', line 34 def value? !callable? end |