Sha256: 3926bd0928aad51a24566c8db02a2938532967b65d639ce269b175aa755e605f

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

require 'active_record/base'
require 'json'

module ActiveRecord
  class SerializeJSON
    def initialize(attribute, opts = {})
      @attribute = attribute.to_s.to_sym
      @serialize = opts[:serialize] || {}
      @deserialize = opts[:deserialize] || {}
    end

    attr_reader :attribute

    def before_save(record)
      json = serialize record
      record.write_attribute(@attribute, json)
    end

    def after_save(record)
      data = deserialize record
      record.write_attribute(@attribute, data)
    end

    def serialize(record)
      self.class.serialize(record.read_attribute(@attribute), @serialize)
    end

    def deserialize(record)
      self.class.deserialize(record.read_attribute(@attribute), @deserialize)
    end

    def self.serialize(value, opts = {})
      opts ||= {}
      JSON.generate(value, opts)
    end

    def self.deserialize(value, opts = {})
      opts ||= {}
      JSON.parse(value, opts)
    rescue => e
      Rails.logger.warn e
      value
    end
  end

  class Base
    def self.serialize_json(attribute, opts = {})
      sj = SerializeJSON.new(attribute, opts)

      before_save sj
      after_save  sj

      unless respond_to?(:serialize_json_attributes)
        cattr_accessor :serialize_json_attributes
        self.serialize_json_attributes = {}
      end
      serialize_json_attributes[sj.attribute] = sj

      class_eval do
        define_method(:after_find) do
          super if defined? super
          self.class.serialize_json_attributes.each do |attribute, sj|
            write_attribute(attribute, sj.deserialize(self))
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_serialize_json-0.0.2 lib/active_record/serialize_json.rb