Class: Sqreen::Util::CappedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/sqreen/util/capped_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ CappedHash

Returns a new instance of CappedHash.



11
12
13
14
15
16
17
18
19
# File 'lib/sqreen/util/capped_hash.rb', line 11

def initialize(*args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  size_cap = opts[:size_cap] || 150
  depth_cap = opts[:depth_cap] || 10
  @size_cap = size_cap
  @depth_cap = depth_cap

  super(*args, &block)
end

Instance Attribute Details

#depth_capObject (readonly)

Returns the value of attribute depth_cap.



9
10
11
# File 'lib/sqreen/util/capped_hash.rb', line 9

def depth_cap
  @depth_cap
end

#size_capObject (readonly)

Returns the value of attribute size_cap.



9
10
11
# File 'lib/sqreen/util/capped_hash.rb', line 9

def size_cap
  @size_cap
end

Instance Method Details

#[]=(key, value) ⇒ Object Also known as: store



21
22
23
# File 'lib/sqreen/util/capped_hash.rb', line 21

def []=(key, value)
  super if key?(key) || keep?(value)
end

#merge!(h) ⇒ Object Also known as: update



26
27
28
# File 'lib/sqreen/util/capped_hash.rb', line 26

def merge!(h)
  h.each { |k, v| self[k] = block_given? ? yield(k, self[k], v) : v }
end

#replace(h) ⇒ Object



31
32
33
34
# File 'lib/sqreen/util/capped_hash.rb', line 31

def replace(h)
  keep_if { false }
  merge!(h)
end