Class: Formed::Reflection::AbstractReflection

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

Overview

Instance Method Summary collapse

Instance Method Details

#alias_candidate(name) ⇒ Object



189
190
191
# File 'lib/formed/reflection.rb', line 189

def alias_candidate(name)
  "#{plural_name}_#{name}"
end

#build_association(attributes, &block) ⇒ Object

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



155
156
157
# File 'lib/formed/reflection.rb', line 155

def build_association(attributes, &block)
  klass.new(attributes, &block)
end

#build_scope(table, predicate_builder = predicate_builder(table), klass = self.klass) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/formed/reflection.rb', line 197

def build_scope(table, predicate_builder = predicate_builder(table), klass = self.klass)
  Relation.create(
    klass,
    table: table,
    predicate_builder: predicate_builder
  )
end

#chainObject



193
194
195
# File 'lib/formed/reflection.rb', line 193

def chain
  collect_join_chain
end

#check_validity_of_inverse!Object

Raises:

  • (InverseOfAssociationNotFoundError)


183
184
185
186
187
# File 'lib/formed/reflection.rb', line 183

def check_validity_of_inverse!
  return if polymorphic?
  raise InverseOfAssociationNotFoundError, self if has_inverse? && inverse_of.nil?
  raise InverseOfAssociationRecursiveError, self if has_inverse? && inverse_of == self
end

#class_nameObject

Returns the class name for the macro.

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



163
164
165
# File 'lib/formed/reflection.rb', line 163

def class_name
  @class_name ||= -(options[:class_name] || derive_class_name).to_s
end

#constraintsObject



173
174
175
# File 'lib/formed/reflection.rb', line 173

def constraints
  chain.flat_map(&:scopes)
end

#inverse_ofObject



177
178
179
180
181
# File 'lib/formed/reflection.rb', line 177

def inverse_of
  return unless inverse_name

  @inverse_of ||= klass._reflect_on_association inverse_name
end

#scopesObject

Returns a list of scopes that should be applied for this Reflection object when querying the database.



169
170
171
# File 'lib/formed/reflection.rb', line 169

def scopes
  []
end

#strict_loading?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/formed/reflection.rb', line 205

def strict_loading?
  options[:strict_loading]
end

#strict_loading_violation_message(owner) ⇒ Object



209
210
211
212
213
# File 'lib/formed/reflection.rb', line 209

def strict_loading_violation_message(owner)
  message = +"`#{owner}` is marked for strict_loading."
  message << " The #{polymorphic? ? "polymorphic association" : "#{klass} association"}"
  message << " named `:#{name}` cannot be lazily loaded."
end

#table_nameObject



149
150
151
# File 'lib/formed/reflection.rb', line 149

def table_name
  klass.table_name
end

#through_reflection?Boolean

:nodoc:

Returns:

  • (Boolean)


145
146
147
# File 'lib/formed/reflection.rb', line 145

def through_reflection?
  false
end