module SerializeAttributes

Serialize ActiveModel attributes in JSON using type casting

Constants

VERSION

Public Instance Methods

serialize_attributes(column_name, &block) click to toggle source

Configure a SerializeAttributes::Store, using the given column to store each attribute.

class Person
  serialize_attributes :settings do
    attribute :user_name, :string, default: "Christian"
  end
end

Person.new(user_name: "Nick")
# File lib/serialize_attributes.rb, line 21
def serialize_attributes(column_name, &block)
  column_name = column_name.to_sym

  @serialized_attribute_stores ||= {}
  @serialized_attribute_stores[column_name] = Store.new(self, column_name, &block)
end
serialized_attribute_names(column_name, type = nil) click to toggle source

Get a list of the attributes registered in a given store

Person.serialized_attribute_names(:settings, :string)
=> [:user_name]
# File lib/serialize_attributes.rb, line 39
def serialized_attribute_names(column_name, type = nil)
  serialized_attributes_store(column_name).attribute_names(type: type)
end
serialized_attributes_on(column_name) click to toggle source

Retrieve all of the SerializeAttributes attributes, including their default values

person = Person.new
person.serialized_attributes_on(:settings)
#=> { "user_name" => "Christian" }
# File lib/serialize_attributes.rb, line 49
def serialized_attributes_on(column_name)
  store = self.class.serialized_attributes_store(column_name)

  store.attribute_names.index_with do |attribute_name|
    public_send(attribute_name)
  end
end
serialized_attributes_store(column_name) click to toggle source

Retrieve a SerializeAttributes registered against the given column

Person.serialized_attributes_store(:settings)
# File lib/serialize_attributes.rb, line 31
def serialized_attributes_store(column_name)
  @serialized_attribute_stores.fetch(column_name.to_sym)
end