Sha256: 92b3bfa2c23fca88f68403068b949f9d483dde0b3aaf07a5d72c7767ab2902f1

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module CassandraObject
  module Types
    class ArrayType < BaseType
      class DirtyArray < Array
        attr_accessor :record, :name, :options
        def initialize(record, name, array, options)
          @record   = record
          @name     = name.to_s
          @options  = options

          super(array)
          setify!
        end

        def <<(obj)
          modifying do
            super
          end
        end

        private
          def setify!
            if options[:unique]
              uniq!
              sort!
            end
          end

          def modifying
            unless record.changed_attributes.include?(name)
              original = dup
            end

            result = yield
            setify!

            if !record.changed_attributes.key?(name) && original != self
              record.changed_attributes[name] = original
            end

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

            result
          end
      end

      def ignore_nil?
        false
      end

      def encode(array)
        raise ArgumentError.new("#{self} requires an Array") unless array.kind_of?(Array)
        array.to_a.to_json
      end

      def decode(str)
        array = ActiveSupport::JSON.decode(str)
        array.uniq! if options[:unique]
        array
      end

      def wrap(record, name, value)
        DirtyArray.new(record, name, value, options)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.6.4 lib/cassandra_object/types/array_type.rb