Module: ActiveFedora::Delegating::ClassMethods
- Defined in:
- lib/active_fedora/delegating.rb
Instance Method Summary collapse
-
#delegate(*methods) ⇒ Object
Provides a delegate class method to expose methods in metadata streams as member of the base object.
- #delegate_registry ⇒ Object
-
#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.
- #delegates ⇒ Object
- #delegates=(val) ⇒ Object
-
#unique?(field) ⇒ Boolean
Reveal if the delegated field is unique or not.
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 = fields.pop unless .is_a?(Hash) && to = [:to] raise ArgumentError, "Target is required" end if ds_specs.has_key? to.to_s create_delegate_reader(fields.first, ) create_delegate_setter(fields.first, ) else super(*methods) end end |
#delegate_registry ⇒ Object
78 79 80 |
# File 'lib/active_fedora/delegating.rb', line 78 def delegate_registry self.delegates.keys 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>
101 102 103 104 105 106 107 |
# File 'lib/active_fedora/delegating.rb', line 101 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 |
#delegates ⇒ Object
34 35 36 37 38 39 |
# File 'lib/active_fedora/delegating.rb', line 34 def delegates @local_delegates ||= {} 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
112 113 114 |
# File 'lib/active_fedora/delegating.rb', line 112 def unique?(field) delegates[field][:unique] end |