Sha256: 4d2732bae39c173deeb6aa6def0a2c75c255b0f13e852c996c0ebe2fc1f7cbd1

Contents?: true

Size: 1.59 KB

Versions: 18

Compression:

Stored size: 1.59 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
            setify!
          end
        end

        def delete(obj)
          modifying do
            super
          end
        end

        private
          def setify!
            if options[:unique]
              reject!(&:blank?)
              uniq!
              begin sort! rescue ArgumentError end
            end
          end

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

            result = yield

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

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

            result
          end
      end

      def default
        []
      end

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

      def decode(str)
        return [] if str.blank?

        ActiveSupport::JSON.decode(str).tap do |array|
          array.uniq! if options[:unique]
        end
      end

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
gotime-cassandra_object-4.5.1 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.5.0 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.4.5 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.4.4 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.4.3 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.4.0 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.3.2 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.3.1 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.3.0 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.2.2 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.2.0 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.1.0 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.0.2 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.0.1 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-4.0.0 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-3.0.5 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-3.0.4 lib/cassandra_object/types/array_type.rb
gotime-cassandra_object-3.0.3 lib/cassandra_object/types/array_type.rb