Class: Formed::Reflection::MacroReflection

Inherits:
AbstractReflection show all
Defined in:
lib/formed/reflection.rb

Overview

Base class for AggregateReflection and AssociationReflection. Objects of AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.

Direct Known Subclasses

AggregateReflection, AssociationReflection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractReflection

#alias_candidate, #build_association, #build_scope, #chain, #check_validity_of_inverse!, #class_name, #constraints, #inverse_of, #scopes, #strict_loading?, #strict_loading_violation_message, #table_name, #through_reflection?

Constructor Details

#initialize(name, scope, options, active_form) ⇒ MacroReflection

Returns a new instance of MacroReflection.



256
257
258
259
260
261
262
# File 'lib/formed/reflection.rb', line 256

def initialize(name, scope, options, active_form)
  @name          = name
  @scope         = scope
  @options       = options
  @active_form = active_form
  @klass         = options[:anonymous_class]
end

Instance Attribute Details

#active_formObject (readonly)

Returns the value of attribute active_form.



248
249
250
# File 'lib/formed/reflection.rb', line 248

def active_form
  @active_form
end

#nameObject (readonly)

Returns the name of the macro.

composed_of :balance, class_name: 'Money' returns :balance has_many :clients returns :clients



246
247
248
# File 'lib/formed/reflection.rb', line 246

def name
  @name
end

#optionsObject (readonly)

Returns the hash of options used for the macro.

composed_of :balance, class_name: 'Money' returns { class_name: "Money" } has_many :clients returns {}



254
255
256
# File 'lib/formed/reflection.rb', line 254

def options
  @options
end

#plural_nameObject (readonly)

Returns the value of attribute plural_name.



248
249
250
# File 'lib/formed/reflection.rb', line 248

def plural_name
  @plural_name
end

#scopeObject (readonly)

Returns the value of attribute scope.



248
249
250
# File 'lib/formed/reflection.rb', line 248

def scope
  @scope
end

Instance Method Details

#==(other) ⇒ Object

Returns true if self and other_aggregation have the same name attribute, active_form attribute, and other_aggregation has an options hash assigned to it.



295
296
297
298
299
300
301
# File 'lib/formed/reflection.rb', line 295

def ==(other)
  super ||
    other.is_a?(self.class) &&
      name == other.name &&
      !other.options.nil? &&
      active_form == other.active_form
end

#autosave=(autosave) ⇒ Object



264
265
266
267
268
# File 'lib/formed/reflection.rb', line 264

def autosave=(autosave)
  @options[:autosave] = autosave
  parent_reflection = self.parent_reflection
  parent_reflection.autosave = autosave if parent_reflection
end

#compute_class(name) ⇒ Object



289
290
291
# File 'lib/formed/reflection.rb', line 289

def compute_class(name)
  name.constantize
end

#klassObject

Returns the class for the macro.

composed_of :balance, class_name: 'Money' returns the Money class has_many :clients returns the Client class

class Company < ActiveRecord::Base
  has_many :clients
end

Company.reflect_on_association(:clients).klass
# => Client

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.



285
286
287
# File 'lib/formed/reflection.rb', line 285

def klass
  @klass ||= compute_class(class_name)
end

#scope_for(relation, owner = nil) ⇒ Object



303
304
305
# File 'lib/formed/reflection.rb', line 303

def scope_for(relation, owner = nil)
  relation.instance_exec(owner, &scope) || relation
end