lib/eco/api/session/config/workflow.rb in eco-helpers-1.1.8 vs lib/eco/api/session/config/workflow.rb in eco-helpers-1.2.1

- old
+ new

@@ -1,58 +1,44 @@ module Eco module API class Session class Config class Workflow - extend Eco::API::Common::ClassHelpers + extend Eco::API::Common::ClassHierarchy - @workflow = { - options: nil, - load: {input: nil, people: {get: nil, filter: nil}, filter: nil}, - usecases: nil, - launch_jobs: nil, - post_launch: {usecases: nil, launch_jobs: nil}, - end: nil, - close: nil - } + WORKFLOW_MODEL = [ + :options, + {load: [:input, {people: [:get, :filter]}, :filter]}, + :usecases, :launch_jobs, + {post_launch: [:usecases, :launch_jobs]}, + :end, :close + ] class << self def stages - @stages ||= (@workflow || {}).keys + model_attrs end + def model + super || {} + end + def validate_stage(stage) "Unknown Workflow stage '#{stage}'. Should be any of #{stages}" unless stages.include?(stage) end - def titleize(key) - str_name = key.to_s.strip.split(/[\-\_ ]/i).compact.map do |str| - str.slice(0).upcase + str.slice(1..-1).downcase - end.join("") - end + def workflow_class(key) + class_name = to_constant(key.to_s) - def get_model(key) - raise "Expected Symbol. Given: #{key.class}" unless key.is_a?(Symbol) - workflow = @workflow[key] - raise "Expected Hash. Given #{model.class}" unless !workflow || workflow.is_a?(Hash) - - class_name = titleize(key.to_s) - full_class_name = "#{self}::#{class_name}" - - if target_class = resolve_class(full_class_name, exception: false) - else - target_class = Class.new(Eco::API::Session::Config::Workflow) do - @workflow = workflow - end - self.const_set class_name, target_class + new_class(class_name, inherits: Eco::API::Session::Config::Workflow) do |klass| + klass.model = model[key] end - - target_class end end + self.model = WORKFLOW_MODEL attr_reader :config attr_reader :name def initialize(name = nil, _parent: self, config:) @config = config @@ -199,10 +185,11 @@ # - it will rather `yield` the target stage after all the `before` _callbacks_ have been run # - aside of this, the rest will be the same as when the _block_ is provided (see previous note) # @param key [Symbol, nil] cases: # - if `key` is not provided, it targets the _current stage_ # - if `key` is provided, it targets the specific _sub-stage_ + # @param io [Eco::API::UseCases::BaseIO] the input/output object # @yield [stage_workflow, io] if a `block` is provided, see `note` # @yieldparam stage_workflow [Eco::API::Session::Config::Workflow] the _target stage_ referred by `key` # @yieldparam io [Eco::API::UseCases::BaseIO] the input/output object carried througout all the _workflow_ # @yieldreturn io [Eco::API::UseCases::BaseIO] the input/output object carried througout all the _workflow_ # @return [Eco::API::Session::Config::Workflow] the current stage object (to ease chainig). @@ -271,10 +258,10 @@ existing_stages.all? {|s| s.ready?} end def stage(key) self.class.validate_stage(key) - @stages[key] ||= self.class.get_model(key).new(key, _parent: self, config: config) + @stages[key] ||= self.class.workflow_class(key).new(key, _parent: self, config: config) end end end end