Class: Krikri::Parser::ValueArray

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

Returns:



331
332
333
# File 'lib/krikri/parser.rb', line 331

def self.build(record)
  new([record.root])
end

Instance Method Details

#<<(value) ⇒ Object

Raises:

See Also:

  • Array#<<


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

Raises:

See Also:

  • Array#[]=


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

Returns:

See Also:

  • Array#concat


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.

Returns:

  • (ValueArray)

    an array containing the nodes available in a particular field.



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.

Returns:

  • (ValueArray)

    an array containing the nodes available in the



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.

Returns:

  • (ValueArray)

    a Krikri::Parser::ValueArray for first n elements



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

Returns:

See Also:

  • Array#concat


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.

Returns:

  • (ValueArray)

    a Krikri::Parser::ValueArray for last n elements



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

Returns:

See Also:

  • Array#map


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.

Examples:

selecting by presence of an attribute; returns all nodes where

`#attribute?(:type)` is true

match_attribute(:type) 

selecting by the value of an attribute; returns all nodes with

`#attribute(:type) == other`

match_attribute(:type, other)

selecting by block against an attribute; returns all nodes with

`block.call(attribute(:type))` is true

match_attribute(:type) { |value| value.starts_with? 'blah' }

selecting by block against an attribute; returns all nodes with

`block.call(attribute(:type)) == other` is true

match_attribute(:type, 'moomin') { |value| value.downcase }

Parameters:

  • name (#to_sym)

    an attribute name

  • other (Object) (defaults to: nil)

    an object to check for equality with the values from the given attribute.

Yields:

  • (value)

    yields each value with the attribute in name to the block

Returns:

  • (ValueArray)

    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

Returns:

See Also:

  • Array#reject


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.

Parameters:

  • name (#to_sym)

    an attribute name

  • other (Object) (defaults to: nil)

    an object to check for equality with the values from the given attribute.

Yields:

  • (value)

    yields each value with the attribute in name to the block

Returns:

  • (ValueArray)

    an array containing nodes for which the specified attribute does not have a value matching the given attribute name, object, and block.

See Also:



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

Returns:

See Also:

  • Array#select


262
263
264
# File 'lib/krikri/parser.rb', line 262

def select(*args, &block)
  self.class.new(@array.select(*args, &block))
end

#valuesArray

Returns literal values from the objects in this array.

Returns:

  • (Array)

    literal values from the objects in this array.

See Also:



188
189
190
# File 'lib/krikri/parser.rb', line 188

def values
  @array.map { |v| v.respond_to?(:value) ? v.value : v }
end