Class: BehaveFun::TaskBuilderFactory

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

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ TaskBuilderFactory

Returns a new instance of TaskBuilderFactory.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/behave_fun/task_builder_factory.rb', line 5

def initialize(&block)
  @tasks = {}

  add_task_type BehaveFun::LeafTasks::Success
  add_task_type BehaveFun::LeafTasks::Failure
  add_task_type BehaveFun::LeafTasks::Wait
  add_task_type BehaveFun::Decorators::AlwaysSucceed
  add_task_type BehaveFun::Decorators::AlwaysFail
  add_task_type BehaveFun::Decorators::UntilSuccess
  add_task_type BehaveFun::Decorators::UntilFail
  add_task_type BehaveFun::Decorators::Invert
  add_task_type BehaveFun::Decorators::Repeat
  add_task_type BehaveFun::BranchTasks::Sequence
  add_task_type BehaveFun::BranchTasks::Selector
  add_task_type BehaveFun::BranchTasks::RandomSequence
  add_task_type BehaveFun::BranchTasks::RandomSelector
  add_task_type BehaveFun::BranchTasks::DynamicGuardSelector

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks.



3
4
5
# File 'lib/behave_fun/task_builder_factory.rb', line 3

def tasks
  @tasks
end

Instance Method Details

#add_lambda_task_type(task_name, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/behave_fun/task_builder_factory.rb', line 30

def add_lambda_task_type(task_name, &block)
  type = Class.new(Task) do
    def name
      task_name
    end

    define_method(:execute, &block)
  end
  tasks[task_name.to_sym] = type
end

#add_task_type(type, name: type.task_name) ⇒ Object



26
27
28
# File 'lib/behave_fun/task_builder_factory.rb', line 26

def add_task_type(type, name: type.task_name)
  tasks[name.to_sym] = type
end

#as_json(tree) ⇒ Object



73
74
75
# File 'lib/behave_fun/task_builder_factory.rb', line 73

def as_json(tree)
  tree.as_json
end

#build_task(&block) ⇒ Object



41
42
43
44
45
# File 'lib/behave_fun/task_builder_factory.rb', line 41

def build_task(&block)
  task = build_tree(&block).root
  task.control = nil
  task
end

#build_task_from_hash(hash) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/behave_fun/task_builder_factory.rb', line 53

def build_task_from_hash(hash)
  hash = hash.deep_symbolize_keys
  tree = build_tree do
    build_from_hash(hash)
  end
  tree.root
end

#build_tree(&block) ⇒ Object



47
48
49
50
51
# File 'lib/behave_fun/task_builder_factory.rb', line 47

def build_tree(&block)
  builder = Builder.new(self, Tree.new)
  builder.instance_eval(&block)
  builder.control
end

#build_tree_from_hash(hash) ⇒ Object



61
62
63
64
65
66
# File 'lib/behave_fun/task_builder_factory.rb', line 61

def build_tree_from_hash(hash)
  hash = hash.deep_symbolize_keys
  build_tree do
    build_from_hash(hash[:root])
  end
end

#build_tree_from_json(json) ⇒ Object



68
69
70
71
# File 'lib/behave_fun/task_builder_factory.rb', line 68

def build_tree_from_json(json)
  hash = ActiveSupport::JSON.decode(json).deep_symbolize_keys
  build_tree_from_hash(hash)
end

#to_json(tree) ⇒ Object



77
78
79
# File 'lib/behave_fun/task_builder_factory.rb', line 77

def to_json(tree)
  ActiveSupport::JSON.encode(as_json(tree))
end