Module: Rubydora::RelationshipsMixin

Included in:
DigitalObject
Defined in:
lib/rubydora/relationships_mixin.rb

Overview

This model inject RELS-EXT-based helper methods for Fedora objects

Constant Summary collapse

RELS_EXT =

FIXME: This should probably be defined on the DigitalObject Map Rubydora accessors to Fedora RELS-EXT predicates

{"annotations"=>"info:fedora/fedora-system:def/relations-external#hasAnnotation",
"has_metadata"=>"info:fedora/fedora-system:def/relations-external#hasMetadata",
"description_of"=>"info:fedora/fedora-system:def/relations-external#isDescriptionOf",
"part_of"=>"info:fedora/fedora-system:def/relations-external#isPartOf",
"descriptions"=>"info:fedora/fedora-system:def/relations-external#hasDescription",
"dependent_of"=>"info:fedora/fedora-system:def/relations-external#isDependentOf",
"constituents"=>"info:fedora/fedora-system:def/relations-external#hasConstituent",
"parts"=>"info:fedora/fedora-system:def/relations-external#hasPart",
"memberOfCollection"=>"info:fedora/fedora-system:def/relations-external#isMemberOfCollection",
"member_of"=>"info:fedora/fedora-system:def/relations-external#isMemberOf",
"equivalents"=>"info:fedora/fedora-system:def/relations-external#hasEquivalent",
"derivations"=>"info:fedora/fedora-system:def/relations-external#hasDerivation",
"derivation_of"=>"info:fedora/fedora-system:def/relations-external#isDerivationOf",
"subsets"=>"info:fedora/fedora-system:def/relations-external#hasSubset",
"annotation_of"=>"info:fedora/fedora-system:def/relations-external#isAnnotationOf",
"metadata_for"=>"info:fedora/fedora-system:def/relations-external#isMetadataFor",
"dependents"=>"info:fedora/fedora-system:def/relations-external#hasDependent",
"subset_of"=>"info:fedora/fedora-system:def/relations-external#isSubsetOf",
"constituent_of"=>"info:fedora/fedora-system:def/relations-external#isConstituentOf",
"collection_members"=>"info:fedora/fedora-system:def/relations-external#hasCollectionMember",
"members"=>"info:fedora/fedora-system:def/relations-external#hasMember"}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

generate accessor methods for each RELS_EXT property



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubydora/relationships_mixin.rb', line 33

def self.included(base)

    # FIXME: ugly, but functional..
    RELS_EXT.each do |name, property|
      base.class_eval <<-RUBY
        def #{name.to_s} args = {}
          relationships[:#{name}] = nil if args.delete(:refetch)
          relationships[:#{name}] ||= relationship('#{property}', args)
        end

        def #{name.to_s}= arr
          arr &&= [arr] unless arr.is_a? Array
          old = #{name.to_s}.dup || []
          arr = relationships[:#{name}] = relationship('#{property}', :values => arr.flatten)
          relationship_changed('#{property}', {:+ => arr - old, :- => old - arr },  arr)

          arr
        end
      RUBY
    end
end

Instance Method Details

#add_relationship(predicate, object) ⇒ Object

Add a relationship for this object

Parameters:

Returns:

  • self



97
98
99
100
101
# File 'lib/rubydora/relationships_mixin.rb', line 97

def add_relationship predicate, object
  obj_uri = (( object.fqpid if object.respond_to? :fqpid ) || ( object.uri if object.respond_to? :uri ) || (object.to_s if object.respond_to? :to_s?) || object )
  repository.add_relationship :subject => fqpid, :predicate => predicate, :object => obj_uri
  self
end

#purge_relationship(predicate, object) ⇒ Object

Purge a relationship from this object

Parameters:

Returns:

  • self



107
108
109
110
# File 'lib/rubydora/relationships_mixin.rb', line 107

def purge_relationship predicate, object
  obj_uri = (( object.fqpid if object.respond_to? :fqpid ) || ( object.uri if object.respond_to? :uri ) || (object.to_s if object.respond_to? :to_s?) || object )
  repository.purge_relationship :subject => fqpid, :predicate => predicate, :object => obj_uri
end

#relationship(predicate, args = {}) ⇒ ArrayWithCallback<Rubydora::DigitalObject>

Provides an accessor to the ‘predicate` RELS-EXT relationship Using ArrayWithCallback, will commit any changes to Fedora

Parameters:

  • predicate (String)
  • args (Hash) (defaults to: {})

Options Hash (args):

  • :values (Array)

    if nil, will query the resource index for related objects

Returns:



63
64
65
66
67
68
# File 'lib/rubydora/relationships_mixin.rb', line 63

def relationship predicate, args = {}
  arr = ArrayWithCallback.new(args[:values] || repository.find_by_sparql_relationship(fqpid, predicate))
  arr.on_change << lambda { |arr, diff| relationship_changed(predicate, diff, arr) } 

  arr
end

#relationship_changed(predicate, diff, arr = []) ⇒ Object

Given a predicate and a diff between before and after states commit the appropriate changes to Fedora

Parameters:

  • predicate (String)
  • diff (Hash)
  • arr (Array) (defaults to: [])

    the current relationship state

Options Hash (diff):

  • :+ (Hash)

    additions

  • :- (Hash)

    deletions



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubydora/relationships_mixin.rb', line 80

def relationship_changed predicate, diff, arr = []
  diff[:+] ||= []
  diff[:-] ||= []

  diff[:+].each do |o| 
    add_relationship(predicate, o)
  end        

  diff[:-].each do |o| 
    purge_relationship(predicate, o)
  end        
end

#relationshipsHash

accessor to all retrieved relationships

Returns:

  • (Hash)


114
115
116
# File 'lib/rubydora/relationships_mixin.rb', line 114

def relationships
  @relationships ||= {}
end