Class: Uuids::Base::BelongsTo

Inherits:
Struct
  • Object
show all
Defined in:
lib/uuids/base/belongs_to.rb

Overview

Adds getter and setter for the model attribute referred by uuid.

Examples:

BelongsTo.add City, :state, State
city = City.new
city.respond_to? :state   # => true
city.respond_to? :state=  # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#class_nameString (readonly)

The name of the #klass constant.

Returns:

  • (String)

    The #klass name.



28
29
30
# File 'lib/uuids/base/belongs_to.rb', line 28

def class_name
  @class_name ||= klass.name
end

#klassActiveRecord::Base

The model to add the attribute to.

Returns:

  • (ActiveRecord::Base)

    The model class.



12
13
14
# File 'lib/uuids/base/belongs_to.rb', line 12

def klass
  @klass
end

#model_nameString (readonly)

The name of the #klass db table column to store reference to the

attribute by uuid.

Returns:

  • (String)

    The name of the column.



37
38
39
# File 'lib/uuids/base/belongs_to.rb', line 37

def column_name
  @column_name ||= "#{ name }_uuid"
end

#nameString, Symbol

The name of the attribute to be added.

Returns:

  • (String, Symbol)

    The name.



12
13
14
# File 'lib/uuids/base/belongs_to.rb', line 12

def name
  @name
end

Class Method Details

.add(klass, name) ⇒ Object

Constructs the object and adds the attribute.

Examples:

BelongsTo.add City, :state, State
city = City.new
city.respond_to? :state   # => true
city.respond_to? :state=  # => true

Parameters:

  • klass (ActiveRecord::Base)

    The model to add attribute to. Should have a corresponding column for reference by uuid.

  • name (String, Symbol)

    The name of the attribute to be added.

Raises:

  • (TypeError)

    if the #model doesn’t include the Uuids::Base module.

  • (NotImplementedError)

    if the #klass doesn’t have the correspondint #column_name column.



48
49
50
# File 'lib/uuids/base/belongs_to.rb', line 48

def self.add(klass, name)
  new(klass, name).add
end

Instance Method Details

#addObject

Adds the attribute’s getter and setter.

Raises:

  • (TypeError)

    if the #model doesn’t include the Uuids::Base module.

  • (NotImplementedError)

    if the #klass doesn’t have the correspondint #column_name column.



58
59
60
61
62
# File 'lib/uuids/base/belongs_to.rb', line 58

def add
  check_column
  add_getter
  add_setter
end

#column_nameString

The name of the #klass db table column to store reference to the

attribute by uuid.

Returns:

  • (String)

    The name of the column.



37
38
39
# File 'lib/uuids/base/belongs_to.rb', line 37

def column_name
  @column_name ||= "#{ name }_uuid"
end