2017-08-14 11:56:23 +00:00
|
|
|
class Sequencer
|
2017-09-01 12:28:06 +00:00
|
|
|
class Units < SimpleDelegator
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2017-09-01 12:28:06 +00:00
|
|
|
# Initializes the lifespan store for the attributes
|
|
|
|
# of the given Units declarations.
|
|
|
|
#
|
|
|
|
# @param [Array<Hash{Symbol => Array<:Symbol>}>] declarations the list of Unit declarations.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# declarations = [{uses: [:attribute1, ...], provides: [:result], ...}]
|
|
|
|
# attributes = Sequencer::Units::Attributes(declarations)
|
|
|
|
class Attributes < Delegator
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
# Lists all `provides` declarations of the Units the instance was initialized with.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# attributes.provided
|
|
|
|
# # => [:result, ...]
|
|
|
|
#
|
|
|
|
# @return [Array<Symbol>] the list of all `provides` declarations
|
|
|
|
def provided
|
|
|
|
select do |_attribute, instance|
|
|
|
|
instance.will_be_provided?
|
|
|
|
end.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
# Lists all `uses` declarations of the Units the instance was initialized with.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# attributes.used
|
|
|
|
# # => [:attribute1, ...]
|
|
|
|
#
|
|
|
|
# @return [Array<Symbol>] the list of all `uses` declarations
|
|
|
|
def used
|
|
|
|
select do |_attribute, instance|
|
|
|
|
instance.will_be_used?
|
|
|
|
end.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if the given attribute is known in the list of Unit declarations.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# attributes.known?(:attribute2)
|
|
|
|
# # => false
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def known?(attribute)
|
|
|
|
key?(attribute)
|
|
|
|
end
|
|
|
|
|
2017-09-01 12:28:06 +00:00
|
|
|
def __getobj__
|
|
|
|
@attributes
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
2017-09-01 12:28:06 +00:00
|
|
|
def __setobj__(declarations)
|
2018-04-12 14:57:37 +00:00
|
|
|
@attributes ||= begin # rubocop:disable Naming/MemoizedInstanceVariableName
|
2017-09-01 12:28:06 +00:00
|
|
|
attributes = Hash.new do |hash, key|
|
|
|
|
hash[key] = Sequencer::Units::Attribute.new
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
2017-09-01 12:28:06 +00:00
|
|
|
attributes.tap do |result|
|
|
|
|
|
|
|
|
declarations.each_with_index do |unit, index|
|
|
|
|
|
|
|
|
unit[:uses].try(:each) do |attribute|
|
|
|
|
result[attribute].to = index
|
|
|
|
end
|
|
|
|
|
|
|
|
unit[:provides].try(:each) do |attribute|
|
|
|
|
next if result[attribute].will_be_provided?
|
|
|
|
result[attribute].from = index
|
|
|
|
end
|
|
|
|
end
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|