Sha256: 8a2d2bb69d00e301ac11970dfc259dc9b1f0caff8ea74900fdb9a474a172512e

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module CassandraObject
  module Types
    class JsonType < BaseType
      class DirtyHash < Hash
        attr_accessor :record, :name, :options
        def initialize(record, name, hash, options)
          @record   = record
          @name     = name.to_s
          @options  = options

          self.merge!(hash)
          @init_hash = self.hash
          @init_value = hash
        end

        def []=(obj, val)
          modifying do super end
        end

        def delete(obj)
          modifying do super end
        end

        private
        def modifying
          result = yield

          if !record.changed_attributes.key?(name) && @init_hash != self.hash
            record.changed_attributes[name] = @init_value
          end

          record.send("#{name}=", self)

          result
        end
      end

      def encode(hash)
        ActiveSupport::JSON.encode(hash)
      end

      def decode(str)
        ActiveSupport::JSON.decode(str)
      end

      def wrap(record, name, value)
        DirtyHash.new(record, name, Hash[value], options)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cassandra_object_rails-0.0.1 lib/cassandra_object/types/json_type.rb