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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array = []) ⇒ ValueArray

Returns a new instance of ValueArray.



137
138
139
# File 'lib/krikri/parser.rb', line 137

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:



208
209
210
# File 'lib/krikri/parser.rb', line 208

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

Instance Method Details

#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.



154
155
156
157
158
159
160
# File 'lib/krikri/parser.rb', line 154

def field(*args)
  result = self
  args.each do |name|
    result = result.get_field(name)
  end
  result
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



168
169
170
171
# File 'lib/krikri/parser.rb', line 168

def first_value(*args)
  return self.class.new(@array.first(*args)) unless args.empty?
  self.class.new([@array.first].compact)
end

#match_attribute(name, other) ⇒ ValueArray

Returns an array containing nodes for which the specified attribute has a value matching the given object.

Parameters:

  • name (#to_sym)

    an attribute name

  • other (Object)

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

Returns:

  • (ValueArray)

    an array containing nodes for which the specified attribute has a value matching the given object.



196
197
198
199
200
201
# File 'lib/krikri/parser.rb', line 196

def match_attribute(name, other)
  select do |v|
    next unless v.attribute?(name.to_sym)
    v.send(name).downcase == other.downcase
  end
end

#reject(*args, &block) ⇒ Object

Wraps the result of Array#reject in a ValueArray

See Also:

  • Array#reject


185
186
187
# File 'lib/krikri/parser.rb', line 185

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

#select(*args, &block) ⇒ Object

Wraps the result of Array#select in a ValueArray

See Also:

  • Array#select


177
178
179
# File 'lib/krikri/parser.rb', line 177

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:



144
145
146
# File 'lib/krikri/parser.rb', line 144

def values
  map(&:value)
end