Class: ActiveFedora::Associations::Builder::Association
- Inherits:
-
Object
- Object
- ActiveFedora::Associations::Builder::Association
- Defined in:
- lib/active_fedora/associations/builder/association.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Attribute Summary collapse
-
#mixin ⇒ Object
readonly
Returns the value of attribute mixin.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.build(model, name, options) ⇒ Object
configure_dependency.
- .define_accessors(model, reflection) ⇒ Object
- .define_callbacks(model, reflection) ⇒ Object
- .define_readers(mixin, name) ⇒ Object
- .define_writers(mixin, name) ⇒ Object
Instance Method Summary collapse
- #build ⇒ Object
- #configure_dependency ⇒ Object
-
#initialize(model, name, options) ⇒ Association
constructor
A new instance of Association.
-
#predicate(property) ⇒ Object
Returns the RDF predicate as defined by the :property attribute.
- #translate_property_to_predicate ⇒ Object
- #validate_options ⇒ Object
Constructor Details
#initialize(model, name, options) ⇒ Association
Returns a new instance of Association.
19 20 21 22 23 24 25 |
# File 'lib/active_fedora/associations/builder/association.rb', line 19 def initialize(model, name, ) @model = model @name = name @options = translate_property_to_predicate end |
Instance Attribute Details
#mixin ⇒ Object (readonly)
Returns the value of attribute mixin.
9 10 11 |
# File 'lib/active_fedora/associations/builder/association.rb', line 9 def mixin @mixin end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/active_fedora/associations/builder/association.rb', line 9 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/active_fedora/associations/builder/association.rb', line 9 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/active_fedora/associations/builder/association.rb', line 9 def @options end |
Class Method Details
.build(model, name, options) ⇒ Object
configure_dependency
12 13 14 15 16 17 |
# File 'lib/active_fedora/associations/builder/association.rb', line 12 def self.build(model, name, ) reflection = new(model, name, ).build define_accessors(model, reflection) define_callbacks(model, reflection) reflection end |
.define_accessors(model, reflection) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/active_fedora/associations/builder/association.rb', line 55 def self.define_accessors(model, reflection) mixin = model.generated_association_methods name = reflection.name define_readers(mixin, name) define_writers(mixin, name) end |
.define_callbacks(model, reflection) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/active_fedora/associations/builder/association.rb', line 48 def self.define_callbacks(model, reflection) if dependent = reflection.[:dependent] (dependent) add_destroy_callbacks(model, reflection) end end |
.define_readers(mixin, name) ⇒ Object
62 63 64 65 66 |
# File 'lib/active_fedora/associations/builder/association.rb', line 62 def self.define_readers(mixin, name) mixin.send(:define_method, name) do |*params| association(name).reader(*params) end end |
.define_writers(mixin, name) ⇒ Object
68 69 70 71 72 |
# File 'lib/active_fedora/associations/builder/association.rb', line 68 def self.define_writers(mixin, name) mixin.send(:define_method, "#{name}=") do |value| association(name).writer(value) end end |
Instance Method Details
#build ⇒ Object
27 28 29 30 |
# File 'lib/active_fedora/associations/builder/association.rb', line 27 def build configure_dependency if [:dependent] # see https://github.com/rails/rails/commit/9da52a5e55cc665a539afb45783f84d9f3607282 model.create_reflection(self.class.macro, name, , model) end |
#configure_dependency ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_fedora/associations/builder/association.rb', line 74 def configure_dependency return unless [:dependent] unless [:destroy, :delete].include?([:dependent]) raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{[:dependent].inspect})" end method_name = "belongs_to_dependent_#{[:dependent]}_for_#{name}" model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1) def #{method_name} association = #{name} association.#{[:dependent]} if association end eoruby model.after_destroy method_name end |
#predicate(property) ⇒ Object
Returns the RDF predicate as defined by the :property attribute
43 44 45 46 |
# File 'lib/active_fedora/associations/builder/association.rb', line 43 def predicate(property) return property if property.is_a? RDF::URI ActiveFedora::Predicates.find_graph_predicate(property) end |
#translate_property_to_predicate ⇒ Object
32 33 34 35 36 |
# File 'lib/active_fedora/associations/builder/association.rb', line 32 def translate_property_to_predicate return unless [:property] Deprecation.warn Association, "the :property option to `#{model}.#{macro} :#{name}' is deprecated and will be removed in active-fedora 10.0. Use :predicate instead", caller(5) [:predicate] = predicate(.delete(:property)) end |
#validate_options ⇒ Object
38 39 40 |
# File 'lib/active_fedora/associations/builder/association.rb', line 38 def .assert_valid_keys(self.class.) end |