Class: Cuboid::Support::Filter::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/cuboid/support/filter/base.rb

Overview

This class is abstract.

Author:

Direct Known Subclasses

Set

Constant Summary collapse

DEFAULT_OPTIONS =
{
    hasher: :hash
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • options (Set) (defaults to: {})

Options Hash (options):

  • (:hasher) (Symbol)

    Method to call on the item to obtain its hash.



17
18
19
20
# File 'lib/cuboid/support/filter/base.rb', line 17

def initialize( options = {} )
    @options = DEFAULT_OPTIONS.merge( options )
    @hasher  = @options[:hasher].to_sym
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



8
9
10
# File 'lib/cuboid/support/filter/base.rb', line 8

def collection
  @collection
end

Class Method Details

._load(data) ⇒ Object



93
94
95
# File 'lib/cuboid/support/filter/base.rb', line 93

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

Instance Method Details

#<<(item) ⇒ Base

Returns ‘self`.

Parameters:

  • item (#persistent_hash)

    Item to insert.

Returns:

  • (Base)

    ‘self`



27
28
29
30
# File 'lib/cuboid/support/filter/base.rb', line 27

def <<( item )
    @collection << calculate_hash( item )
    self
end

#==(other) ⇒ Object



77
78
79
# File 'lib/cuboid/support/filter/base.rb', line 77

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

#_dump(_) ⇒ Object



89
90
91
# File 'lib/cuboid/support/filter/base.rb', line 89

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

#any?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/cuboid/support/filter/base.rb', line 44

def any?
    !empty?
end

#clearObject



52
53
54
# File 'lib/cuboid/support/filter/base.rb', line 52

def clear
    @collection.clear
end

#dupObject



85
86
87
# File 'lib/cuboid/support/filter/base.rb', line 85

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

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/cuboid/support/filter/base.rb', line 40

def empty?
    @collection.empty?
end

#hashObject



81
82
83
# File 'lib/cuboid/support/filter/base.rb', line 81

def hash
    @collection.hash
end

#include?(item) ⇒ Bool

Parameters:

  • item (#persistent_hash)

    Item to check.

Returns:

  • (Bool)


36
37
38
# File 'lib/cuboid/support/filter/base.rb', line 36

def include?( item )
    @collection.include? calculate_hash( item )
end

#merge(other) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cuboid/support/filter/base.rb', line 56

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

#sizeObject



48
49
50
# File 'lib/cuboid/support/filter/base.rb', line 48

def size
    @collection.size
end