Class: ActiveTriples::Relation

Inherits:
Object
  • Object
show all
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.

See Also:

  • RDF::Term

Direct Known Subclasses

Term

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute node_cache to.



17
18
19
# File 'lib/active_triples/relation.rb', line 17

def node_cache=(value)
  @node_cache = value
end

#parentObject

Returns the value of attribute parent.



17
18
19
# File 'lib/active_triples/relation.rb', line 17

def parent
  @parent
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



18
19
20
# File 'lib/active_triples/relation.rb', line 18

def reflections
  @reflections
end

#rel_argsObject

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_argumentsObject

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

Raises:

  • (IndexError)


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

#clearObject



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_propertyObject



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

#propertyObject



116
117
118
# File 'lib/active_triples/relation.rb', line 116

def property
  value_arguments.last
end

#property_configObject



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

#resultObject



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_propertyObject



109
110
111
# File 'lib/active_triples/relation.rb', line 109

def type_property
  { :predicate => RDF.type, :cast => false }
end