Class: DuckRecord::Reflection::MacroReflection

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractReflection

#alias_candidate, #build_association, #check_validity!, #class_name

Constructor Details

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

Returns a new instance of MacroReflection.



168
169
170
171
172
173
174
175
# File 'lib/duck_record/reflection.rb', line 168

def initialize(name, scope, options, duck_record)
  @name          = name
  @scope         = scope
  @options       = options
  @duck_record   = duck_record
  @klass         = options[:anonymous_class]
  @plural_name   = name.to_s.pluralize
end

Instance Attribute Details

#duck_recordObject (readonly)

Returns the value of attribute duck_record.



164
165
166
# File 'lib/duck_record/reflection.rb', line 164

def duck_record
  @duck_record
end

#nameObject (readonly)

Returns the name of the macro.

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



154
155
156
# File 'lib/duck_record/reflection.rb', line 154

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 {}



162
163
164
# File 'lib/duck_record/reflection.rb', line 162

def options
  @options
end

#plural_nameObject (readonly)

:nodoc:



166
167
168
# File 'lib/duck_record/reflection.rb', line 166

def plural_name
  @plural_name
end

#scopeObject (readonly)

Returns the value of attribute scope.



156
157
158
# File 'lib/duck_record/reflection.rb', line 156

def scope
  @scope
end

Instance Method Details

#==(other_aggregation) ⇒ Object

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



191
192
193
194
195
196
197
# File 'lib/duck_record/reflection.rb', line 191

def ==(other_aggregation)
  super ||
    other_aggregation.kind_of?(self.class) &&
      name == other_aggregation.name &&
      !other_aggregation.options.nil? &&
      active_record == other_aggregation.active_record
end

#compute_class(name) ⇒ Object



185
186
187
# File 'lib/duck_record/reflection.rb', line 185

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



181
182
183
# File 'lib/duck_record/reflection.rb', line 181

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