Class: RecipeStep

Inherits:
Object
  • Object
show all
Defined in:
lib/baker/bakerlib.rb

Overview

Encapsulate a single date of todo information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, type:, directive_type: nil, content: nil, attributes: nil, command: nil, task_marker: nil, description: nil, line_index: nil) ⇒ RecipeStep

Returns a new instance of RecipeStep.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/baker/bakerlib.rb', line 228

def initialize(lines, type: , directive_type: nil, content: nil, attributes: nil, command: nil, task_marker: nil, description: nil, line_index: nil)
  if lines.is_a?(Array)
    @lines = lines
  else
    @lines = [lines]
  end
  @type = type
  @directive_type = directive_type
  @content = content
  @attributes = attributes
  @command = command
  @task_marker = task_marker
  @description = description
  @line_index = line_index
end

Instance Attribute Details

#attributesObject



340
341
342
343
# File 'lib/baker/bakerlib.rb', line 340

def attributes
  raise "Not a directive" if type != :directive
  return @attributes
end

#line_indexObject (readonly)

matches the index into the parent Recipe.steps.array



226
227
228
# File 'lib/baker/bakerlib.rb', line 226

def line_index
  @line_index
end

#linesObject

Lines are expected to contain the line end character ā€œnā€



223
224
225
# File 'lib/baker/bakerlib.rb', line 223

def lines
  @lines
end

#typeObject

Returns the value of attribute type.



224
225
226
# File 'lib/baker/bakerlib.rb', line 224

def type
  @type
end

Instance Method Details

#commandObject



345
346
347
348
# File 'lib/baker/bakerlib.rb', line 345

def command
  raise "Not a shell" if type != :shell && type != :ruby
  return @command
end

#completed?Boolean

Returns:

  • (Boolean)


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/baker/bakerlib.rb', line 272

def completed?
  case type
  when :directive
    case directive_type
    when :var
      return attributes != nil
    when :template
      return false
    when :template_source
      return false
    when :cd
      return false
    end
    raise "Unknown directive type: #{directive_type}"
  when :shell, :manual, :ruby
    return task_marker != " "
  else
    return true
  end
end

#contentObject



335
336
337
338
# File 'lib/baker/bakerlib.rb', line 335

def content
  raise "RecipeStep.content is only valid for a directive, but is #{type}" if type != :directive
  return @content
end

#deleteObject



244
245
246
# File 'lib/baker/bakerlib.rb', line 244

def delete
  self.lines = []
end

#descriptionObject



355
356
357
# File 'lib/baker/bakerlib.rb', line 355

def description
  return @description
end

#directive_typeObject



330
331
332
333
# File 'lib/baker/bakerlib.rb', line 330

def directive_type
  raise "Not a directive" if type != :directive
  return @directive_type
end

#indentation_levelObject

Returns the indentation level (whitespace count) of the first line in this RecipeLine Empty lines are considered to have an indentation level of nil



315
316
317
318
# File 'lib/baker/bakerlib.rb', line 315

def indentation_level
  return nil if !lines || lines.size == 0 || lines[0].strip.length == 0
  return lines[0][/\A\s*/].length
end

#mark_completeObject



293
294
295
296
297
298
299
300
301
# File 'lib/baker/bakerlib.rb', line 293

def mark_complete
  if lines[0] =~ /\[\s\]/
    lines[0].sub!(/\[\s\]/, "[x]")
    @task_marker = "x"
  else
    puts lines.inspect
    raise
  end
end

#mark_strikethroughObject



303
304
305
306
307
308
309
310
311
# File 'lib/baker/bakerlib.rb', line 303

def mark_strikethrough
  if lines[0] =~ /\[\s\]/
    lines[0].sub!(/\[\s\]/, "[-]")
    @task_marker = "-"
  else
    puts lines.inspect
    raise
  end
end

#mark_todoObject



320
321
322
323
324
325
326
327
328
# File 'lib/baker/bakerlib.rb', line 320

def mark_todo
  if lines[0] =~ /\[[xX\-.y]\]/
    lines[0].sub!(/\[[xX\-.y]\]/, "[ ]")
    @task_marker = " "
  else
    puts lines.inspect
    raise
  end
end

#single_line_for_displayObject

Returns the description if any, otherwise the first line of the command with an indication of type ruby or shell Otherwise first line



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/baker/bakerlib.rb', line 254

def single_line_for_display
  if description && description.strip.size > 0
    return description
  end
  if command && command.strip.size > 0
    return "#{type} command - " + (command.split("\n").first == command ? command : command.split("\n").first + "...").strip
  end

  s = to_s
  return '<empty line>' if s.strip.size == 0

  return (s.split("\n").first == s ? s : s.split("\n").first + "...").strip
end

#source_code_line_indexObject



268
269
270
# File 'lib/baker/bakerlib.rb', line 268

def source_code_line_index
  return @line_index + 1
end

#task_markerObject



350
351
352
353
# File 'lib/baker/bakerlib.rb', line 350

def task_marker
  raise "Not a task" if type != :shell && type != :manual && type != :ruby
  return @task_marker
end

#to_sObject



248
249
250
# File 'lib/baker/bakerlib.rb', line 248

def to_s
  lines.join
end