Sha256: c093d0aed9117f20a43d754435f7b677b61e96e9b4e9e9a010a0274a233630ea

Contents?: true

Size: 1.93 KB

Versions: 32

Compression:

Stored size: 1.93 KB

Contents

module Cuboid
module Support::Filter

# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
# @abstract
class Base

    attr_reader :collection

    DEFAULT_OPTIONS = {
        hasher: :hash
    }

    # @param    [Set]  options
    # @option   options [Symbol]    (:hasher)
    #   Method to call on the item to obtain its hash.
    def initialize( options = {} )
        @options = DEFAULT_OPTIONS.merge( options )
        @hasher  = @options[:hasher].to_sym
    end

    # @param    [#persistent_hash] item
    #   Item to insert.
    #
    # @return   [Base]
    #   `self`
    def <<( item )
        @collection << calculate_hash( item )
        self
    end

    # @param    [#persistent_hash] item
    #   Item to check.
    #
    # @return   [Bool]
    def include?( item )
        @collection.include? calculate_hash( item )
    end

    def empty?
        @collection.empty?
    end

    def any?
        !empty?
    end

    def size
        @collection.size
    end

    def clear
        @collection.clear
    end

    def merge( other )
        case other
            when self.class

                @collection.merge other.collection

            when Array

                other.each do |k|
                    fail 'Cannot merge with unhashed entries' if !k.is_a?( Numeric )
                    @collection << k
                end

            else
                fail ArgumentError,
                     "Don't know how to merge with: #{other.class}"
        end

        self
    end

    def ==( other )
        hash == other.hash
    end

    def hash
        @collection.hash
    end

    def dup
        self.class.new( @options.dup ).merge self
    end

    def _dump( _ )
        Marshal.dump( to_rpc_data )
    end

    def self._load( data )
        from_rpc_data Marshal.load( data )
    end

    def collection=( c )
        @collection = c
    end

    private

    def calculate_hash( item )
        item.send @hasher
    end

end

end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
cuboid-0.2.11 lib/cuboid/support/filter/base.rb
cuboid-0.2.10 lib/cuboid/support/filter/base.rb
cuboid-0.2.9 lib/cuboid/support/filter/base.rb
cuboid-0.2.8 lib/cuboid/support/filter/base.rb
cuboid-0.2.7 lib/cuboid/support/filter/base.rb
cuboid-0.2.6 lib/cuboid/support/filter/base.rb
cuboid-0.2.5 lib/cuboid/support/filter/base.rb
cuboid-0.2.4.2 lib/cuboid/support/filter/base.rb
cuboid-0.2.4.1 lib/cuboid/support/filter/base.rb
cuboid-0.2.4 lib/cuboid/support/filter/base.rb
cuboid-0.2.3 lib/cuboid/support/filter/base.rb
cuboid-0.2.2 lib/cuboid/support/filter/base.rb
cuboid-0.2.1 lib/cuboid/support/filter/base.rb
cuboid-0.2 lib/cuboid/support/filter/base.rb
cuboid-0.1.9.1 lib/cuboid/support/filter/base.rb
cuboid-0.1.9 lib/cuboid/support/filter/base.rb
cuboid-0.1.8 lib/cuboid/support/filter/base.rb
cuboid-0.1.7 lib/cuboid/support/filter/base.rb
cuboid-0.1.6.1 lib/cuboid/support/filter/base.rb
cuboid-0.1.6 lib/cuboid/support/filter/base.rb