Sha256: 7d0c876ad937870bff2c48d443a5caedc1a46dccb8e8787373f996200798ade3

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 KB

Contents

module ActiveRecord::Import
  class ValueSetsBytesParser
    attr_reader :reserved_bytes, :max_bytes, :values

    def self.parse(values, options)
      new(values, options).parse
    end

    def initialize(values, options)
      @values = values
      @reserved_bytes = options[:reserved_bytes] || 0
      @max_bytes = options.fetch(:max_bytes) { default_max_bytes }
    end

    def parse
      value_sets = []
      arr = []
      current_size = 0
      values.each_with_index do |val, i|
        comma_bytes = arr.size
        bytes_thus_far = reserved_bytes + current_size + val.bytesize + comma_bytes
        if bytes_thus_far <= max_bytes
          current_size += val.bytesize
          arr << val
        else
          value_sets << arr
          arr = [val]
          current_size = val.bytesize
        end

        # if we're on the last iteration push whatever we have in arr to value_sets
        value_sets << arr if i == (values.size - 1)
      end

      value_sets
    end

    private

    def default_max_bytes
      values_in_bytes = values.sum(&:bytesize)
      comma_separated_bytes = values.size - 1
      reserved_bytes + values_in_bytes + comma_separated_bytes
    end
  end

  class ValueSetsRecordsParser
    attr_reader :max_records, :values

    def self.parse(values, options)
      new(values, options).parse
    end

    def initialize(values, options)
      @values = values
      @max_records = options[:max_records]
    end

    def parse
      @values.in_groups_of(max_records, false)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
activerecord-import-0.19.0 lib/activerecord-import/value_sets_parser.rb
activerecord-import-0.18.3 lib/activerecord-import/value_sets_parser.rb
activerecord-import-0.18.2 lib/activerecord-import/value_sets_parser.rb
activerecord-import-0.18.1 lib/activerecord-import/value_sets_parser.rb
activerecord-import-0.18.0 lib/activerecord-import/value_sets_parser.rb
activerecord-import-0.17.2 lib/activerecord-import/value_sets_parser.rb
activerecord-import-0.17.1 lib/activerecord-import/value_sets_parser.rb
activerecord-import-0.17.0 lib/activerecord-import/value_sets_parser.rb