Skip to content

Commit

Permalink
Add WorkflowBase base class for ItemProcessor/Workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Jun 10, 2024
1 parent 1bdc3f8 commit 4e4bb5e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/floe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require_relative "floe/runner"

require_relative "floe/workflow_base"
require_relative "floe/workflow"
require_relative "floe/workflow/catcher"
require_relative "floe/workflow/choice_rule"
Expand Down
2 changes: 1 addition & 1 deletion lib/floe/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "json"

module Floe
class Workflow
class Workflow < Floe::WorkflowBase
include Logging

class << self
Expand Down
14 changes: 3 additions & 11 deletions lib/floe/workflow/item_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@

module Floe
class Workflow
class ItemProcessor
attr_reader :processor_config, :payload, :states, :start_at
class ItemProcessor < Floe::WorkflowBase
attr_reader :processor_config

def initialize(payload, name = nil)
@payload = payload
@name = name

raise Floe::InvalidWorkflowError, "Missing field \"States\" for state [#{name}]" if payload["States"].nil?
raise Floe::InvalidWorkflowError, "Missing field \"StartAt\" for state [#{name}]" if payload["StartAt"].nil?
raise Floe::InvalidWorkflowError, "\"StartAt\" not in the \"States\" field for state [#{name}]" unless payload["States"].key?(payload["StartAt"])

super
@processor_config = payload.fetch("ProcessorConfig", "INLINE")
@states = payload["States"].to_a.map { |state_name, state| State.build!(self, state_name, state) }
@states_by_name = @states.to_h { |state| [state.name, state] }
end

def value(_context, input = {})
Expand Down
19 changes: 19 additions & 0 deletions lib/floe/workflow_base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Floe
class WorkflowBase
attr_reader :payload, :states, :start_at

def initialize(payload, name = nil)
@payload = payload
@name = name

raise Floe::InvalidWorkflowError, "Missing field \"States\" for state [#{name}]" if payload["States"].nil?
raise Floe::InvalidWorkflowError, "Missing field \"StartAt\" for state [#{name}]" if payload["StartAt"].nil?
raise Floe::InvalidWorkflowError, "\"StartAt\" not in the \"States\" field for state [#{name}]" unless payload["States"].key?(payload["StartAt"])

@states = payload["States"].to_a.map { |state_name, state| Floe::Workflow::State.build!(self, state_name, state) }
@states_by_name = @states.to_h { |state| [state.name, state] }
end
end
end

0 comments on commit 4e4bb5e

Please sign in to comment.