Module: ActiveFedora::SemanticNode

Extended by:
ActiveSupport::Concern
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

#load_from_solrObject

Returns the value of attribute load_from_solr.



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

def load_from_solr
  @load_from_solr
end

#relationships_loadedObject

Returns the value of attribute relationships_loaded.



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

def relationships_loaded
  @relationships_loaded
end

#subjectObject

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#add_relationship(predicate, target, literal = false) ⇒ Object

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

Parameters:

  • predicate (Symbol, String)
  • target (URI, ActiveFedora::Base)

    Either a string URI or an object that is a kind of ActiveFedora::Base



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

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

#assert_kind_of(n, o, t) ⇒ Object



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

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

#clear_relationship(predicate) ⇒ Object

Clears all relationships with the specified predicate

Parameters:

  • predicate


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

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

#clear_relationshipsObject



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

def clear_relationships
  @relationships_loaded = false
  @object_relations = nil
end

#conforms_to?(model_class) ⇒ Boolean

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.

Parameters:

  • model_class (Class)

    the model class name to check if an object conforms_to that model

Returns:

  • (Boolean)

    true if this object conforms to the given model name



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

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

#ids_for_outbound(predicate) ⇒ Object



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

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

#internal_uriString

Returns the internal fedora URI.

Returns:

  • (String)

    the internal fedora URI



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

def internal_uri
  self.class.internal_uri(pid)
end

#load_relationshipsObject



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

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

#object_relationsObject



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

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

#relationships(*args) ⇒ Object

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



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

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

#relationships=(xml) ⇒ Object



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

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

#relationships_are_dirty?Boolean Also known as: relationships_are_dirty

Returns:

  • (Boolean)


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

def relationships_are_dirty?
  object_relations.dirty
end

#relationships_are_not_dirty!Object



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

def relationships_are_not_dirty!
  object_relations.dirty = false
end

#remove_relationship(predicate, obj, literal = false) ⇒ Object

Remove a Rels-Ext relationship from the Object.

Parameters:

  • predicate
  • obj

    Either a string URI or an object that responds to .pid



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

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