1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
module Dry
class Container
# Base class to abstract Memoizable and Callable implementations
#
# @api abstract
#
class Item
# @return [Mixed] the item to be solved later
attr_reader :item
# @return [Hash] the options to memoize, call or no.
attr_reader :options
# @api abstract
def initialize(item, options = {})
@item = item
@options = {
call: item.is_a?(::Proc) && item.parameters.empty?
}.merge(options)
end
# @api abstract
def call
raise NotImplementedError
end
# @private
def value?
!callable?
end
# @private
def callable?
options[:call]
end
# Build a new item with transformation applied
#
# @private
def map(func)
if callable?
self.class.new(-> { func.(item.call) }, options)
else
self.class.new(func.(item), options)
end
end
end
end
end
|