Class: ActiveTriples::Relation
- Inherits:
-
Object
- Object
- ActiveTriples::Relation
- Includes:
- Enumerable
- Defined in:
- lib/active_triples/relation.rb
Overview
A ‘Relation` represents the values of a specific property/predicate on an RDFSource. Each relation is a set (Array) of RDF::Terms that are objects in the of source’s triples of the form:
<{#parent}> <{#predicate}> [term] .
Relations express a set of binary relationships (on a predicate) between the parent node and a term.
When the term is a URI or Blank Node, it is represented in the results Array as an RDFSource with a graph selected as a subgraph of the parent’s. The triples in this subgraph are: (a) those whose subject is the term; (b) …
Defined Under Namespace
Classes: ValueError
Instance Attribute Summary collapse
-
#parent ⇒ RDFSource
The resource that is the domain of this relation.
- #reflections ⇒ Class readonly
- #rel_args ⇒ Hash
- #value_arguments ⇒ Array<Object>
Instance Method Summary collapse
-
#<<(values) ⇒ Relation
(also: #push)
Adds values to the result set.
-
#build(attributes = {}) ⇒ Object
Builds a node with the given attributes, adding it to the relation.
-
#clear ⇒ Relation
Empties the ‘Relation`, deleting any associated triples from `parent`.
-
#delete(value) ⇒ ActiveTriples::Relation
Self.
-
#delete?(value) ⇒ Object?
A variation on ‘#delete`.
-
#first_or_create(attributes = {}) ⇒ Object
The first result, if present; else a newly built node.
-
#initialize(parent_source, value_arguments) ⇒ Relation
constructor
A new instance of Relation.
-
#predicate ⇒ RDF::Term?
Gives the predicate used by the Relation.
-
#property ⇒ Symbol, RDF::URI
Returns the property for the Relation.
-
#property_config ⇒ Hash<Symbol, ]
Hash<Symbol, ].
-
#result ⇒ Array<Object>
Gives an Array containing the result set for the Relation.
-
#set(values) ⇒ Relation
Adds values to the relation.
-
#subtract(*values) ⇒ Relation
Self.
-
#swap(swap_out, swap_in) ⇒ Relation
Replaces the first argument with the second as a value within the relation.
Constructor Details
#initialize(parent_source, value_arguments) ⇒ Relation
Returns a new instance of Relation.
43 44 45 46 47 48 49 50 |
# File 'lib/active_triples/relation.rb', line 43 def initialize(parent_source, value_arguments) self.parent = parent_source @reflections = parent_source.reflections self.rel_args ||= {} self.rel_args = value_arguments.pop if value_arguments.is_a?(Array) && value_arguments.last.is_a?(Hash) self.value_arguments = value_arguments end |
Instance Attribute Details
#parent ⇒ RDFSource
Returns the resource that is the domain of this relation.
33 34 35 |
# File 'lib/active_triples/relation.rb', line 33 def parent @parent end |
#reflections ⇒ Class (readonly)
33 |
# File 'lib/active_triples/relation.rb', line 33 attr_accessor :parent, :value_arguments, :rel_args |
#rel_args ⇒ Hash
33 |
# File 'lib/active_triples/relation.rb', line 33 attr_accessor :parent, :value_arguments, :rel_args |
#value_arguments ⇒ Array<Object>
33 |
# File 'lib/active_triples/relation.rb', line 33 attr_accessor :parent, :value_arguments, :rel_args |
Instance Method Details
#<<(values) ⇒ Relation Also known as: push
Adds values to the result set
303 304 305 306 |
# File 'lib/active_triples/relation.rb', line 303 def <<(values) values = Array.wrap(result) | Array.wrap(values) self.set(values) end |
#build(attributes = {}) ⇒ Object
Builds a node with the given attributes, adding it to the relation.
Nodes are built using the configured ‘class_name` for the relation. Attributes passed in the Hash argument are set on the new node through `RDFSource#attributes=`. If the attribute keys are not valid properties on the built node, we raise an error.
@todo: clarify class behavior; it is actually tied to type, in some cases.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/active_triples/relation.rb', line 182 def build(attributes={}) new_subject = attributes.fetch('id') { RDF::Node.new } make_node(new_subject).tap do |node| node.attributes = attributes.except('id') if parent.kind_of? List::ListResource parent.list << node elsif node.kind_of? RDF::List self.push node.rdf_subject else self.push node end end end |
#clear ⇒ Relation
Empties the ‘Relation`, deleting any associated triples from `parent`.
56 57 58 59 60 |
# File 'lib/active_triples/relation.rb', line 56 def clear parent.delete([rdf_subject, predicate, nil]) self end |
#delete(value) ⇒ ActiveTriples::Relation
this method behaves somewhat differently from ‘Array#delete`. It succeeds on deletion of non-existing values, always returning `self` unless an error is raised. There is no option to pass a block to evaluate if the value is not present. This is because access for `value` depends on query time. i.e. the `Relation` set does not have an underlying efficient data structure allowing a reliably cheap existence check.
symbols are treated as RDF::Nodes by default in ‘RDF::Mutable#delete`, but may also represent tokens in statements. This casts symbols to a literals, which gets us symmetric behavior between `#set(:sym)` and `#delete(:sym)`.
Returns self.
225 226 227 228 229 230 |
# File 'lib/active_triples/relation.rb', line 225 def delete(value) value = RDF::Literal(value) if value.is_a? Symbol parent.delete([rdf_subject, predicate, value]) self end |
#delete?(value) ⇒ Object?
A variation on ‘#delete`. This queries the relation for matching values before running the deletion, returning `nil` if it does not exist.
241 242 243 244 245 246 247 248 |
# File 'lib/active_triples/relation.rb', line 241 def delete?(value) value = RDF::Literal(value) if value.is_a? Symbol return nil if parent.query([rdf_subject, predicate, value]).nil? delete(value) value end |
#first_or_create(attributes = {}) ⇒ Object
Returns the first result, if present; else a newly built node.
293 294 295 |
# File 'lib/active_triples/relation.rb', line 293 def first_or_create(attributes={}) result.first || build(attributes) end |
#predicate ⇒ RDF::Term?
Gives the predicate used by the Relation. Values of this object are those that match the pattern ‘<rdf_subject> <predicate> [value] .`
336 337 338 339 |
# File 'lib/active_triples/relation.rb', line 336 def predicate return property if property.is_a?(RDF::Term) property_config[:predicate] if is_property? end |
#property ⇒ Symbol, RDF::URI
Returns the property for the Relation. This may be a registered property key or an RDF::URI.
324 325 326 |
# File 'lib/active_triples/relation.rb', line 324 def property value_arguments.last end |
#property_config ⇒ Hash<Symbol, ]
find a way to simplify this?
Returns Hash<Symbol, ].
312 313 314 315 316 |
# File 'lib/active_triples/relation.rb', line 312 def property_config return type_property if is_type? reflections.reflect_on_property(property) end |
#result ⇒ Array<Object>
Gives an Array containing the result set for the ActiveTriples::Relation.
By default, RDF::URI and RDF::Node results are cast to ‘RDFSource`. Literal results are given as their `#object` representations (e.g. String, Date.
When ‘cast?` is `false`, RDF::Resource values are left in their raw form. Similarly, when `#return_literals?` is `true`, literals are returned in their RDF::Literal form, preserving language tags, datatype, and value.
100 101 102 103 104 105 106 107 108 |
# File 'lib/active_triples/relation.rb', line 100 def result return [] if predicate.nil? statements = parent.query(:subject => rdf_subject, :predicate => predicate) statements.each_with_object([]) do |x, collector| converted_object = convert_object(x.object) collector << converted_object unless converted_object.nil? end end |
#set(values) ⇒ Relation
Adds values to the relation
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_triples/relation.rb', line 125 def set(values) raise UndefinedPropertyError.new(property, reflections) if predicate.nil? values = values.to_a if values.is_a? Relation values = [values].compact unless values.kind_of?(Array) clear values.each { |val| set_value(val) } parent.persist! if parent.persistence_strategy.is_a? ParentStrategy self end |
#subtract(enum) ⇒ Relation #subtract(*values) ⇒ Relation
This casts symbols to a literals, which gets us symmetric behavior with ‘#set(:sym)`.
Returns self.
263 264 265 266 267 268 269 270 271 272 |
# File 'lib/active_triples/relation.rb', line 263 def subtract(*values) values = values.first if values.first.is_a? Enumerable statements = values.map do |value| value = RDF::Literal(value) if value.is_a? Symbol [rdf_subject, predicate, value] end parent.delete(*statements) self end |
#swap(swap_out, swap_in) ⇒ Relation
Replaces the first argument with the second as a value within the relation.
285 286 287 |
# File 'lib/active_triples/relation.rb', line 285 def swap(swap_out, swap_in) self.<<(swap_in) if delete?(swap_out) end |