Module: Controll::Flow::Master::Macros::ClassMethods

Defined in:
lib/controll/flow/master/macros.rb

Instance Method Summary collapse

Instance Method Details

#event(&block) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
# File 'lib/controll/flow/master/macros.rb', line 48

def event &block
  raise ArgumentError, "Must be called with a block" unless block_given?
  define_method :event do
    instance_variable_get("@event") || instance_variable_set("@event", instance_eval(&block))
  end
end

#handler(options = {}, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/controll/flow/master/macros.rb', line 7

def handler options = {}, &block
  mapper_type = :simple if options[:simple]
  mapper_type ||= :complex if options[:complex]

  master_clazz = Controll::Flow::Master

  unless mapper_type
    raise ArgumentError, "You must specify mapper type, one of: #{master_clazz.mapper_types} in: #{options}" 
  end

  handler_type = options.delete(mapper_type)

  unless master_clazz.valid_handler? handler_type
    raise ArgumentError, "Must one of: #{master_clazz.valid_handlers} was: #{handler_type}"
  end
  
  parent = "Controll::Flow::ActionMapper::#{mapper_type.to_s.camelize}".constantize

  clazz_name = handler_type.to_s.camelize
  context = self.kind_of?(Class) ? self : self.class

  clazz = parent ? Class.new(parent) : Class.new
  context.const_set clazz_name, clazz          
  clazz = context.const_get(clazz_name)

  container_class_name = clazz.name.sub(/\.*(::\w+)$/, '')          
  container_class = container_class_name.constantize
  container_class.add_action_handler clazz.name.demodulize

  clazz.instance_eval(&block) if block_given?
  clazz
end

#redirecter(mapper_type = :complex, options = {}, &block) ⇒ Object



44
45
46
# File 'lib/controll/flow/master/macros.rb', line 44

def redirecter mapper_type = :complex, options = {}, &block
  handler options.merge(mapper_type => :redirecter), &block
end

#renderer(mapper_type = :simple, options = {}, &block) ⇒ Object



40
41
42
# File 'lib/controll/flow/master/macros.rb', line 40

def renderer mapper_type = :simple, options = {}, &block
  handler options.merge(mapper_type => :renderer), &block
end