Class: ActiveFedora::Associations::Builder::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/associations/builder/association.rb

Overview

:nodoc:

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options) ⇒ Association


19
20
21
22
23
# File 'lib/active_fedora/associations/builder/association.rb', line 19

def initialize(model, name, options)
  @model, @name, @options = model, name, options
  translate_property_to_predicate
  validate_options
end

Instance Attribute Details

#mixinObject (readonly)

Returns the value of attribute mixin


9
10
11
# File 'lib/active_fedora/associations/builder/association.rb', line 9

def mixin
  @mixin
end

#modelObject (readonly)

Returns the value of attribute model


9
10
11
# File 'lib/active_fedora/associations/builder/association.rb', line 9

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name


9
10
11
# File 'lib/active_fedora/associations/builder/association.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


9
10
11
# File 'lib/active_fedora/associations/builder/association.rb', line 9

def options
  @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, options)
  reflection = new(model, name, options).build
  define_accessors(model, reflection)
  define_callbacks(model, reflection)
  reflection
end

.define_accessors(model, reflection) ⇒ Object


54
55
56
57
58
59
# File 'lib/active_fedora/associations/builder/association.rb', line 54

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


47
48
49
50
51
52
# File 'lib/active_fedora/associations/builder/association.rb', line 47

def self.define_callbacks(model, reflection)
  if dependent = reflection.options[:dependent]
    check_dependent_options(dependent)
    add_destroy_callbacks(model, reflection)
  end
end

.define_readers(mixin, name) ⇒ Object


61
62
63
64
65
# File 'lib/active_fedora/associations/builder/association.rb', line 61

def self.define_readers(mixin, name)
  mixin.send(:define_method, name) do |*params|
    association(name).reader(*params)
  end
end

.define_writers(mixin, name) ⇒ Object


67
68
69
70
71
# File 'lib/active_fedora/associations/builder/association.rb', line 67

def self.define_writers(mixin, name)
  mixin.send(:define_method, "#{name}=") do |value|
    association(name).writer(value)
  end
end

Instance Method Details

#buildObject


25
26
27
28
# File 'lib/active_fedora/associations/builder/association.rb', line 25

def build
  configure_dependency if options[:dependent] # see https://github.com/rails/rails/commit/9da52a5e55cc665a539afb45783f84d9f3607282
  model.create_reflection(self.class.macro, name, options, model)
end

#configure_dependencyObject


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_fedora/associations/builder/association.rb', line 73

def configure_dependency
  if options[:dependent]
    unless [:destroy, :delete].include?(options[:dependent])
      raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{options[:dependent].inspect})"
    end

    method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
    model.send(:class_eval, "def \#{method_name}\nassociation = \#{name}\nassociation.\#{options[:dependent]} if association\nend\n", __FILE__, __LINE__ + 1)
    model.after_destroy method_name
  end
end

#predicate(property) ⇒ Object

Returns the RDF predicate as defined by the :property attribute


42
43
44
45
# File 'lib/active_fedora/associations/builder/association.rb', line 42

def predicate(property)
  return property if property.kind_of? RDF::URI
  ActiveFedora::Predicates.find_graph_predicate(property)
end

#translate_property_to_predicateObject


30
31
32
33
34
# File 'lib/active_fedora/associations/builder/association.rb', line 30

def translate_property_to_predicate
  return unless options[: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)
  options[:predicate] = predicate(options.delete(:property))
end

#validate_optionsObject


36
37
38
# File 'lib/active_fedora/associations/builder/association.rb', line 36

def validate_options
  options.assert_valid_keys(self.class.valid_options)
end