Sha256: cf1976710698609df63c1ddbaa98dbb508c49acd56af43e90cb0295ac03de9a7

Contents?: true

Size: 1.07 KB

Versions: 13

Compression:

Stored size: 1.07 KB

Contents

module CassandraObject
  module Types
    class HashType < 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 { super }
        end

        def delete(obj)
          modifying { super }
        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

13 entries across 13 versions & 1 rubygems

Version Path
gotime-cassandra_object-4.12.1 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.12.0 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.11.6 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.11.5 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.11.4 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.11.3 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.11.2 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.11.1 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.11.0 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.10.5 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.10.4 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.10.3 lib/cassandra_object/types/hash_type.rb
gotime-cassandra_object-4.10.2 lib/cassandra_object/types/hash_type.rb