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
# 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)
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



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

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



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cucumberator/commands/save.rb', line 67

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



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

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

#insert_line_to_feature_file(line) ⇒ Object



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

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

#saveObject



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

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



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

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

#spaces_in_last_inputObject



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

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