Class: HatTrick::StepDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/hat_trick/step_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ StepDefinition

Returns a new instance of StepDefinition.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hat_trick/step_definition.rb', line 9

def initialize(args={})
  args.each_pair do |k,v|
    setter = "#{k}="
    if respond_to?(setter)
      send(setter,v)
    end
  end
  @callbacks = {}
  @buttons = [
    { next: default_button(:next) },
    { back: default_button(:back) }
  ]
  @skipped ||= false
  @last ||= false
  @first ||= false
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



3
4
5
# File 'lib/hat_trick/step_definition.rb', line 3

def callbacks
  @callbacks
end

#fieldsetObject

Returns the value of attribute fieldset.



4
5
6
# File 'lib/hat_trick/step_definition.rb', line 4

def fieldset
  @fieldset
end

#first=(value) ⇒ Object (writeonly)

Sets the attribute first

Parameters:

  • value

    the value to set the attribute first to.



5
6
7
# File 'lib/hat_trick/step_definition.rb', line 5

def first=(value)
  @first = value
end

#include_data_keyObject (readonly)

Returns the value of attribute include_data_key.



3
4
5
# File 'lib/hat_trick/step_definition.rb', line 3

def include_data_key
  @include_data_key
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/hat_trick/step_definition.rb', line 4

def name
  @name
end

#skipped=(value) ⇒ Object (writeonly)

Sets the attribute skipped

Parameters:

  • value

    the value to set the attribute skipped to.



5
6
7
# File 'lib/hat_trick/step_definition.rb', line 5

def skipped=(value)
  @skipped = value
end

#wizardObject

Returns the value of attribute wizard.



4
5
6
# File 'lib/hat_trick/step_definition.rb', line 4

def wizard
  @wizard
end

Instance Method Details

#add_button(button) ⇒ Object



55
56
57
58
59
60
# File 'lib/hat_trick/step_definition.rb', line 55

def add_button(button)
  @buttons.delete_if do |b|
    b.keys.first == button.keys.first && b[b.keys.first][:default]
  end
  @buttons << button
end

#after_callback=(block) ⇒ Object



114
115
116
# File 'lib/hat_trick/step_definition.rb', line 114

def after_callback=(block)
  callbacks[:after] = block
end

#after_callbacksObject



163
164
165
166
167
168
169
# File 'lib/hat_trick/step_definition.rb', line 163

def after_callbacks
  after_callbacks = [callbacks[:after]]
  if wizard.after_callback_for_all_steps
    after_callbacks.unshift wizard.after_callback_for_all_steps
  end
  after_callbacks
end

#back_buttonObject



42
43
44
# File 'lib/hat_trick/step_definition.rb', line 42

def back_button
  get_button(:back)
end

#before_callback=(block) ⇒ Object



110
111
112
# File 'lib/hat_trick/step_definition.rb', line 110

def before_callback=(block)
  callbacks[:before] = block
end

#before_callbacksObject



143
144
145
146
147
148
149
# File 'lib/hat_trick/step_definition.rb', line 143

def before_callbacks
  before_callbacks = [callbacks[:before]]
  if wizard.before_callback_for_all_steps
    before_callbacks.unshift wizard.before_callback_for_all_steps
  end
  before_callbacks
end

#buttonsObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hat_trick/step_definition.rb', line 26

def buttons
  # We should check for a new translation every time because the user may
  # have changed their desired localization.
  @buttons.map do |b|
    button_type = b.keys.first
    if b[button_type][:default]
      b[button_type][:label] = button_label(button_type)
    end
    b
  end
end

#default_button(type) ⇒ Object



51
52
53
# File 'lib/hat_trick/step_definition.rb', line 51

def default_button(type)
  { :label => button_label(type), :default => true }
end

#delete_button(type) ⇒ Object



62
63
64
# File 'lib/hat_trick/step_definition.rb', line 62

def delete_button(type)
  @buttons.delete_if { |b| b.keys.first == type }
end

#first?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/hat_trick/step_definition.rb', line 95

def first?
  @first
end

#get_button(type) ⇒ Object



46
47
48
49
# File 'lib/hat_trick/step_definition.rb', line 46

def get_button(type)
  button = buttons.detect { |b| b.keys.first == type.to_sym }
  button[type.to_sym] unless button.nil?
end

#include_data(context, model) ⇒ Object



136
137
138
139
140
141
# File 'lib/hat_trick/step_definition.rb', line 136

def include_data(context, model)
  inc_data = run_include_data_callback(context, model)
  return {} unless inc_data.respond_to?(:as_json)
  key = include_data_key.to_s.camelize(:lower)
  { key => camelize_hash_keys(inc_data) }
end

#include_data=(hash) ⇒ Object



118
119
120
121
# File 'lib/hat_trick/step_definition.rb', line 118

def include_data=(hash)
  callbacks[:include_data] = hash.values.first
  @include_data_key = hash.keys.first
end

#last=(_last) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/hat_trick/step_definition.rb', line 82

def last=(_last)
  @last = _last
  if @last
    @buttons.delete_if { |b| b.keys.include?(:next) }
  else
    @buttons << { :next => default_next_button }
  end
end

#last?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/hat_trick/step_definition.rb', line 91

def last?
  @last
end

#next_buttonObject



38
39
40
# File 'lib/hat_trick/step_definition.rb', line 38

def next_button
  get_button(:next)
end

#run_after_callback(context, model) ⇒ Object



171
172
173
# File 'lib/hat_trick/step_definition.rb', line 171

def run_after_callback(context, model)
  run_callbacks(after_callbacks, context, model)
end

#run_before_callback(context, model) ⇒ Object



151
152
153
# File 'lib/hat_trick/step_definition.rb', line 151

def run_before_callback(context, model)
  run_callbacks(before_callbacks, context, model)
end

#run_include_data_callback(context, model) ⇒ Object



155
156
157
# File 'lib/hat_trick/step_definition.rb', line 155

def run_include_data_callback(context, model)
  run_callbacks([callbacks[:include_data]], context, model)
end

#run_step_contents_callback(context, model) ⇒ Object



159
160
161
# File 'lib/hat_trick/step_definition.rb', line 159

def run_step_contents_callback(context, model)
  run_callbacks([callbacks[:step_contents]], context, model)
end

#skipped?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/hat_trick/step_definition.rb', line 78

def skipped?
  @skipped
end

#step_contents(context, model) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/hat_trick/step_definition.rb', line 127

def step_contents(context, model)
  contents = run_step_contents_callback(context, model)
  {
    :hatTrickStepContents => {
      name.to_s.camelize(:lower).to_sym => contents
    }
  }
end

#step_contents_callback=(block) ⇒ Object



123
124
125
# File 'lib/hat_trick/step_definition.rb', line 123

def step_contents_callback=(block)
  callbacks[:step_contents] = block
end

#to_sObject



99
100
101
102
103
104
# File 'lib/hat_trick/step_definition.rb', line 99

def to_s
  str = "<HatTrick::Step:0x%08x :#{name}" % (object_id * 2)
  str += " fieldset: #{fieldset}" if fieldset != name
  str += ">"
  str
end

#to_symObject



106
107
108
# File 'lib/hat_trick/step_definition.rb', line 106

def to_sym
  name.to_sym
end