Class: Superstore::Associations::Reflection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(macro, name, model, options) ⇒ Reflection

Returns a new instance of Reflection.



5
6
7
8
9
10
# File 'lib/superstore/associations/reflection.rb', line 5

def initialize(macro, name, model, options)
  @macro  = macro
  @name   = name
  @model  = model
  @options = options
end

Instance Attribute Details

#macroObject (readonly)

Returns the value of attribute macro.



4
5
6
# File 'lib/superstore/associations/reflection.rb', line 4

def macro
  @macro
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/superstore/associations/reflection.rb', line 4

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/superstore/associations/reflection.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/superstore/associations/reflection.rb', line 4

def options
  @options
end

Instance Method Details

#association_classObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/superstore/associations/reflection.rb', line 12

def association_class
  case macro
  when :belongs_to
    Superstore::Associations::BelongsTo
  when :has_many
    Superstore::Associations::HasMany
  when :has_one
    Superstore::Associations::HasOne
  end

end

#belongs_to?Boolean

Returns:

  • (Boolean)


48
# File 'lib/superstore/associations/reflection.rb', line 48

def belongs_to?; false; end

#class_nameObject



50
51
52
# File 'lib/superstore/associations/reflection.rb', line 50

def class_name
  @class_name ||= (options[:class_name] || name.to_s.classify)
end

#default_primary_key?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/superstore/associations/reflection.rb', line 36

def default_primary_key?
  primary_key == "id"
end

#foreign_keyObject



28
29
30
# File 'lib/superstore/associations/reflection.rb', line 28

def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key
end

#instance_variable_nameObject



24
25
26
# File 'lib/superstore/associations/reflection.rb', line 24

def instance_variable_name
  "@#{name}"
end

#inverse_nameObject



54
55
56
# File 'lib/superstore/associations/reflection.rb', line 54

def inverse_name
  options[:inverse_of]
end

#polymorphic?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/superstore/associations/reflection.rb', line 44

def polymorphic?
  options[:polymorphic]
end

#polymorphic_columnObject



40
41
42
# File 'lib/superstore/associations/reflection.rb', line 40

def polymorphic_column
  "#{name}_type"
end

#primary_keyObject



32
33
34
# File 'lib/superstore/associations/reflection.rb', line 32

def primary_key
  options[:primary_key] || "id"
end