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.



33
34
35
# File 'lib/uuids/base/belongs_to.rb', line 33

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

#modelActiveRecord::Base

The model to be referred by uuid.

Returns:

  • (ActiveRecord::Base)

    The model class to be referred to.



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

def model
  @model
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.



41
42
43
# File 'lib/uuids/base/belongs_to.rb', line 41

def model_name
  @model_name ||= model.name
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, model) ⇒ 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.

  • model (ActiveRecord::Base)

    The model to be referred by uuid. Should include the Uuids::Base module.

Raises:

  • (TypeError)

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

  • (NotImplementedError)

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



63
64
65
# File 'lib/uuids/base/belongs_to.rb', line 63

def self.add(klass, name, model)
  new(klass, name, model).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.



73
74
75
76
77
78
# File 'lib/uuids/base/belongs_to.rb', line 73

def add
  check_model
  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.



50
51
52
# File 'lib/uuids/base/belongs_to.rb', line 50

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