Class: ActiveTriples::Relation
- Inherits:
-
Object
- Object
- ActiveTriples::Relation
- 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, then, express n binary relationships between the parent node and a term.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#node_cache ⇒ Object
writeonly
Sets the attribute node_cache.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#reflections ⇒ Object
readonly
Returns the value of attribute reflections.
-
#rel_args ⇒ Object
Returns the value of attribute rel_args.
-
#value_arguments ⇒ Object
Returns the value of attribute value_arguments.
Instance Method Summary collapse
- #<<(values) ⇒ Object (also: #push)
- #[]=(index, value) ⇒ Object
- #build(attributes = {}) ⇒ Object
- #clear ⇒ Object
- #delete(*values) ⇒ Object
- #empty_property ⇒ Object
- #first_or_create(attributes = {}) ⇒ Object
-
#initialize(parent_source, value_arguments) ⇒ Relation
constructor
A new instance of Relation.
- #property ⇒ Object
- #property_config ⇒ Object
- #reset! ⇒ Object
- #result ⇒ Object
- #set(values) ⇒ Object
- #type_property ⇒ Object
Constructor Details
#initialize(parent_source, value_arguments) ⇒ Relation
Returns a new instance of Relation.
22 23 24 25 26 27 |
# File 'lib/active_triples/relation.rb', line 22 def initialize(parent_source, value_arguments) self.parent = parent_source @reflections = parent_source.reflections self.rel_args ||= {} self.value_arguments = value_arguments end |
Instance Attribute Details
#node_cache=(value) ⇒ Object
Sets the attribute node_cache
17 18 19 |
# File 'lib/active_triples/relation.rb', line 17 def node_cache=(value) @node_cache = value end |
#parent ⇒ Object
Returns the value of attribute parent.
17 18 19 |
# File 'lib/active_triples/relation.rb', line 17 def parent @parent end |
#reflections ⇒ Object (readonly)
Returns the value of attribute reflections.
18 19 20 |
# File 'lib/active_triples/relation.rb', line 18 def reflections @reflections end |
#rel_args ⇒ Object
Returns the value of attribute rel_args.
17 18 19 |
# File 'lib/active_triples/relation.rb', line 17 def rel_args @rel_args end |
#value_arguments ⇒ Object
Returns the value of attribute value_arguments.
17 18 19 |
# File 'lib/active_triples/relation.rb', line 17 def value_arguments @value_arguments end |
Instance Method Details
#<<(values) ⇒ Object Also known as: push
90 91 92 93 |
# File 'lib/active_triples/relation.rb', line 90 def << (values) values = Array.wrap(result) | Array.wrap(values) self.set(values) end |
#[]=(index, value) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/active_triples/relation.rb', line 97 def []=(index, value) values = Array.wrap(result) raise IndexError, "Index #{index} out of bounds." if values[index].nil? values[index] = value self.set(values) end |
#build(attributes = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/active_triples/relation.rb', line 66 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 ⇒ Object
36 37 38 |
# File 'lib/active_triples/relation.rb', line 36 def clear set(nil) end |
#delete(*values) ⇒ Object
84 85 86 87 88 |
# File 'lib/active_triples/relation.rb', line 84 def delete(*values) values.each do |value| parent.delete([rdf_subject, predicate, value]) end end |
#empty_property ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/active_triples/relation.rb', line 58 def empty_property parent.query([rdf_subject, predicate, nil]).each_statement do |statement| if !uri_class(statement.object) || uri_class(statement.object) == class_for_property parent.delete(statement) end end end |
#first_or_create(attributes = {}) ⇒ Object
80 81 82 |
# File 'lib/active_triples/relation.rb', line 80 def first_or_create(attributes={}) result.first || build(attributes) end |
#property ⇒ Object
116 117 118 |
# File 'lib/active_triples/relation.rb', line 116 def property value_arguments.last end |
#property_config ⇒ Object
104 105 106 107 |
# File 'lib/active_triples/relation.rb', line 104 def property_config return type_property if (property == RDF.type || property.to_s == "type") && (!reflections.kind_of?(RDFSource) || !reflections.reflect_on_property(property)) reflections.reflect_on_property(property) end |
#reset! ⇒ Object
113 114 |
# File 'lib/active_triples/relation.rb', line 113 def reset! end |
#result ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/active_triples/relation.rb', line 40 def result parent.query(:subject => rdf_subject, :predicate => predicate) .each_with_object([]) do |x, collector| converted_object = convert_object(x.object) collector << converted_object unless converted_object.nil? end end |
#set(values) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/active_triples/relation.rb', line 48 def set(values) values = [values].compact unless values.kind_of?(Array) values = values.to_a if values.class == Relation empty_property values.each do |val| set_value(val) end parent.persist! if parent.class.repository == :parent && parent.send(:repository) end |
#type_property ⇒ Object
109 110 111 |
# File 'lib/active_triples/relation.rb', line 109 def type_property { :predicate => RDF.type, :cast => false } end |