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 ‘#bindings` to track variables for recovery via `#else`, `#from`, and `#back`. Methods that return a `ValueArray` pass `#bindings` down to the new instance.
Defined Under Namespace
Classes: InvalidParserValueError
Instance Attribute Summary collapse
-
#bindings ⇒ Object
readonly
Returns the value of attribute bindings.
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
-
#and(from: :top) ⇒ ValueArray
Returns to a bound variable unless the current value is empty.
-
#at(idx) ⇒ ValueArray
An array containing the node(s) in the specified index posiition(s).
-
#bind(var) ⇒ ValueArray
Binds the current array to the variable name given.
- #compact ⇒ ValueArray
- #concat(*args, &block) ⇒ ValueArray
-
#else {|arry| ... } ⇒ ValueArray
Short circuits if ‘self` is not empty, else passes the top of the call chain (`@bindings`) to the given block.
-
#field(*args) ⇒ ValueArray
Accesses a given field.
-
#field_names ⇒ ValueArray
Lists field names on this node.
-
#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 = [], bindings = {}) ⇒ 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.
-
#match_child(*children) ⇒ ValueArray
An array containing nodes for which the specified attribute does not have a child node matching the given child name.
-
#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.
-
#reject_child(*children) ⇒ ValueArray
An array containing nodes for which the specified attribute does not have a childnode matching the given child name.
-
#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 = [], bindings = {}) ⇒ ValueArray
Returns a new instance of ValueArray.
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/krikri/parser.rb', line 190 def initialize(array = [], bindings = {}) @array = array if bindings.is_a?(ValueArray) # this way is deprected! # how should we handle deprecations? @bindings = {} @bindings[:top] ||= bindings else @bindings = bindings @bindings[:top] ||= self end end |
Instance Attribute Details
#bindings ⇒ Object (readonly)
Returns the value of attribute bindings.
179 180 181 |
# File 'lib/krikri/parser.rb', line 179 def bindings @bindings end |
Class Method Details
.build(record) ⇒ ValueArray
Wraps the root node of the given record in this class.
504 505 506 |
# File 'lib/krikri/parser.rb', line 504 def self.build(record) new([record.root]) end |
Instance Method Details
#<<(value) ⇒ Object
216 217 218 219 220 |
# File 'lib/krikri/parser.rb', line 216 def <<(value) raise InvalidParserValueError unless value.is_a? Value @array << value value end |
#[]=(index, value) ⇒ Object
207 208 209 210 211 |
# File 'lib/krikri/parser.rb', line 207 def []=(index, value) raise InvalidParserValueError unless value.is_a? Value @array[index] = value self end |
#and(from: :top) ⇒ ValueArray
Returns to a bound variable unless the current value is empty. If no variable is given, returns to the top of the call chain.
This allows checking complex conditionals, where mappings depend on data from deep within multiple branches of the tree.
240 241 242 243 244 |
# File 'lib/krikri/parser.rb', line 240 def and(from: :top) return self if self.empty? bindings[from.to_sym] or raise ArgumentError, "Tried to return to unbound variable: #{from}" end |
#at(idx) ⇒ ValueArray
Returns an array containing the node(s) in the specified index posiition(s).
274 275 276 |
# File 'lib/krikri/parser.rb', line 274 def at(idx) self.class.new(Array(@array[idx])) end |
#bind(var) ⇒ ValueArray
Binds the current array to the variable name given
251 252 253 254 |
# File 'lib/krikri/parser.rb', line 251 def bind(var) bindings[var.to_sym] = self self end |
#compact ⇒ ValueArray
392 393 394 |
# File 'lib/krikri/parser.rb', line 392 def compact self.class.new(@array.compact, bindings) end |
#concat(*args, &block) ⇒ ValueArray
259 260 261 |
# File 'lib/krikri/parser.rb', line 259 def concat(*args, &block) self.class.new(@array.concat(*args, &block), bindings) end |
#else {|arry| ... } ⇒ ValueArray
Short circuits if ‘self` is not empty, else passes the top of the call chain (`@bindings`) to the given block.
361 362 363 364 365 |
# File 'lib/krikri/parser.rb', line 361 def else(&block) raise ArgumentError, 'No block given for `#else`' unless block_given? return self unless self.empty? yield bindings[:top] end |
#field(*args) ⇒ ValueArray
Accesses a given field. Use multiple arguments to travel down the node hierarchy.
284 285 286 287 288 289 |
# File 'lib/krikri/parser.rb', line 284 def field(*args) result = self args.each { |name| result = result.get_field(name) } result end |
#field_names ⇒ ValueArray
Lists field names on this node.
This can be useful in cases where the desired value is a key or child node label, rather than a value.
312 313 314 |
# File 'lib/krikri/parser.rb', line 312 def field_names self.class.new(flat_map(&:children).uniq, bindings) end |
#fields(*args) ⇒ ValueArray
Accesses the union of multiple specified fields.
given fields.
296 297 298 299 300 301 |
# File 'lib/krikri/parser.rb', line 296 def fields(*args) results = args.map do |f| field(*Array(f)) end self.class.new(results.flatten, bindings) 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.
373 374 375 376 |
# File 'lib/krikri/parser.rb', line 373 def first_value(*args) return self.class.new(@array.first(*args)) unless args.empty? self.class.new([@array.first].compact, bindings) end |
#flatten(*args, &block) ⇒ ValueArray
399 400 401 |
# File 'lib/krikri/parser.rb', line 399 def flatten(*args, &block) self.class.new(@array.flatten(*args, &block), bindings) end |
#if {|arry| ... } ⇒ ValueArray
Sets the top of the call chain to self and returns or yields self.
This is syntactic sugar for ‘#bind(:top)`, with the addition of block syntax.
335 336 337 338 339 |
# File 'lib/krikri/parser.rb', line 335 def if(&block) bind(:top) 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.
384 385 386 387 |
# File 'lib/krikri/parser.rb', line 384 def last_value(*args) return self.class.new(@array.last(*args)) unless args.empty? self.class.new([@array.last].compact, bindings) end |
#map(*args, &block) ⇒ ValueArray
Wraps the result of Array#map in a ValueArray
408 409 410 |
# File 'lib/krikri/parser.rb', line 408 def map(*args, &block) self.class.new(@array.map(*args, &block), bindings) 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.
460 461 462 |
# File 'lib/krikri/parser.rb', line 460 def match_attribute(name, other = nil, &block) select(&compare_to_attribute(name, other, &block)) end |
#match_child(*children) ⇒ ValueArray
Returns an array containing nodes for which the specified attribute does not have a child node matching the given child name.
469 470 471 |
# File 'lib/krikri/parser.rb', line 469 def match_child(*children) select { |v| !(v.children & children).empty? } end |
#reject(*args, &block) ⇒ ValueArray
Wraps the result of Array#reject in a ValueArray
426 427 428 |
# File 'lib/krikri/parser.rb', line 426 def reject(*args, &block) self.class.new(@array.reject(*args, &block), bindings) 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.
486 487 488 |
# File 'lib/krikri/parser.rb', line 486 def reject_attribute(name, other = nil, &block) reject(&compare_to_attribute(name, other, &block)) end |
#reject_child(*children) ⇒ ValueArray
Returns an array containing nodes for which the specified attribute does not have a childnode matching the given child name.
495 496 497 |
# File 'lib/krikri/parser.rb', line 495 def reject_child(*children) reject { |v| !(v.children & children).empty? } end |
#select(*args, &block) ⇒ ValueArray
Wraps the result of Array#select in a ValueArray
417 418 419 |
# File 'lib/krikri/parser.rb', line 417 def select(*args, &block) self.class.new(@array.select(*args, &block), bindings) end |
#values ⇒ Array
Returns literal values from the objects in this array.
266 267 268 |
# File 'lib/krikri/parser.rb', line 266 def values @array.map { |v| v.respond_to?(:value) ? v.value : v } end |