Class: ActiveFedora::Associations::Builder::Association
- Inherits:
-
Object
- Object
- ActiveFedora::Associations::Builder::Association
show all
- Defined in:
- lib/active_fedora/associations/builder/association.rb
Overview
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model, name, options) ⇒ Association
19
20
21
22
23
24
25
|
# File 'lib/active_fedora/associations/builder/association.rb', line 19
def initialize(model, name, options)
@model = model
@name = name
@options = options
translate_property_to_predicate
validate_options
end
|
Instance Attribute Details
#mixin ⇒ Object
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
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
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
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
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
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.options[:dependent]
check_dependent_options(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 options[:dependent]
model.create_reflection(self.class.macro, name, options, model)
end
|
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 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}\n association = \#{name}\n association.\#{options[:dependent]} if association\n end\n eoruby\n model.after_destroy method_name\nend\n", __FILE__, __LINE__ + 1)
|
#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 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_options ⇒ Object
38
39
40
|
# File 'lib/active_fedora/associations/builder/association.rb', line 38
def validate_options
options.assert_valid_keys(self.class.valid_options)
end
|