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.
Class Method Summary collapse
-
.build(record) ⇒ ValueArray
Wraps the root node of the given record in this class.
Instance Method Summary collapse
-
#field(*args) ⇒ ValueArray
Accesses a given field.
-
#first_value(*args) ⇒ ValueArray
Retrieves the first element of a ValueArray.
-
#initialize(array = []) ⇒ ValueArray
constructor
A new instance of ValueArray.
-
#match_attribute(name, other) ⇒ ValueArray
An array containing nodes for which the specified attribute has a value matching the given object.
-
#reject(*args, &block) ⇒ Object
Wraps the result of Array#reject in a ValueArray.
-
#select(*args, &block) ⇒ Object
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.
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.
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.
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.
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.
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
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
177 178 179 |
# File 'lib/krikri/parser.rb', line 177 def select(*args, &block) self.class.new(@array.select(*args, &block)) end |
#values ⇒ Array
Returns literal values from the objects in this array.
144 145 146 |
# File 'lib/krikri/parser.rb', line 144 def values map(&:value) end |