class StoreSchema::AccessorDefiner

Attributes

attribute[R]

@return [Symbol]

column[R]

@return [Symbol]

klass[R]

@return [Class]

type[R]

@return [Symbol]

Public Class Methods

new(klass, column, type, attribute) click to toggle source

@param klass [Class] the class to define the accessor on @param column [Symbol] the name of the column to define the accessor on @param type [Symbol] the data type of the {#attribute} @param attribute [Symbol] the name of the {#column}'s attribute

# File lib/store_schema/accessor_definer.rb, line 26
def initialize(klass, column, type, attribute)
  @klass     = klass
  @column    = column
  @type      = type
  @attribute = attribute
end

Public Instance Methods

define() click to toggle source

Defines all necessary accessors on {#klass}.

# File lib/store_schema/accessor_definer.rb, line 35
def define
  define_store_accessor
  define_attribute
  define_getter
  define_setter
end

Private Instance Methods

define_attribute() click to toggle source

Defines the attribute on the class using the {.attribute}.

# File lib/store_schema/accessor_definer.rb, line 52
def define_attribute
  klass.attribute(attribute)
end
define_getter() click to toggle source

Enhances the store getter by adding data conversion capabilities.

Calls superclass method
# File lib/store_schema/accessor_definer.rb, line 58
def define_getter
  _type = type

  klass.send(:define_method, attribute) do
    value = super()

    if value.is_a?(NilClass)
      value
    else
      StoreSchema::Converter.new(value, _type).from_db
    end
  end
end
define_setter() click to toggle source

Enhances the store setter by adding data conversion capabilities.

Calls superclass method
# File lib/store_schema/accessor_definer.rb, line 74
def define_setter
  _type = type

  klass.send(:define_method, "#{attribute}=") do |value|
    converted_value = StoreSchema::Converter.new(value, _type).to_db

    if converted_value
      super(converted_value)
    else
      super(nil)
    end
  end
end
define_store_accessor() click to toggle source

Defines the standard store accessor.

# File lib/store_schema/accessor_definer.rb, line 46
def define_store_accessor
  klass.store_accessor(column, attribute)
end