Module: ActiveFedora::Delegating::ClassMethods

Defined in:
lib/active_fedora/delegating.rb

Instance Method Summary collapse

Instance Method Details

#delegate(*methods) ⇒ 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                 # => [""]
foo.field3                 # => NoMethodError: undefined method `field3' for #<Foo:0x1af30c>


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_fedora/delegating.rb', line 64

def delegate(*methods)
  fields = methods.dup
  options = fields.pop
  unless options.is_a?(Hash) && to = options[:to]
    raise ArgumentError, "Target is required"
  end
  if ds_specs.has_key? to.to_s 
    create_delegate_reader(fields.first, options)
    create_delegate_setter(fields.first, options)
  else
    super(*methods)
  end
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                 # => [""]
foo.field3                 # => NoMethodError: undefined method `field3' for #<Foo:0x1af30c>


97
98
99
100
101
102
103
# File 'lib/active_fedora/delegating.rb', line 97

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

#delegatesObject



34
35
36
37
38
39
# File 'lib/active_fedora/delegating.rb', line 34

def delegates
  @local_delegates ||= {}.with_indifferent_access
  return @local_delegates unless superclass.respond_to?(:delegates) and value = superclass.delegates
  @local_delegates = value.dup if @local_delegates.empty?
  @local_delegates
end

#delegates=(val) ⇒ Object



41
42
43
# File 'lib/active_fedora/delegating.rb', line 41

def delegates= val
  @local_delegates = val
end

#unique?(field) ⇒ Boolean

Reveal if the delegated field is unique or not

Parameters:

  • field (Symbol)

    the field to query

Returns:

  • (Boolean)


108
109
110
# File 'lib/active_fedora/delegating.rb', line 108

def unique?(field)
  delegates[field][:unique]
end