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_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>
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/active_fedora/delegating.rb', line 74 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 define_attribute_method fields.first create_delegate_reader(fields.first, ) create_delegate_setter(fields.first, ) 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>
108 109 110 111 112 113 114 115 |
# File 'lib/active_fedora/delegating.rb', line 108 def delegate_to(datastream,fields,args={}) define_attribute_methods fields fields.each do |f| args.merge!({:to=>datastream}) create_delegate_reader(f, args) create_delegate_setter(f, args) end end |
#delegates ⇒ Object
44 45 46 47 48 49 |
# File 'lib/active_fedora/delegating.rb', line 44 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
51 52 53 |
# File 'lib/active_fedora/delegating.rb', line 51 def delegates= val @local_delegates = val end |
#unique?(field) ⇒ Boolean
Reveal if the delegated field is unique or not
120 121 122 |
# File 'lib/active_fedora/delegating.rb', line 120 def unique?(field) delegates[field][:unique] end |