Module: FlowMachine::FactoryMethods

Included in:
Workflow
Defined in:
lib/flow_machine/workflow/factory_methods.rb

Instance Method Summary collapse

Instance Method Details

#class_for(object_or_class) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/flow_machine/workflow/factory_methods.rb', line 19

def class_for(object_or_class)
  if object_or_class.is_a? Class
    "#{object_or_class.name}Workflow".constantize
  else
    class_for(object_or_class.class)
  end
rescue NameError # if the workflow class doesn't exist
  nil
end

#for(object, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/flow_machine/workflow/factory_methods.rb', line 4

def for(object, options = {})
  # If the object is enumerable, delegate. This allows workflow_for
  # as shorthand
  return for_collection(object, options) if object.respond_to?(:map)

  klazz = class_for(object)
  return nil unless klazz

  klazz.new(object, options)
end

#for_collection(collection, options = {}) ⇒ Object



15
16
17
# File 'lib/flow_machine/workflow/factory_methods.rb', line 15

def for_collection(collection, options = {})
  collection.map { |item| self.for(item, options) }
end