Module: ActiveFedora::SemanticNode

Extended by:
ActiveSupport::Concern, Deprecation
Included in:
Base
Defined in:
lib/active_fedora/semantic_node.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) load_from_solr

Returns the value of attribute load_from_solr



7
8
9
# File 'lib/active_fedora/semantic_node.rb', line 7

def load_from_solr
  @load_from_solr
end

- (Object) relationships_loaded

Returns the value of attribute relationships_loaded



6
7
8
# File 'lib/active_fedora/semantic_node.rb', line 6

def relationships_loaded
  @relationships_loaded
end

- (Object) subject

Returns the value of attribute subject



7
8
9
# File 'lib/active_fedora/semantic_node.rb', line 7

def subject
  @subject
end

Instance Method Details

- (Object) add_relationship(predicate, target, literal = false)

Add a relationship to the Object. TODO is target ever a AF::Base anymore?



49
50
51
52
53
# File 'lib/active_fedora/semantic_node.rb', line 49

def add_relationship(predicate, target, literal=false)
  #raise ArgumentError, "predicate must be a symbol. You provided `#{predicate.inspect}'" unless predicate.class.in?([Symbol, String])
  object_relations.add(predicate, target, literal)
  rels_ext.content_will_change! if object_relations.dirty
end

- (Object) assert_kind_of(n, o, t)



9
10
11
# File 'lib/active_fedora/semantic_node.rb', line 9

def assert_kind_of(n, o,t)
  raise "Assertion failure: #{n}: #{o} is not of type #{t}" unless o.kind_of?(t)
end

- (Object) clear_relationship(predicate)

Clears all relationships with the specified predicate



57
58
59
60
61
62
# File 'lib/active_fedora/semantic_node.rb', line 57

def clear_relationship(predicate)
  relationships(predicate).each do |target|
    object_relations.delete(predicate, target) 
  end
  rels_ext.content_will_change! if object_relations.dirty
end

- (Object) clear_relationships



13
14
15
16
# File 'lib/active_fedora/semantic_node.rb', line 13

def clear_relationships
  @relationships_loaded = false
  @object_relations = nil
end

- (Boolean) conforms_to?(model_class)

Checks that this object is matches the model class passed in. It requires two steps to pass to return true

1. It has a hasModel relationship of the same model
2. kind_of? returns true for the model passed in

This method can most often be used to detect if an object from Fedora that was created with a different model was then used to populate this object.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_fedora/semantic_node.rb', line 72

def conforms_to?(model_class)
  if self.kind_of?(model_class)
    #check has model and class match
    mod = relationships.first(:predicate=>Predicates.find_graph_predicate(:has_model))
    if mod
      expected = self.class.to_class_uri
      if mod.object.to_s == expected
        return true
      else
        raise "has_model relationship check failed for model #{model_class} raising exception, expected: '#{expected}' actual: '#{mod.object.to_s}'"
      end
    else
      raise "has_model relationship does not exist for model #{model_class} check raising exception"
    end
  else
    raise "kind_of? check failed for model #{model_class}, actual #{self.class} raising exception"
  end
  return false
end

- (Object) ids_for_outbound(predicate)



122
123
124
125
126
127
# File 'lib/active_fedora/semantic_node.rb', line 122

def ids_for_outbound(predicate)
  (object_relations[predicate] || []).map do |o|
    o = o.to_s if o.kind_of? RDF::Literal
    o.kind_of?(String) ? self.class.pid_from_uri(o) : o.pid
  end
end

- (String) internal_uri



130
131
132
# File 'lib/active_fedora/semantic_node.rb', line 130

def internal_uri
  self.class.internal_uri(pid)
end

- (Object) load_relationships



115
116
117
118
119
120
# File 'lib/active_fedora/semantic_node.rb', line 115

def load_relationships
  @relationships_loaded = true
  content = rels_ext.content
  return unless content.present?
  RelsExtDatastream.from_xml content, rels_ext
end

- (Object) object_relations



18
19
20
21
# File 'lib/active_fedora/semantic_node.rb', line 18

def object_relations
  load_relationships if !relationships_loaded
  @object_relations ||= RelationshipGraph.new
end

- (Object) relationships(*args)

If no arguments are supplied, return the whole RDF::Graph. if a predicate is supplied as a parameter, then it returns the result of quering the graph with that predicate



104
105
106
107
108
109
110
111
112
113
# File 'lib/active_fedora/semantic_node.rb', line 104

def relationships(*args)
  load_relationships unless relationships_loaded

  if args.empty?
    raise "Must have internal_uri" unless internal_uri
    return object_relations.to_graph(internal_uri)
  end
  rels = object_relations[args.first] || []
  rels.map {|o| o.respond_to?(:internal_uri) ? o.internal_uri : o }.compact   #TODO, could just return the object
end

- (Object) relationships=(xml)



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_fedora/semantic_node.rb', line 32

def relationships=(xml)
  RDF::RDFXML::Reader.new(xml) do |reader|
    reader.each_statement do |statement|
      literal = statement.object.kind_of?(RDF::Literal)
      object = literal ? statement.object.value : statement.object.to_str
      object_relations.add(statement.predicate, object, literal)
    end
  end
  # Adding the relationships to the graph causes the graph to be marked as dirty,
  # so now we assert that the graph is in sync
  relationships_are_not_dirty!
end

- (Boolean) relationships_are_dirty? Also known as: relationships_are_dirty



23
24
25
# File 'lib/active_fedora/semantic_node.rb', line 23

def relationships_are_dirty?
  object_relations.dirty
end

- (Object) relationships_are_not_dirty!



28
29
30
# File 'lib/active_fedora/semantic_node.rb', line 28

def relationships_are_not_dirty!
  object_relations.dirty = false
end

- (Object) remove_relationship(predicate, obj, literal = false)

Remove a Rels-Ext relationship from the Object.



96
97
98
99
100
# File 'lib/active_fedora/semantic_node.rb', line 96

def remove_relationship(predicate, obj, literal=false)
  object_relations.delete(predicate, obj)
  object_relations.dirty = true
  rels_ext.content_will_change!
end