Module: SerializableAttributes

Defined in:
lib/serializable_attributes.rb,
lib/serializable_attributes/types.rb,
lib/serializable_attributes/schema.rb,
lib/serializable_attributes/format/active_support_json.rb

Overview

create a BLOB column and setup your field types for conversion and convenient attr methods

class Profile < ActiveRecord::Base
  # not needed if used as a rails plugin
  SerializableAttributes.setup(self)

  # assumes #data serializes to raw_data blob field
  serialize_attributes do
    string  :title, :description
    integer :age
    float   :rank, :percentage
    time    :birthday
  end

  # Serializes #data to assumed raw_data blob field
  serialize_attributes :data do
    string  :title, :description
    integer :age
    float   :rank, :percentage
    time    :birthday
  end

  # set the blob field
  serialize_attributes :data, :blob => :serialized_field do
    string  :title, :description
    integer :age
    float   :rank, :percentage
    time    :birthday
  end
end

Defined Under Namespace

Modules: Format, ModelMethods Classes: Array, AttributeType, Boolean, Float, Hash, Integer, Schema, String, Time

Constant Summary collapse

VERSION =
"1.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.typesObject

Returns the value of attribute types.



132
133
134
# File 'lib/serializable_attributes/types.rb', line 132

def types
  @types
end

Class Method Details

.add_type(type, object = nil) ⇒ Object



133
134
135
136
137
138
# File 'lib/serializable_attributes/types.rb', line 133

def add_type(type, object = nil)
  types[type] = object
  Schema.send(:define_method, type) do |*names|
    field type, *names
  end
end

.setup(active_record = ActiveRecord::Base) ⇒ Object

Install the plugin for the given model class.

active_record - A class to install the plugin. Default: ActiveRecord::Base.

Returns nothing.



69
70
71
# File 'lib/serializable_attributes.rb', line 69

def self.setup(active_record = ActiveRecord::Base)
  active_record.extend ModelMethods
end