Class: ActiveFedora::Reflection::ClassMethods::MacroReflection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/reflection.rb

Direct Known Subclasses

AssociationReflection

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (MacroReflection) initialize(macro, name, options, active_fedora)

Returns a new instance of MacroReflection



92
93
94
95
# File 'lib/active_fedora/reflection.rb', line 92

def initialize(macro, name, options, active_fedora)
  @macro, @name, @options, @active_fedora = macro, name, options, active_fedora
  @automatic_inverse_of = nil
end

Instance Attribute Details

- (Object) active_fedora (readonly)

Returns the value of attribute active_fedora



74
75
76
# File 'lib/active_fedora/reflection.rb', line 74

def active_fedora
  @active_fedora
end

- (Object) macro (readonly)

Returns the macro type.

has_many :clients returns :has_many



67
68
69
# File 'lib/active_fedora/reflection.rb', line 67

def macro
  @macro
end

- (Object) name (readonly)

Returns the name of the macro.

has_many :clients returns :clients



62
63
64
# File 'lib/active_fedora/reflection.rb', line 62

def name
  @name
end

- (Object) options (readonly)

Returns the hash of options used for the macro.

has_many :clients returns {}



72
73
74
# File 'lib/active_fedora/reflection.rb', line 72

def options
  @options
end

Instance Method Details

- (Boolean) belongs_to?

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


119
120
121
# File 'lib/active_fedora/reflection.rb', line 119

def belongs_to?
  macro == :belongs_to
end

- (Object) build_association(*options)

Returns a new, unsaved instance of the associated class. options will be passed to the class's constructor.



99
100
101
# File 'lib/active_fedora/reflection.rb', line 99

def build_association(*options)
  klass.new(*options)
end

- (Object) class_name

Returns the class name for the macro.

has_many :clients returns 'Client'



107
108
109
# File 'lib/active_fedora/reflection.rb', line 107

def class_name
  @class_name ||= options[:class_name] || derive_class_name
end

- (Boolean) collection?

Returns whether or not this association reflection is for a collection association. Returns true if the macro is either has_many or has_and_belongs_to_many, false otherwise.

Returns:

  • (Boolean)


114
115
116
# File 'lib/active_fedora/reflection.rb', line 114

def collection?
  @collection
end

- (Object) klass

Returns the target association's class.

class Author < ActiveRecord::Base
  has_many :books
end

Author.reflect_on_association(:books).klass
# => Book

Note: Do not call klass.new or klass.create to instantiate a new association object. Use build_association or create_association instead. This allows plugins to hook into association object creation.



88
89
90
# File 'lib/active_fedora/reflection.rb', line 88

def klass
  @klass ||= class_name.constantize
end