Module: RSpec::ActionCheck::Helpers::ClassMethods

Defined in:
lib/rspec/actioncheck/helpers.rb

Instance Method Summary collapse

Instance Method Details

#__action_dagsObject



8
9
10
# File 'lib/rspec/actioncheck/helpers.rb', line 8

def __action_dags
  {}
end

#__before_action_nameObject



5
6
7
# File 'lib/rspec/actioncheck/helpers.rb', line 5

def __before_action_name
  :root
end

#_update_action_dags(name, action_description, before_action_name, action_block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rspec/actioncheck/helpers.rb', line 76

def _update_action_dags(name, action_description, before_action_name, action_block)

  _action_dags = __action_dags
  _action_dags[name] = {
    forwards: [],
    backwards: [],
    examples: [],
  } if _action_dags[name].nil?
  _action_dags[before_action_name] = {
    forwards: [],
    backwards: [],
    examples: [],
  } if _action_dags[before_action_name].nil?
  _action_dags[name].merge!({
    forwards: _action_dags[name][:forwards],
    backwards: _action_dags[name][:backwards] | [before_action_name],
    action: {description: "action:#{description}", block: action_block},
  })
  _action_dags[before_action_name].merge!({
    forwards: _action_dags[before_action_name][:forwards] | [name],
    backwards: _action_dags[before_action_name][:backwards],
  })
  self.define_singleton_method(:__action_dags) do ||
    _action_dags
  end
end

#_update_before_action_name(name) ⇒ Object



103
104
105
106
107
108
# File 'lib/rspec/actioncheck/helpers.rb', line 103

def _update_before_action_name(name)
  define_singleton_method(:__before_action_name) do ||
    name
  end

end

#action(description, &action_block) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/rspec/actioncheck/helpers.rb', line 54

def action(description, &action_block)
  before_action_name = __before_action_name
  name = "#{description.gsub(/ /, "_")}_#{__action_dags.size}".to_sym

  _update_before_action_name(name)

  _update_action_dags(name, description, before_action_name, action_block)
end

#actions(description, &actions_block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rspec/actioncheck/helpers.rb', line 28

def actions(description, &actions_block)
  context description do
    before_action_name = __before_action_name
    self.module_exec(&actions_block)
    if __action_dags.empty?
      pending 'actions is empty'
      return
    end
    node = __action_dags[before_action_name]

    self.module_exec(&create_from_dag(node))
  end
end

#branch(description, &branch_block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rspec/actioncheck/helpers.rb', line 42

def branch(description, &branch_block)
  name = "#{description.gsub(/ /, "_")}_#{__action_dags.size}".to_sym
  before_action_name = __before_action_name

  _update_action_dags(name, description, before_action_name, Proc.new{})

  context do
    _update_before_action_name(name)
    self.module_exec(&branch_block)
  end
end

#check(description, &action_block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rspec/actioncheck/helpers.rb', line 63

def check(description, &action_block)
  check_action_name =  __before_action_name

  _action_dags = __action_dags
  _action_dags[check_action_name][:examples] << {
    description: description,
    block: action_block,
  }
  define_singleton_method(:__action_dags) do ||
    _action_dags
  end
end

#create_from_dag(node) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec/actioncheck/helpers.rb', line 12

def create_from_dag(node)
  Proc.new do ||
    node[:forwards].each do |next_node_name|
    next_node = __action_dags[next_node_name]
    context next_node[:action][:description] do
      before(&next_node[:action][:block])
      next_node[:examples].each do |example_info|
        example("check:#{example_info[:description]}", &example_info[:block])
      end
      context_proc = create_from_dag(next_node)
      self.module_exec(&context_proc) if context_proc
    end
  end
  end
end