Class: SmartEnum::Associations::Association

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

Direct Known Subclasses

HasAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_class, association_name, class_name: nil, as: nil, foreign_key: nil) ⇒ Association

Returns a new instance of Association.



101
102
103
104
105
106
107
# File 'lib/smart_enum/associations.rb', line 101

def initialize(owner_class, association_name, class_name: nil, as: nil, foreign_key: nil)
  @owner_class = owner_class
  @association_name = association_name.to_sym
  @class_name_option = class_name
  @as_option = as
  @foreign_key_option = foreign_key
end

Instance Attribute Details

#as_optionObject (readonly)

Returns the value of attribute as_option.



99
100
101
# File 'lib/smart_enum/associations.rb', line 99

def as_option
  @as_option
end

#association_nameObject (readonly)

Returns the value of attribute association_name.



99
100
101
# File 'lib/smart_enum/associations.rb', line 99

def association_name
  @association_name
end

#class_name_optionObject (readonly)

Returns the value of attribute class_name_option.



99
100
101
# File 'lib/smart_enum/associations.rb', line 99

def class_name_option
  @class_name_option
end

#foreign_key_optionObject (readonly)

Returns the value of attribute foreign_key_option.



99
100
101
# File 'lib/smart_enum/associations.rb', line 99

def foreign_key_option
  @foreign_key_option
end

#owner_classObject (readonly)

Returns the value of attribute owner_class.



99
100
101
# File 'lib/smart_enum/associations.rb', line 99

def owner_class
  @owner_class
end

Instance Method Details

#association_classObject



121
122
123
124
125
# File 'lib/smart_enum/associations.rb', line 121

def association_class
  @association_class ||= SmartEnum::Utilities.constantize(class_name).tap{|klass|
    ::SmartEnum::Associations.__assert_enum(klass)
  }
end

#class_nameObject



109
110
111
# File 'lib/smart_enum/associations.rb', line 109

def class_name
  @class_name ||= (class_name_option || SmartEnum::Utilities.classify(association_name)).to_s
end

#foreign_keyObject



113
114
115
# File 'lib/smart_enum/associations.rb', line 113

def foreign_key
  @foreign_key ||= (foreign_key_option || SmartEnum::Utilities.foreign_key(association_name)).to_sym
end

#generated_method_nameObject



117
118
119
# File 'lib/smart_enum/associations.rb', line 117

def generated_method_name
  @generated_method_name ||= (as_option || association_name).to_sym
end