Module: ActiveFedora::Delegating::ClassMethods

Defined in:
lib/active_fedora/delegating.rb

Instance Method Summary collapse

Instance Method Details

#delegate(field, args = {}) ⇒ Object

Provides a delegate class method to expose methods in metadata streams as member of the base object. Pass the target datastream via the :to argument. If you want to return a unique result, (e.g. string instead of an array) set the :unique argument to true.

The optional :at argument provides a terminology that the delegate will point to.

class Foo < ActiveFedora::Base
   :name => "descMetadata", :type => MyDatastream

  delegate :field1, :to=>"descMetadata", :unique=>true
  delegate :field2, :to=>"descMetadata", :at=>[:term1, :term2]
end

foo = Foo.new
foo.field1 = "My Value"
foo.field1                 # => "My Value"
foo.field2                 # => NoMethodError: undefined method `field2' for #<Foo:0x1af30c>


25
26
27
28
# File 'lib/active_fedora/delegating.rb', line 25

def delegate(field, args ={})
  create_delegate_accessor(field, args)
  create_delegate_setter(field, args)
end

#delegate_to(datastream, fields, args = {}) ⇒ Object

Allows you to delegate multiple terminologies to the same datastream, instead having to call the method each time for each term. The target datastream is the first argument, followed by an array of the terms that will point to that datastream. Terms must be a single value, ie. :field and not [:term1, :term2]. This is best accomplished by refining your OM terminology using :ref or :proxy methods to reduce nested terms down to one.

class Foo < ActiveFedora::Base
   :name => "descMetadata", :type => MyDatastream

  delegate_to :descMetadata, [:field1, :field2]
end

foo = Foo.new
foo.field1 = "My Value"
foo.field1                 # => "My Value"
foo.field2                 # => NoMethodError: undefined method `field2' for #<Foo:0x1af30c>


49
50
51
52
53
54
55
# File 'lib/active_fedora/delegating.rb', line 49

def delegate_to(datastream,fields,args={})
  fields.each do |f|
    args.merge!({:to=>datastream})
    create_delegate_accessor(f, args)
    create_delegate_setter(f, args)
  end
end