Class: ActiveTriples::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/active_triples/term.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_resource, value_arguments) ⇒ Term

Returns a new instance of Term.



11
12
13
14
15
16
# File 'lib/active_triples/term.rb', line 11

def initialize(parent_resource, value_arguments)
  self.parent = parent_resource
  @reflections = parent_resource.reflections
  self.term_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.



6
7
8
# File 'lib/active_triples/term.rb', line 6

def node_cache=(value)
  @node_cache = value
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/active_triples/term.rb', line 6

def parent
  @parent
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



7
8
9
# File 'lib/active_triples/term.rb', line 7

def reflections
  @reflections
end

#term_argsObject

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_argumentsObject

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



79
80
81
82
# File 'lib/active_triples/term.rb', line 79

def << (values)
  values = Array.wrap(result) | Array.wrap(values)
  self.set(values)
end

#[]=(index, value) ⇒ Object

Raises:

  • (IndexError)


86
87
88
89
90
91
# File 'lib/active_triples/term.rb', line 86

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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_triples/term.rb', line 55

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



25
26
27
# File 'lib/active_triples/term.rb', line 25

def clear
  set(nil)
end

#delete(*values) ⇒ Object



73
74
75
76
77
# File 'lib/active_triples/term.rb', line 73

def delete(*values)
  values.each do |value|
    parent.delete([rdf_subject, predicate, value])
  end
end

#empty_propertyObject



47
48
49
50
51
52
53
# File 'lib/active_triples/term.rb', line 47

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



69
70
71
# File 'lib/active_triples/term.rb', line 69

def first_or_create(attributes={})
  result.first || build(attributes)
end

#propertyObject



105
106
107
# File 'lib/active_triples/term.rb', line 105

def property
  value_arguments.last
end

#property_configObject



93
94
95
96
# File 'lib/active_triples/term.rb', line 93

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



102
103
# File 'lib/active_triples/term.rb', line 102

def reset!
end

#resultObject



29
30
31
32
33
34
35
# File 'lib/active_triples/term.rb', line 29

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



37
38
39
40
41
42
43
44
45
# File 'lib/active_triples/term.rb', line 37

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_propertyObject



98
99
100
# File 'lib/active_triples/term.rb', line 98

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