Sha256: addc2f1633c65779f2d7d7e2fa64e52df6ad79f8292781fee7874630b2950826

Contents?: true

Size: 779 Bytes

Versions: 29

Compression:

Stored size: 779 Bytes

Contents

# typed: false

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

require 'sqreen/util'

class Sqreen::Util::CappedArray < Array
  attr_reader :size_cap, :depth_cap

  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

  def <<(value)
    keep?(size, value) ? super : self
  end
  alias_method :append, :<<

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

  private

  def keep?(index, value)
    index < size_cap && (depth_cap > 0 || !value.is_a?(Hash) && !value.is_a?(Array))
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
sqreen-1.22.0-java lib/sqreen/util/capped_array.rb
sqreen-1.22.0 lib/sqreen/util/capped_array.rb
sqreen-1.21.1-java lib/sqreen/util/capped_array.rb
sqreen-1.21.1 lib/sqreen/util/capped_array.rb
sqreen-1.21.0-java lib/sqreen/util/capped_array.rb
sqreen-1.21.0 lib/sqreen/util/capped_array.rb
sqreen-1.20.4-java lib/sqreen/util/capped_array.rb
sqreen-1.20.4 lib/sqreen/util/capped_array.rb
sqreen-1.21.0.beta3-java lib/sqreen/util/capped_array.rb
sqreen-1.21.0.beta3 lib/sqreen/util/capped_array.rb
sqreen-1.20.4.beta1 lib/sqreen/util/capped_array.rb
sqreen-1.19.4 lib/sqreen/util/capped_array.rb
sqreen-1.20.3 lib/sqreen/util/capped_array.rb
sqreen-1.20.2 lib/sqreen/util/capped_array.rb
sqreen-1.21.0.beta2 lib/sqreen/util/capped_array.rb
sqreen-1.21.0.beta1 lib/sqreen/util/capped_array.rb
sqreen-1.20.1-java lib/sqreen/util/capped_array.rb
sqreen-1.20.1 lib/sqreen/util/capped_array.rb
sqreen-1.20.0-java lib/sqreen/util/capped_array.rb
sqreen-1.20.0 lib/sqreen/util/capped_array.rb