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) ⇒ RecipeStep

Returns a new instance of RecipeStep.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/baker/bakerlib.rb', line 120

def initialize(lines, type: , directive_type: nil, content: nil, attributes: nil, command: nil, task_marker: nil, description: 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
end

Instance Attribute Details

#attributesObject



190
191
192
193
# File 'lib/baker/bakerlib.rb', line 190

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

#linesObject

Lines are expected to contain the line end character “n”



116
117
118
# File 'lib/baker/bakerlib.rb', line 116

def lines
  @lines
end

#typeObject

Returns the value of attribute type.



117
118
119
# File 'lib/baker/bakerlib.rb', line 117

def type
  @type
end

Instance Method Details

#commandObject



195
196
197
198
# File 'lib/baker/bakerlib.rb', line 195

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

#completed?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/baker/bakerlib.rb', line 143

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

#contentObject



185
186
187
188
# File 'lib/baker/bakerlib.rb', line 185

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

#deleteObject



135
136
137
# File 'lib/baker/bakerlib.rb', line 135

def delete
  self.lines = []
end

#descriptionObject



205
206
207
# File 'lib/baker/bakerlib.rb', line 205

def description
  return @description
end

#directive_typeObject



180
181
182
183
# File 'lib/baker/bakerlib.rb', line 180

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

#indent_depth(line_index) ⇒ Object

Returns the number of leading spaces of the given line



210
211
212
213
214
# File 'lib/baker/bakerlib.rb', line 210

def indent_depth(line_index)
  return nil if !lines[line_index] || lines[line_index].strip.length == 0

  lines[line_index][/\A\s*/].length
end

#is_any_open?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/baker/bakerlib.rb', line 256

def is_any_open?
  return lines.any? { |line| line =~ /^\s*(-\s+)?\[\s\]/ }
end

#is_closed?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/baker/bakerlib.rb', line 260

def is_closed?
  return !is_any_open?
end

#mark_completeObject



160
161
162
163
164
165
166
167
168
# File 'lib/baker/bakerlib.rb', line 160

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

#mark_todoObject



170
171
172
173
174
175
176
177
178
# File 'lib/baker/bakerlib.rb', line 170

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

#parent_index(line_index) ⇒ Object

Returns the line index of the parent line if any or nil The parent line is the line with a reduced indentation or the section header in case there no reduced indented line



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/baker/bakerlib.rb', line 218

def parent_index(line_index)
  j = line_index - 1
  my_indent = indent_depth(line_index)
  return nil if !my_indent
  while j > 0 # Day header does not count as parent
    other_indent = indent_depth(j)
    if other_indent && other_indent < my_indent
      return j
    end
    j -= 1
  end
  return nil
end

#structureObject

Turns the linear list of lines of this TodoDay into a nested structure of the form

“text”, children: […], …

where … is the same hash structure “text”, children: […]



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/baker/bakerlib.rb', line 235

def structure

  indents = [nil] * lines.size
  (lines.size - 1).downto(0).each { |i|
    indents[i] = indent_depth(i) || (i+1 < indents.size ? indents[i+1] : 0)
  }

  stack = [{depth: -1, children: []}]
  lines.each_with_index { |s, i|
    indent = indents[i]
    new_child = {depth: indent, text: s, index: i, children: []}
    while indent <= stack.last[:depth]
      stack.pop
    end
    stack.last[:children] << new_child
    stack << new_child
  }

  return stack.first[:children]
end

#task_markerObject



200
201
202
203
# File 'lib/baker/bakerlib.rb', line 200

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

#to_sObject



139
140
141
# File 'lib/baker/bakerlib.rb', line 139

def to_s
  lines.join
end