Class: ActiveFedora::Reflection::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

#initialize(macro, name, options, active_fedora) ⇒ MacroReflection

Returns a new instance of MacroReflection.



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

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

#active_fedoraObject (readonly)

Returns the value of attribute active_fedora.



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

def active_fedora
  @active_fedora
end

#macroObject (readonly)

Returns the macro type.

has_many :clients returns :has_many



81
82
83
# File 'lib/active_fedora/reflection.rb', line 81

def macro
  @macro
end

#nameObject (readonly)

Returns the name of the macro.

has_many :clients returns :clients



76
77
78
# File 'lib/active_fedora/reflection.rb', line 76

def name
  @name
end

#optionsObject (readonly)

Returns the hash of options used for the macro.

has_many :clients returns {}



86
87
88
# File 'lib/active_fedora/reflection.rb', line 86

def options
  @options
end

Instance Method Details

#belongs_to?Boolean

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


132
133
134
# File 'lib/active_fedora/reflection.rb', line 132

def belongs_to?
  macro == :belongs_to
end

#build_association(*options, &block) ⇒ Object

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



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

def build_association(*options, &block)
  klass.new(*options, &block)
end

#class_nameObject

Returns the class name for the macro.

has_many :clients returns 'Client'



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

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

#collection?Boolean

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)


127
128
129
# File 'lib/active_fedora/reflection.rb', line 127

def collection?
  @collection
end

#has_and_belongs_to_many?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/active_fedora/reflection.rb', line 140

def has_and_belongs_to_many?
  macro == :has_and_belongs_to_many
end

#has_many?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/active_fedora/reflection.rb', line 136

def has_many?
  macro == :has_many
end

#klassObject

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.



102
103
104
# File 'lib/active_fedora/reflection.rb', line 102

def klass
  @klass ||= class_name.constantize
end