Class: StaticModel::Associations::Association

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

Direct Known Subclasses

BelongsToAssociation, HasManyAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options = {}) ⇒ Association

Returns a new instance of Association.



28
29
30
31
32
33
34
35
# File 'lib/static_model/associations.rb', line 28

def initialize(klass, name, options = {})
  @klass = klass
  @name = name
  @options = options
  @reflection_klass_name = @options[:class_name] || @name.to_s.classify
  @foreign_key = @options[:foreign_key]
  define_association_methods
end

Instance Attribute Details

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



26
27
28
# File 'lib/static_model/associations.rb', line 26

def foreign_key
  @foreign_key
end

#klassObject (readonly)

Returns the value of attribute klass.



26
27
28
# File 'lib/static_model/associations.rb', line 26

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/static_model/associations.rb', line 26

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/static_model/associations.rb', line 26

def options
  @options
end

Instance Method Details

#define_association_methodsObject



46
47
48
# File 'lib/static_model/associations.rb', line 46

def define_association_methods
  raise 'Should only use descendants of Association'
end

#reflection_klassObject



37
38
39
40
41
42
43
44
# File 'lib/static_model/associations.rb', line 37

def reflection_klass
  Object.const_get(@reflection_klass_name)
rescue 
  eval <<-EOT
    class #{@reflection_klass_name}; end;
    #{@reflection_klass_name}
  EOT
end