Class: FormatEngine::SpecInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/format_engine/spec_info.rb

Overview

A little package of info about the engine’s progress.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, dst, engine) ⇒ SpecInfo

Set up the spec info.



22
23
24
# File 'lib/format_engine/spec_info.rb', line 22

def initialize(src, dst, engine)
  @src, @dst, @engine, @tmp = src, dst, engine, {}
end

Instance Attribute Details

#dstObject (readonly)

The destination of the process.



10
11
12
# File 'lib/format_engine/spec_info.rb', line 10

def dst
  @dst
end

#engineObject (readonly)

The formatting engine.



13
14
15
# File 'lib/format_engine/spec_info.rb', line 13

def engine
  @engine
end

#fmtObject (readonly)

The format specifier currently being processed.



19
20
21
# File 'lib/format_engine/spec_info.rb', line 19

def fmt
  @fmt
end

#srcObject (readonly)

The source data for formatting, string input for parsing.



7
8
9
# File 'lib/format_engine/spec_info.rb', line 7

def src
  @src
end

#tmpObject (readonly)

A hash for state storage for the formatting/parsing process.



16
17
18
# File 'lib/format_engine/spec_info.rb', line 16

def tmp
  @tmp
end

Instance Method Details

#cat(str) ⇒ Object

Concatenate onto the formatted output string.



27
28
29
# File 'lib/format_engine/spec_info.rb', line 27

def cat(str)
  @dst << str
end

#do_format(fmt) ⇒ Object

Pass the formatting action along to the current format element.



37
38
39
# File 'lib/format_engine/spec_info.rb', line 37

def do_format(fmt)
  (@fmt = fmt).do_format(self)
end

#do_parse(fmt) ⇒ Object

Pass the parsing action along to the current format element.



42
43
44
# File 'lib/format_engine/spec_info.rb', line 42

def do_parse(fmt)
  (@fmt = fmt).do_parse(self)
end

#foundObject

What was found by the last parse?



96
97
98
# File 'lib/format_engine/spec_info.rb', line 96

def found
  @match
end

#found?Boolean

Was the last parse a success?

Returns:

  • (Boolean)


91
92
93
# File 'lib/format_engine/spec_info.rb', line 91

def found?
   @prematch.empty? && !@match.empty?
end

#grabObject

Grab some text



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/format_engine/spec_info.rb', line 74

def grab
  width = fmt.width

  if width > 0
    result, @src = src[0...width], src[width..-1] || ""
  elsif width == 0
    result, @src = src[0...1], src[1..-1] || ""
  elsif width == -1
    result, @src = src, ""
  else
    result, @src = src[0..width], src[(width+1)..-1] || ""
  end

  result
end

#parse(target) ⇒ Object

Parse the source string for a target string or regex or return nil.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/format_engine/spec_info.rb', line 47

def parse(target)
  #Handle the width option if specified.
  if (width = fmt.width) > 0
    head, tail = src[0...width], src[width..-1] || ""
  else
    head, tail = src, ""
  end

  #Do the parse on the input string or regex.
  @prematch, @match, @postmatch = head.partition(target)

  #Analyze the results.
  if found?
    @src = @postmatch + tail
    @match
  else
    nil
  end
end

#parse!(target, msg = "#{target.inspect} not found") ⇒ Object

Parse the source string for a target string or regex or raise error.



68
69
70
71
# File 'lib/format_engine/spec_info.rb', line 68

def parse!(target, msg = "#{target.inspect} not found")
  fail "Parse error: #{msg}" unless parse(target)
  @match
end

#set(obj) ⇒ Object

Set the result of this parsing operation.



32
33
34
# File 'lib/format_engine/spec_info.rb', line 32

def set(obj)
  @dst = obj
end