Class: BehaveFun::TaskBuilderFactory::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/behave_fun/task_builder_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory, control) ⇒ Builder

Returns a new instance of Builder.



84
85
86
87
# File 'lib/behave_fun/task_builder_factory.rb', line 84

def initialize(factory, control)
  @factory = factory
  @control = control
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/behave_fun/task_builder_factory.rb', line 122

def method_missing(name, *args, &block)
  type = factory.tasks[name.to_sym]
  if type
    add_task(type, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



82
83
84
# File 'lib/behave_fun/task_builder_factory.rb', line 82

def control
  @control
end

#factoryObject (readonly)

Returns the value of attribute factory.



82
83
84
# File 'lib/behave_fun/task_builder_factory.rb', line 82

def factory
  @factory
end

Instance Method Details

#add_task(type, params = {}, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/behave_fun/task_builder_factory.rb', line 89

def add_task(type, params = {}, &block)
  task = type.new(params)
  @control.add_child(task)

  if block
    builder = Builder.new(factory, task)
    builder.instance_eval(&block)
  end
end

#build_from_hash(task_hash) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/behave_fun/task_builder_factory.rb', line 110

def build_from_hash(task_hash)
  type_name, params, guard_with, children =
    task_hash.values_at(:type, :params, :guard_with, :children)
  params ||= {}
  children ||= []
  type = factory.tasks[type_name.to_sym]
  add_task type, **params do
    guard_with { build_from_hash(guard_with) } if guard_with
    children.each { build_from_hash(_1) }
  end
end

#guard_with(&block) ⇒ Object



99
100
101
102
103
# File 'lib/behave_fun/task_builder_factory.rb', line 99

def guard_with(&block)
  builder = Builder.new(factory, Tree.new)
  builder.instance_eval(&block)
  control.guard = builder.control.root
end

#include(task) ⇒ Object



105
106
107
108
# File 'lib/behave_fun/task_builder_factory.rb', line 105

def include(task)
  cloned_task = task.dup
  @control.add_child(cloned_task)
end