Class: FixtureFox::Line

Inherits:
TokenizedLine show all
Defined in:
lib/fixture_fox/line.rb

Instance Attribute Summary collapse

Attributes inherited from TokenizedLine

#file, #indent, #initial_indent, #line, #lineno

Instance Method Summary collapse

Methods inherited from TokenizedLine

#error

Constructor Details

#initialize(file, lineno, initial_indent, indent, line) ⇒ Line

Returns a new instance of Line.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fixture_fox/line.rb', line 100

def initialize(file, lineno, initial_indent, indent, line)
  super

  # Remove comments not preceded by a quote (' or ")
  if line =~ /^([^'"#]*?)\s*#.*/
    line = $1
  end

  # Parse root element
  if indent == 0 && line[0] != "-"
    # Table
    case line
      when /^(\w+(?:\.\w+)?)$/
        @ident = make_ident($1)
        return
      when /^(\w+(?:\.\w+)?)(\s*:\s*)(\[\])$/
        @ident = make_ident($1)
        @pos += $1.size + $2.size
        @empty = make_empty($3)
        return
    end

    # Check for missing value in root record
    if line =~ /^(\w+\s*:)$/
      error "Table name expected"
    end
  end

  # Parse dash
  case line
    when /^-$/
      error "Entry content expected"
    when /^(-\s+)(&\w+.*)/
      @dash = make_token("-")
      @pos += $1.size
      @anchor = make_anchor($2.sub(/#.*/, ""))
      return
    when /^(-\s+)(.*)/
      @dash = make_token("-")
      @pos += $1.size
      line = $2
    when /^-./
      @pos += 1
      error "Illegal character after '-'"
  end

  # Expect key/value pair
  case line
    when /^(\w+)(\s*):\s*$/ # Record
      @ident = make_ident($1)
      @pos += $1.size + $2.size + 1
    when /^(\w+)(\s*:\s*)(&\w+)(\s*)(\S.*)?$/ # Anchor
      @ident = make_ident($1)
      @pos += $1.size + $2.size
      @anchor = make_anchor($3)
      @pos += $3.size + $4.size
      $5.nil? or error "Illegal character after anchor"
    when /^(\w+)(\s*:\s*)(\*.+)(\s*)(\S.*)?/ # Reference
      @ident = make_ident($1)
      @pos += $1.size + $2.size
      @reference = make_reference($3)
      @pos += $3.size + $4.size
      $5.nil? or error "Illegal character after reference"
    when /^(\w+)(\s*:\s*)(["']?.+)/ # Simple value
      @ident = make_ident($1)
      @pos += $1.size + $2.size
      @value = make_value($3)
    when /^(\w+)(\s*:\s*)(\[.*)/ # Array value
      @ident = make_ident($1)
      @pos += $1.size + $2.size
      litt =~ /.*\]\s*$/ or error "Missing ']'"
      @value = make_value($3)
    when /^(\w+)(\s*:\s*)(\{.*)/ # Hash value
      @ident = make_ident($1)
      @pos += $1.size + $2.size
      litt =~ /.*\]\s*$/ or error "Missing '}'"
      @value = make_value($3)
    when /^(\*.+)(\s*)(\S.*)?$/
      @reference = make_reference($1)
      @pos += $1.size + $2.size
      $3.nil? or error "Illegal character after reference"
    when /^\[.*\]\s*$/
    else
      @pos += line.size
      error "Missing value"
  end
end

Instance Attribute Details

#anchorObject (readonly)

Returns the value of attribute anchor.



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

def anchor
  @anchor
end

#dashObject (readonly)

Returns the value of attribute dash.



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

def dash
  @dash
end

#directiveObject (readonly)

SchemaDirective token



98
99
100
# File 'lib/fixture_fox/line.rb', line 98

def directive
  @directive
end

#emptyObject (readonly)

Empty token



93
94
95
# File 'lib/fixture_fox/line.rb', line 93

def empty
  @empty
end

#identObject (readonly)

Returns the value of attribute ident.



92
93
94
# File 'lib/fixture_fox/line.rb', line 92

def ident
  @ident
end

#referenceObject (readonly)

Returns the value of attribute reference.



95
96
97
# File 'lib/fixture_fox/line.rb', line 95

def reference
  @reference
end

#valueObject (readonly)

Returns the value of attribute value.



94
95
96
# File 'lib/fixture_fox/line.rb', line 94

def value
  @value
end

Instance Method Details

#element?Boolean

True if the line is the first line of a table element

Returns:

  • (Boolean)


195
# File 'lib/fixture_fox/line.rb', line 195

def element?() !dash.nil? end

#root_record?Boolean

True if the line is the first line of a root record

Returns:

  • (Boolean)


192
# File 'lib/fixture_fox/line.rb', line 192

def root_record?() indent == 0 && dash.nil? && !value.nil? || false end

#root_table?Boolean

True if the line is the first line of a root table

Returns:

  • (Boolean)


189
# File 'lib/fixture_fox/line.rb', line 189

def root_table?() indent == 0 && dash.nil? && value.nil? end

#to_s(long: false) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fixture_fox/line.rb', line 197

def to_s(long: false)
  super +
      if long
        [ dash && "dash:#{dash.pos}",
          ident && "ident:#{ident.pos} #{ident.value}",
          value && "value:#{value.pos} #{value.value}",
          reference && "reference:#{reference.pos} #{reference.value}",
          anchor && "anchor:##{anchor.pos} #{anchor.value}"
        ]
      else
        [ dash && "dash",
          ident && "ident: #{ident.to_s}",
          value && "value: #{value.to_s}",
          reference && "reference: #{reference.to_s}",
          anchor && "anchor: #{anchor.to_s}"
        ]
      end.compact.join(", ")
end