Class: ActiveTriples::Term
- Inherits:
-
Object
- Object
- ActiveTriples::Term
- Defined in:
- lib/active_triples/term.rb
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.
-
#term_args ⇒ Object
Returns the value of attribute term_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_resource, value_arguments) ⇒ Term
constructor
A new instance of Term.
- #property ⇒ Object
- #property_config ⇒ Object
- #reset! ⇒ Object
- #result ⇒ Object
- #set(values) ⇒ Object
- #type_property ⇒ Object
Constructor Details
#initialize(parent_resource, value_arguments) ⇒ Term
Returns a new instance of Term.
11 12 13 14 15 |
# File 'lib/active_triples/term.rb', line 11 def initialize(parent_resource, value_arguments) self.parent = parent_resource @reflections = parent_resource.reflections self.value_arguments = value_arguments end |
Instance Attribute Details
#node_cache=(value) ⇒ Object
Sets the attribute node_cache
6 7 8 |
# File 'lib/active_triples/term.rb', line 6 def node_cache=(value) @node_cache = value end |
#parent ⇒ Object
Returns the value of attribute parent.
6 7 8 |
# File 'lib/active_triples/term.rb', line 6 def parent @parent end |
#reflections ⇒ Object (readonly)
Returns the value of attribute reflections.
7 8 9 |
# File 'lib/active_triples/term.rb', line 7 def reflections @reflections end |
#term_args ⇒ Object
Returns the value of attribute term_args.
6 7 8 |
# File 'lib/active_triples/term.rb', line 6 def term_args @term_args end |
#value_arguments ⇒ Object
Returns the value of attribute value_arguments.
6 7 8 |
# File 'lib/active_triples/term.rb', line 6 def value_arguments @value_arguments end |
Instance Method Details
#<<(values) ⇒ Object Also known as: push
78 79 80 81 |
# File 'lib/active_triples/term.rb', line 78 def << (values) values = Array.wrap(result) | Array.wrap(values) self.set(values) end |
#[]=(index, value) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/active_triples/term.rb', line 85 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
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_triples/term.rb', line 54 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
24 25 26 |
# File 'lib/active_triples/term.rb', line 24 def clear set(nil) end |
#delete(*values) ⇒ Object
72 73 74 75 76 |
# File 'lib/active_triples/term.rb', line 72 def delete(*values) values.each do |value| parent.delete([rdf_subject, predicate, value]) end end |
#empty_property ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/active_triples/term.rb', line 46 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
68 69 70 |
# File 'lib/active_triples/term.rb', line 68 def first_or_create(attributes={}) result.first || build(attributes) end |
#property ⇒ Object
104 105 106 |
# File 'lib/active_triples/term.rb', line 104 def property value_arguments.last end |
#property_config ⇒ Object
92 93 94 95 |
# File 'lib/active_triples/term.rb', line 92 def property_config return type_property if (property == RDF.type || property.to_s == "type") && (!reflections.kind_of?(Resource) || !reflections.reflect_on_property(property)) reflections.reflect_on_property(property) end |
#reset! ⇒ Object
101 102 |
# File 'lib/active_triples/term.rb', line 101 def reset! end |
#result ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/active_triples/term.rb', line 28 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
36 37 38 39 40 41 42 43 44 |
# File 'lib/active_triples/term.rb', line 36 def set(values) values = [values].compact unless values.kind_of?(Array) values = values.to_a if values.class == Term empty_property values.each do |val| set_value(val) end parent.persist! if parent.class.repository == :parent && parent.send(:repository) end |
#type_property ⇒ Object
97 98 99 |
# File 'lib/active_triples/term.rb', line 97 def type_property { :predicate => RDF.type, :cast => false } end |