Class: Krikri::Parser::ValueArray
- Inherits:
-
Object
- Object
- Krikri::Parser::ValueArray
- Includes:
- Enumerable
- Defined in:
- lib/krikri/parser.rb
Overview
A specialized Array object for containing Parser::Values. Provides methods for accessing and filtering values that can be chained.
Methods defined on this class should return another ValueArray, an Array of literal values (retrieved from Parser::Value#value), or a single literal value.
Uses ‘@top` to track a base node for recovery via `#else`. Methods that return a `ValueArray` should pass `@top` down to the new instance.
Defined Under Namespace
Classes: InvalidParserValueError
Class Method Summary collapse
-
.build(record) ⇒ ValueArray
Wraps the root node of the given record in this class.
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #[]=(index, value) ⇒ Object
- #concat(*args, &block) ⇒ ValueArray
-
#else {|arry| ... } ⇒ ValueArray
Short circuits if ‘self` is not empty, else passes the top of the call chain (`@top`) to the given block.
-
#field(*args) ⇒ ValueArray
Accesses a given field.
-
#fields(*args) ⇒ ValueArray
Accesses the union of multiple specified fields.
-
#first_value(*args) ⇒ ValueArray
Retrieves the first element of a ValueArray.
- #flatten(*args, &block) ⇒ ValueArray
-
#if {|arry| ... } ⇒ ValueArray
Sets the top of the call chain to self and returns or yields self.
-
#initialize(array = [], top = nil) ⇒ ValueArray
constructor
A new instance of ValueArray.
-
#last_value(*args) ⇒ ValueArray
Retrieves the last element of a ValueArray.
-
#map(*args, &block) ⇒ ValueArray
Wraps the result of Array#map in a ValueArray.
-
#match_attribute(name, other = nil) {|value| ... } ⇒ ValueArray
An array containing nodes for which the specified attribute has a value matching the given attribute name, object, and block.
-
#reject(*args, &block) ⇒ ValueArray
Wraps the result of Array#reject in a ValueArray.
-
#reject_attribute(name, other = nil) {|value| ... } ⇒ ValueArray
An array containing nodes for which the specified attribute does not have a value matching the given attribute name, object, and block.
-
#select(*args, &block) ⇒ ValueArray
Wraps the result of Array#select in a ValueArray.
-
#values ⇒ Array
Literal values from the objects in this array.
Constructor Details
#initialize(array = [], top = nil) ⇒ ValueArray
Returns a new instance of ValueArray.
178 179 180 181 |
# File 'lib/krikri/parser.rb', line 178 def initialize(array = [], top = nil) @array = array @top = top || self end |
Class Method Details
.build(record) ⇒ ValueArray
Wraps the root node of the given record in this class.
401 402 403 |
# File 'lib/krikri/parser.rb', line 401 def self.build(record) new([record.root]) end |
Instance Method Details
#<<(value) ⇒ Object
195 196 197 198 199 |
# File 'lib/krikri/parser.rb', line 195 def <<(value) raise InvalidParserValueError unless value.is_a? Value @array << value value end |
#[]=(index, value) ⇒ Object
186 187 188 189 190 |
# File 'lib/krikri/parser.rb', line 186 def []=(index, value) raise InvalidParserValueError unless value.is_a? Value @array[index] = value self end |
#concat(*args, &block) ⇒ ValueArray
204 205 206 |
# File 'lib/krikri/parser.rb', line 204 def concat(*args, &block) self.class.new(@array.concat(*args, &block), @top) end |
#else {|arry| ... } ⇒ ValueArray
Short circuits if ‘self` is not empty, else passes the top of the call chain (`@top`) to the given block.
282 283 284 285 286 |
# File 'lib/krikri/parser.rb', line 282 def else(&block) raise ArgumentError, 'No block given for `#else`' unless block_given? return self unless self.empty? yield @top end |
#field(*args) ⇒ ValueArray
Accesses a given field. Use multiple arguments to travel down the node hierarchy.
221 222 223 224 225 226 |
# File 'lib/krikri/parser.rb', line 221 def field(*args) result = self args.each { |name| result = result.get_field(name) } result end |
#fields(*args) ⇒ ValueArray
Accesses the union of multiple specified fields.
given fields.
233 234 235 236 237 238 |
# File 'lib/krikri/parser.rb', line 233 def fields(*args) results = args.map do |f| field(*Array(f)) end self.class.new(results.flatten, @top) end |
#first_value(*args) ⇒ ValueArray
Retrieves the first element of a ValueArray. Uses an optional argument to specify how many items to return. By design, it behaves similarly to Array#first, but it intentionally doesn’t override it.
294 295 296 297 |
# File 'lib/krikri/parser.rb', line 294 def first_value(*args) return self.class.new(@array.first(*args)) unless args.empty? self.class.new([@array.first].compact, @top) end |
#flatten(*args, &block) ⇒ ValueArray
314 315 316 |
# File 'lib/krikri/parser.rb', line 314 def flatten(*args, &block) self.class.new(@array.flatten(*args, &block), @top) end |
#if {|arry| ... } ⇒ ValueArray
Sets the top of the call chain to self and returns or yields self
256 257 258 259 260 |
# File 'lib/krikri/parser.rb', line 256 def if(&block) @top = self return yield self if block_given? self end |
#last_value(*args) ⇒ ValueArray
Retrieves the last element of a ValueArray. Uses an optional argument to specify how many items to return. By design, it behaves similarly to Array#last, but it intentionally doesn’t override it.
305 306 307 308 |
# File 'lib/krikri/parser.rb', line 305 def last_value(*args) return self.class.new(@array.last(*args)) unless args.empty? self.class.new([@array.last].compact, @top) end |
#map(*args, &block) ⇒ ValueArray
Wraps the result of Array#map in a ValueArray
323 324 325 |
# File 'lib/krikri/parser.rb', line 323 def map(*args, &block) self.class.new(@array.map(*args, &block), @top) end |
#match_attribute(name, other = nil) {|value| ... } ⇒ ValueArray
Returns an array containing nodes for which the specified attribute has a value matching the given attribute name, object, and block.
375 376 377 |
# File 'lib/krikri/parser.rb', line 375 def match_attribute(name, other = nil, &block) select(&compare_to_attribute(name, other, &block)) end |
#reject(*args, &block) ⇒ ValueArray
Wraps the result of Array#reject in a ValueArray
341 342 343 |
# File 'lib/krikri/parser.rb', line 341 def reject(*args, &block) self.class.new(@array.reject(*args, &block), @top) end |
#reject_attribute(name, other = nil) {|value| ... } ⇒ ValueArray
Returns an array containing nodes for which the specified attribute does not have a value matching the given attribute name, object, and block.
392 393 394 |
# File 'lib/krikri/parser.rb', line 392 def reject_attribute(name, other = nil, &block) reject(&compare_to_attribute(name, other, &block)) end |
#select(*args, &block) ⇒ ValueArray
Wraps the result of Array#select in a ValueArray
332 333 334 |
# File 'lib/krikri/parser.rb', line 332 def select(*args, &block) self.class.new(@array.select(*args, &block), @top) end |
#values ⇒ Array
Returns literal values from the objects in this array.
211 212 213 |
# File 'lib/krikri/parser.rb', line 211 def values @array.map { |v| v.respond_to?(:value) ? v.value : v } end |