123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- require_dependency 'mixin/rails_logger'
- require_dependency 'mixin/start_finish_logger'
- class Sequencer
- class State
- include ::Mixin::RailsLogger
- include ::Mixin::StartFinishLogger
- def initialize(sequence, parameters: {}, expecting: nil)
- @index = -1
- @units = sequence.units
- @result_index = @units.count
- @values = {}
- initialize_attributes(sequence.units)
- initialize_parameters(parameters)
- initialize_expectations(expecting || sequence.expecting)
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def provide(attribute, value = nil)
- if provideable?(attribute)
- value = yield if block_given?
- set(attribute, value)
- else
- value = "UNEXECUTED BLOCK: #{caller(1..1).first}" if block_given?
- unprovideable_setter(attribute, value)
- end
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def use(attribute)
- if useable?(attribute)
- get(attribute)
- else
- unaccessable_getter(attribute)
- end
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def optional(attribute)
- return get(attribute) if @attributes.known?(attribute)
- logger.public_send(log_level[:optional]) { "Access to unknown optional attribute '#{attribute}'." }
- nil
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def provided?(attribute)
- optional(attribute) != nil
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
- def unset(attribute)
- value = nil
- if useable?(attribute)
- set(attribute, value)
- else
- unprovideable_setter(attribute, value)
- end
- end
-
-
-
-
-
-
-
-
-
-
-
-
- def process
- @index += 1
- yield
- cleanup
- end
-
-
-
-
-
-
-
-
-
- def to_h
- available.map { |identifier| [identifier, @values[identifier]] }.to_h
- end
- private
- def available
- @attributes.select do |_identifier, attribute|
- @index.between?(attribute.from, attribute.till)
- end.keys
- end
- def unit(index = nil)
- @units[index || @index]
- end
- def provideable?(attribute)
- unit.provides.include?(attribute)
- end
- def useable?(attribute)
- return true if unit.uses.include?(attribute)
- unit.optional.include?(attribute)
- end
- def set(attribute, value)
- logger.public_send(log_level[:set]) { "Setting '#{attribute}' value (#{value.class.name}):
- @values[attribute] = value
- end
- def get(attribute)
- value = @values[attribute]
- logger.public_send(log_level[:get]) { "Getting '#{attribute}' value (#{value.class.name}):
- value
- end
- def unprovideable_setter(attribute, value)
- message = "Unprovideable attribute '#{attribute}' set with value (#{value.class.name}):
- logger.error(message)
- raise message
- end
- def unaccessable_getter(attribute)
- message = "Unaccessable getter used for attribute '#{attribute}'"
- logger.error(message)
- raise message
- end
- def initialize_attributes(units)
- log_start_finish(log_level[:attribute_initialization][:start_finish], 'Attributes lifespan initialization') do
- @attributes = Sequencer::Units::Attributes.new(units.declarations)
- logger.public_send(log_level[:attribute_initialization][:attributes]) { "Attributes lifespan: #{@attributes.inspect}" }
- end
- end
- def initialize_parameters(parameters)
- logger.public_send(log_level[:parameter_initialization][:parameters]) { "Initializing Sequencer::State with initial parameters:
- log_start_finish(log_level[:parameter_initialization][:start_finish], 'Attribute value provisioning check and initialization') do
- @attributes.each do |identifier, attribute|
- if !attribute.will_be_used?
- logger.public_send(log_level[:parameter_initialization][:unused]) { "Attribute '#{identifier}' is provided by Unit(s) but never used." }
- next
- end
- init_param = parameters.key?(identifier)
- provided_attr = attribute.will_be_provided?
- if !init_param && !provided_attr
- next if attribute.optional?
- message = "Attribute '#{identifier}' is used in Unit '#{unit(attribute.to).name}' (index:
- logger.error(message)
- raise message
- end
-
-
- next if !init_param
-
-
- attribute.from = @index
-
- set(identifier, parameters[identifier])
- end
- end
- end
- def initialize_expectations(expected_attributes)
- expected_attributes.each do |identifier|
- logger.public_send(log_level[:expectations_initialization]) { "Adding attribute '#{identifier}' to the list of expected result attributes." }
- @attributes[identifier].to = @result_index
- end
- end
- def cleanup
- log_start_finish(log_level[:cleanup][:start_finish], "State cleanup of Unit
- @attributes.delete_if do |identifier, attribute|
- remove = !attribute.will_be_used?
- remove ||= attribute.till <= @index
- if remove && attribute.will_be_used?
- logger.public_send(log_level[:cleanup][:remove]) { "Removing unneeded attribute '#{identifier}': #{@values[identifier].inspect}" }
- end
- remove
- end
- end
- end
- def log_level
- @log_level ||= Sequencer.log_level_for(:state)
- end
- end
- end
|