Class: Cucumberator::Commands::Save

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumberator/commands/save.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scenario, step_line, last_input, saved_stack) ⇒ Save

Returns a new instance of Save.



17
18
19
20
21
# File 'lib/cucumberator/commands/save.rb', line 17

def initialize(scenario, step_line, last_input, saved_stack)
  @step_line, @last_input, @saved_stack = step_line, last_input, saved_stack
  @feature_file  = Cucumberator::FeatureFile.new(scenario)
  @scenario_line = scenario.line - 1
end

Instance Attribute Details

#saved_stackObject

Returns the value of attribute saved_stack.



15
16
17
# File 'lib/cucumberator/commands/save.rb', line 15

def saved_stack
  @saved_stack
end

#step_lineObject

Returns the value of attribute step_line.



15
16
17
# File 'lib/cucumberator/commands/save.rb', line 15

def step_line
  @step_line
end

Class Method Details

.perform(scenario, step_line, last_input, saved_stack, *args, &block) ⇒ Object



4
5
6
7
# File 'lib/cucumberator/commands/save.rb', line 4

def perform(scenario, step_line, last_input, saved_stack, *args, &block)
  new(scenario, step_line, last_input, saved_stack).save
  false
end

.save_empty_line(scenario, step_line, saved_stack) ⇒ Object



9
10
11
12
# File 'lib/cucumberator/commands/save.rb', line 9

def save_empty_line(scenario, step_line, saved_stack)
  new(scenario, step_line, "", saved_stack).force_save_empty_line
  false
end

Instance Method Details

#append_line_to_feature_file(line) ⇒ Object



57
58
59
60
# File 'lib/cucumberator/commands/save.rb', line 57

def append_line_to_feature_file(line)
  @feature_file.append(line)
  self.saved_stack << [@feature_file.lines.size, line]
end

#detect_last_line(lines) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cucumberator/commands/save.rb', line 78

def detect_last_line(lines)
  if step_line
    line  = lines[step_line-1]
    lines = lines.slice(0, step_line-1) if line.to_s.empty?
  end

  if line.to_s.empty?
    line = lines.reverse.detect { |l| !l.to_s.empty? }
  end

  line
end

#force_save_empty_lineObject



35
36
37
38
# File 'lib/cucumberator/commands/save.rb', line 35

def force_save_empty_line
  save_to_feature_file("")
  @last_input = nil
end

#insert_line_to_feature_file(line) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/cucumberator/commands/save.rb', line 48

def insert_line_to_feature_file(line)
  lines = @feature_file.lines
  lines.insert(step_line - 1, line.to_s+$/) # $/ - default newline separator
  @feature_file.overwrite(lines.join)

  self.saved_stack << [step_line.number, line]
  self.step_line.increment!
end

#parent_scenario_spacingObject



70
71
72
# File 'lib/cucumberator/commands/save.rb', line 70

def parent_scenario_spacing
  @parent_depth ||= @feature_file.lines[@scenario_line] =~ /\S/
end

#saveObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cucumberator/commands/save.rb', line 23

def save
  if @last_input.to_s.empty?
    puts "Hm... nothing to save yet?"
  else
    string_to_save = (" " * spaces_in_last_input) + @last_input
    save_to_feature_file(string_to_save)

    puts "Saved `#{@last_input}` to #{@feature_file}"
    @last_input = nil
  end
end

#save_to_feature_file(line) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/cucumberator/commands/save.rb', line 40

def save_to_feature_file(line)
  if step_line
    insert_line_to_feature_file(line)
  else
    append_line_to_feature_file(line)
  end
end

#scenario_child_spacingObject



74
75
76
# File 'lib/cucumberator/commands/save.rb', line 74

def scenario_child_spacing
  @child_depth ||= parent_scenario_spacing + 2
end

#spaces_in_last_inputObject



62
63
64
65
66
67
68
# File 'lib/cucumberator/commands/save.rb', line 62

def spaces_in_last_input
  return scenario_child_spacing unless step_line

  line = detect_last_line(@feature_file.lines)
  spaces = line.to_s =~ /\S/
  spaces.to_i
end