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


139
140
141
# File 'lib/krikri/parser.rb', line 139

def initialize(array = [])
  @array = array
end

Class Method Details

.build(record) ⇒ ValueArray

Wraps the root node of the given record in this class.


256
257
258
# File 'lib/krikri/parser.rb', line 256

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

Instance Method Details

#<<(value) ⇒ Object

Raises:

See Also:

  • Array#<<

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

def <<(value)
  raise InvalidParserValueError unless value.is_a? Value
  @array << value
  value
end

#[]=(index, value) ⇒ Object

Raises:

See Also:

  • Array#[]=

146
147
148
149
150
# File 'lib/krikri/parser.rb', line 146

def []=(index, value)
  raise InvalidParserValueError unless value.is_a? Value
  @array[index] = value
  self
end

#concat(*args, &block) ⇒ ValueArray

See Also:

  • Array#concat

164
165
166
# File 'lib/krikri/parser.rb', line 164

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.


181
182
183
184
185
186
187
# File 'lib/krikri/parser.rb', line 181

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.


194
195
196
197
198
199
# File 'lib/krikri/parser.rb', line 194

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.


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

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

See Also:

  • Array#concat

215
216
217
# File 'lib/krikri/parser.rb', line 215

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

#match_attribute(name, other) ⇒ ValueArray


244
245
246
247
248
249
# File 'lib/krikri/parser.rb', line 244

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) ⇒ ValueArray

Wraps the result of Array#reject in a ValueArray

See Also:

  • Array#reject

233
234
235
# File 'lib/krikri/parser.rb', line 233

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

#select(*args, &block) ⇒ ValueArray

Wraps the result of Array#select in a ValueArray

See Also:

  • Array#select

224
225
226
# File 'lib/krikri/parser.rb', line 224

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

#valuesArray

Returns literal values from the objects in this array.


171
172
173
# File 'lib/krikri/parser.rb', line 171

def values
  map(&:value)
end