Class: QED::Parser::Block

Inherits:
Object show all
Defined in:
lib/qed/parser.rb

Overview

Section Block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Block

Returns a new instance of Block.



205
206
207
208
209
210
211
212
213
# File 'lib/qed/parser.rb', line 205

def initialize(file)
  QED.all_steps << self

  @file = file
  @raw  = []
  @type = :description
  @back_step = nil
  @next_step = nil
end

Instance Attribute Details

#back_stepObject (readonly)

previous block



199
200
201
# File 'lib/qed/parser.rb', line 199

def back_step
  @back_step
end

#next_stepObject

next block



202
203
204
# File 'lib/qed/parser.rb', line 202

def next_step
  @next_step
end

#rawObject (readonly)

Block raw code/text.



196
197
198
# File 'lib/qed/parser.rb', line 196

def raw
  @raw
end

Instance Method Details

#argumentsObject

Returns an Array of prepared example text for use in advice.



250
251
252
253
254
255
256
# File 'lib/qed/parser.rb', line 250

def arguments
  if next_step && next_step.data?
    [next_step.sample_text]
  else
    []
  end
end

#clean_textObject

Clean up the example text, removing unccesseary white lines and triple quote brackets, but keep indention intact.



295
296
297
298
299
300
301
# File 'lib/qed/parser.rb', line 295

def clean_text
  str = text.chomp.sub(/\A\n/,'')
  if md = /\A["]{3,}(.*?)["]{3,}\Z/.match(str)
    str = md[1]
  end
  str.rstrip
end

#codeObject



289
290
291
# File 'lib/qed/parser.rb', line 289

def code
  @code ||= tweak_code
end

#code?Boolean

Returns:

  • (Boolean)


270
# File 'lib/qed/parser.rb', line 270

def code? ; @type == :code ; end

#data?Boolean

Any commentary ending in ‘…` or `:` will mark the following block as a plain text sample and not example code to be evaluated.

Returns:

  • (Boolean)


274
# File 'lib/qed/parser.rb', line 274

def data? ; @type == :data ; end

#desc?Boolean Also known as: description?

Returns:

  • (Boolean)


267
# File 'lib/qed/parser.rb', line 267

def desc? ; @type == :desc ; end

#flush?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/qed/parser.rb', line 244

def flush?
  @flush
end

#head?Boolean Also known as: header?

Returns:

  • (Boolean)


264
# File 'lib/qed/parser.rb', line 264

def head? ; @type == :head ; end

#inspectObject

TODO: object_hexid



315
316
317
# File 'lib/qed/parser.rb', line 315

def inspect
  %[#<Block:#{object_id} "#{text[0..25]} ...">]
end

#linenoObject

First line of example text.



284
285
286
# File 'lib/qed/parser.rb', line 284

def lineno
  @line ||= @raw.first.first
end

#ready!(flush, back_step) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/qed/parser.rb', line 216

def ready!(flush, back_step)
  @flush     = flush
  @back_step = back_step

  @text  = raw.map{ |lineno, line| line }.join
  @type  = parse_type

  @back_step.next_step = self if @back_step

  self
end

#sample_textObject

When the text is sample text and passed to an adivce block, this provides the prepared form of the example text, removing white lines, triple quote brackets and indention.



306
307
308
309
310
311
312
# File 'lib/qed/parser.rb', line 306

def sample_text
  str = text.tabto(0).chomp.sub(/\A\n/,'')
  if md = /\A["]{3,}(.*?)["]{3,}\Z/.match(str)
    str = md[1]
  end
  str.rstrip
end

#textObject



239
240
241
# File 'lib/qed/parser.rb', line 239

def text
  @text
end

#to_sObject



229
230
231
232
233
234
235
236
# File 'lib/qed/parser.rb', line 229

def to_s
  case type
  when :description
    text
  else
    text
  end
end

#typeObject

What type of block is this?



259
260
261
# File 'lib/qed/parser.rb', line 259

def type
  @type
end