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.
my_value_array.field('dc:creator', 'foaf:name')
.match_attribute('first_name').values
Methods defined on this class should return another ValueArray, an Array of literal values (retrieved from Parser::Value#value), or a single literal value.
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
-
#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
-
#initialize(array = []) ⇒ 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 = []) ⇒ ValueArray
Returns a new instance of ValueArray.
156 157 158 |
# File 'lib/krikri/parser.rb', line 156 def initialize(array = []) @array = array end |
Class Method Details
.build(record) ⇒ ValueArray
Wraps the root node of the given record in this class.
331 332 333 |
# File 'lib/krikri/parser.rb', line 331 def self.build(record) new([record.root]) end |
Instance Method Details
#<<(value) ⇒ Object
172 173 174 175 176 |
# File 'lib/krikri/parser.rb', line 172 def <<(value) raise InvalidParserValueError unless value.is_a? Value @array << value value end |
#[]=(index, value) ⇒ Object
163 164 165 166 167 |
# File 'lib/krikri/parser.rb', line 163 def []=(index, value) raise InvalidParserValueError unless value.is_a? Value @array[index] = value self end |
#concat(*args, &block) ⇒ ValueArray
181 182 183 |
# File 'lib/krikri/parser.rb', line 181 def concat(*args, &block) self.class.new(@array.concat(*args, &block)) end |
#field(*args) ⇒ ValueArray
Accesses a given field. Use multiple arguments to travel down the node hierarchy.
198 199 200 201 202 203 204 |
# File 'lib/krikri/parser.rb', line 198 def field(*args) result = self args.each do |name| result = result.get_field(name) end result end |
#fields(*args) ⇒ ValueArray
Accesses the union of multiple specified fields.
given fields.
211 212 213 214 215 216 |
# File 'lib/krikri/parser.rb', line 211 def fields(*args) results = args.map do |f| field(*Array(f)) end self.class.new(results.flatten) 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.
224 225 226 227 |
# File 'lib/krikri/parser.rb', line 224 def first_value(*args) return self.class.new(@array.first(*args)) unless args.empty? self.class.new([@array.first].compact) end |
#flatten(*args, &block) ⇒ ValueArray
244 245 246 |
# File 'lib/krikri/parser.rb', line 244 def flatten(*args, &block) self.class.new(@array.flatten(*args, &block)) 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.
235 236 237 238 |
# File 'lib/krikri/parser.rb', line 235 def last_value(*args) return self.class.new(@array.last(*args)) unless args.empty? self.class.new([@array.last].compact) end |
#map(*args, &block) ⇒ ValueArray
Wraps the result of Array#map in a ValueArray
253 254 255 |
# File 'lib/krikri/parser.rb', line 253 def map(*args, &block) self.class.new(@array.map(*args, &block)) 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.
305 306 307 |
# File 'lib/krikri/parser.rb', line 305 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
271 272 273 |
# File 'lib/krikri/parser.rb', line 271 def reject(*args, &block) self.class.new(@array.reject(*args, &block)) 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.
322 323 324 |
# File 'lib/krikri/parser.rb', line 322 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
262 263 264 |
# File 'lib/krikri/parser.rb', line 262 def select(*args, &block) self.class.new(@array.select(*args, &block)) end |
#values ⇒ Array
Returns literal values from the objects in this array.
188 189 190 |
# File 'lib/krikri/parser.rb', line 188 def values @array.map { |v| v.respond_to?(:value) ? v.value : v } end |