state.rb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::State
  3. include ::Mixin::RailsLogger
  4. include ::Mixin::StartFinishLogger
  5. def initialize(sequence, parameters: {}, expecting: nil)
  6. @index = -1
  7. @units = sequence.units
  8. @result_index = @units.count
  9. @values = {}
  10. initialize_attributes(sequence.units)
  11. initialize_parameters(parameters)
  12. initialize_expectations(expecting || sequence.expecting)
  13. end
  14. # Stores a value for the given attribute. Value can be a regular object
  15. # or the result of a given code block.
  16. # The attribute gets validated against the .provides list of attributes.
  17. # In the case than an attribute gets provided that is not declared to
  18. # be provided an exception will be raised.
  19. #
  20. # @param [Symbol] attribute the attribute for which the value gets provided.
  21. # @param [Object] value the value that should get stored for the given attribute.
  22. # @yield [] executes the given block and takes the result as the value.
  23. # @yieldreturn [Object] the value for the given attribute.
  24. #
  25. # @example
  26. # state.provide(:sum, 3)
  27. #
  28. # @example
  29. # state.provide(:sum) do
  30. # some_value = ...
  31. # some_value * 3
  32. # end
  33. #
  34. # @raise [RuntimeError] if the attribute is not provideable from the calling Unit
  35. #
  36. # @return [nil]
  37. def provide(attribute, value = nil)
  38. if provideable?(attribute)
  39. value = yield if block_given?
  40. set(attribute, value)
  41. else
  42. value = "UNEXECUTED BLOCK: #{caller(1..1).first}" if block_given?
  43. unprovideable_setter(attribute, value)
  44. end
  45. end
  46. # Returns the value of the given attribute.
  47. # The attribute gets validated against the .uses and .optionals
  48. # lists of attributes. In the case that an attribute gets used
  49. # that is not declared to be used or optional, an exception
  50. # gets raised.
  51. #
  52. # @param [Symbol] attribute the attribute for which the value is requested.
  53. #
  54. # @example
  55. # state.use(:answer)
  56. # #=> 42
  57. #
  58. # @raise [RuntimeError] if the attribute is not useable from the calling Unit
  59. #
  60. # @return [nil]
  61. def use(attribute)
  62. if useable?(attribute)
  63. get(attribute)
  64. else
  65. unaccessable_getter(attribute)
  66. end
  67. end
  68. # Returns the value of the given attribute.
  69. # The attribute DOES NOT get validated against the .uses list of attributes.
  70. # Use this method only in edge cases and prefer .optional macro and state.use otherwise.
  71. #
  72. # @param [Symbol] attribute the attribute for which the value is requested.
  73. #
  74. # @example
  75. # state.optional(:answer)
  76. # #=> 42
  77. #
  78. # @example
  79. # state.optional(:unknown)
  80. # #=> nil
  81. #
  82. # @return [Object, nil]
  83. def optional(attribute)
  84. return get(attribute) if @attributes.known?(attribute)
  85. logger.public_send(log_level[:optional]) { "Access to unknown optional attribute '#{attribute}'." }
  86. nil
  87. end
  88. # Checks if a value for the given attribute is provided.
  89. # The attribute DOES NOT get validated against the .uses list of attributes.
  90. # Use this method only in edge cases and prefer .optional macro and state.use otherwise.
  91. #
  92. # @param [Symbol] attribute the attribute which should get checked.
  93. #
  94. # @example
  95. # state.provided?(:answer)
  96. # #=> true
  97. #
  98. # @example
  99. # state.provided?(:unknown)
  100. # #=> false
  101. #
  102. # @return [Boolean]
  103. def provided?(attribute)
  104. optional(attribute) != nil
  105. end
  106. # Unsets the value for the given attribute.
  107. # The attribute gets validated against the .uses list of attributes.
  108. # In the case than an attribute gets unset that is not declared
  109. # to be used an exception will be raised.
  110. #
  111. # @param [Symbol] attribute the attribute for which the value gets unset.
  112. #
  113. # @example
  114. # state.unset(:answer)
  115. #
  116. # @raise [RuntimeError] if the attribute is not useable from the calling Unit
  117. #
  118. # @return [nil]
  119. def unset(attribute)
  120. value = nil
  121. if useable?(attribute)
  122. set(attribute, value)
  123. else
  124. unprovideable_setter(attribute, value)
  125. end
  126. end
  127. # Handles state processing of the next Unit in the Sequence while executing
  128. # the given block. After the Unit is processed the state will get cleaned up
  129. # and no longer needed attribute values will get discarded.
  130. #
  131. # @yield [] executes the given block and handles the state changes before and afterwards.
  132. #
  133. # @example
  134. # state.process do
  135. # unit.process
  136. # end
  137. #
  138. # @return [nil]
  139. def process
  140. @index += 1
  141. yield
  142. cleanup
  143. end
  144. # Handles state processing of the next Unit in the Sequence while executing
  145. # the given block. After the Unit is processed the state will get cleaned up
  146. # and no longer needed attribute values will get discarded.
  147. #
  148. # @example
  149. # state.to_h
  150. # #=> {"ssl_verify"=>true, "host"=>"192...", ...}
  151. #
  152. # @return [Hash{Symbol => Object}]
  153. def to_h
  154. available.index_with { |identifier| @values[identifier] }
  155. end
  156. private
  157. def available
  158. @attributes.select do |_identifier, attribute|
  159. @index.between?(attribute.from, attribute.till)
  160. end.keys
  161. end
  162. def unit(index = nil)
  163. @units[index || @index]
  164. end
  165. def provideable?(attribute)
  166. unit.provides.include?(attribute)
  167. end
  168. def useable?(attribute)
  169. return true if unit.uses.include?(attribute)
  170. unit.optional.include?(attribute)
  171. end
  172. def set(attribute, value)
  173. logger.public_send(log_level[:set]) { "Setting '#{attribute}' value (#{value.class.name}): #{value.inspect}" }
  174. @values[attribute] = value
  175. end
  176. def get(attribute)
  177. value = @values[attribute]
  178. logger.public_send(log_level[:get]) { "Getting '#{attribute}' value (#{value.class.name}): #{value.inspect}" }
  179. value
  180. end
  181. def unprovideable_setter(attribute, value)
  182. message = "Unprovideable attribute '#{attribute}' set with value (#{value.class.name}): #{value.inspect}"
  183. logger.error(message)
  184. raise message
  185. end
  186. def unaccessable_getter(attribute)
  187. message = "Unaccessable getter used for attribute '#{attribute}'"
  188. logger.error(message)
  189. raise message
  190. end
  191. def initialize_attributes(units)
  192. log_start_finish(log_level[:attribute_initialization][:start_finish], 'Attributes lifespan initialization') do
  193. @attributes = Sequencer::Units::Attributes.new(units.declarations)
  194. logger.public_send(log_level[:attribute_initialization][:attributes]) { "Attributes lifespan: #{@attributes.inspect}" }
  195. end
  196. end
  197. def initialize_parameters(parameters)
  198. logger.public_send(log_level[:parameter_initialization][:parameters]) { "Initializing Sequencer::State with initial parameters: #{parameters.inspect}" }
  199. log_start_finish(log_level[:parameter_initialization][:start_finish], 'Attribute value provisioning check and initialization') do
  200. @attributes.each do |identifier, attribute|
  201. if !attribute.will_be_used?
  202. logger.public_send(log_level[:parameter_initialization][:unused]) { "Attribute '#{identifier}' is provided by Unit(s) but never used." }
  203. next
  204. end
  205. init_param = parameters.key?(identifier)
  206. provided_attr = attribute.will_be_provided?
  207. if !init_param && !provided_attr
  208. next if attribute.optional?
  209. message = "Attribute '#{identifier}' is used in Unit '#{unit(attribute.to).name}' (index: #{attribute.to}) but is not provided or given via initial parameters."
  210. logger.error(message)
  211. raise message
  212. end
  213. # skip if attribute is provided by an Unit but not
  214. # an initial parameter
  215. next if !init_param
  216. # update 'from' lifespan information for attribute
  217. # since it's provided via the initial parameter
  218. attribute.from = @index
  219. # set initial value
  220. set(identifier, parameters[identifier])
  221. end
  222. end
  223. end
  224. def initialize_expectations(expected_attributes)
  225. expected_attributes.each do |identifier|
  226. logger.public_send(log_level[:expectations_initialization]) { "Adding attribute '#{identifier}' to the list of expected result attributes." }
  227. @attributes[identifier].to = @result_index
  228. end
  229. end
  230. def cleanup
  231. log_start_finish(log_level[:cleanup][:start_finish], "State cleanup of Unit #{unit.name} (index: #{@index})") do
  232. @attributes.delete_if do |identifier, attribute|
  233. remove = !attribute.will_be_used?
  234. remove ||= attribute.till <= @index
  235. if remove && attribute.will_be_used?
  236. logger.public_send(log_level[:cleanup][:remove]) { "Removing unneeded attribute '#{identifier}': #{@values[identifier].inspect}" }
  237. end
  238. remove
  239. end
  240. end
  241. end
  242. def log_level
  243. @log_level ||= Sequencer.log_level_for(:state)
  244. end
  245. end