Class: ActiveRecord::Import::ValueSetsBytesParser
- Inherits:
-
Object
- Object
- ActiveRecord::Import::ValueSetsBytesParser
- Defined in:
- lib/activerecord-import/value_sets_parser.rb
Instance Attribute Summary collapse
-
#max_bytes ⇒ Object
readonly
Returns the value of attribute max_bytes.
-
#reserved_bytes ⇒ Object
readonly
Returns the value of attribute reserved_bytes.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(values, options) ⇒ ValueSetsBytesParser
constructor
A new instance of ValueSetsBytesParser.
- #parse ⇒ Object
Constructor Details
#initialize(values, options) ⇒ ValueSetsBytesParser
Returns a new instance of ValueSetsBytesParser.
22 23 24 25 26 |
# File 'lib/activerecord-import/value_sets_parser.rb', line 22 def initialize(values, ) @values = values @reserved_bytes = [:reserved_bytes] || 0 @max_bytes = .fetch(:max_bytes) { default_max_bytes } end |
Instance Attribute Details
#max_bytes ⇒ Object (readonly)
Returns the value of attribute max_bytes.
16 17 18 |
# File 'lib/activerecord-import/value_sets_parser.rb', line 16 def max_bytes @max_bytes end |
#reserved_bytes ⇒ Object (readonly)
Returns the value of attribute reserved_bytes.
16 17 18 |
# File 'lib/activerecord-import/value_sets_parser.rb', line 16 def reserved_bytes @reserved_bytes end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
16 17 18 |
# File 'lib/activerecord-import/value_sets_parser.rb', line 16 def values @values end |
Class Method Details
.parse(values, options) ⇒ Object
18 19 20 |
# File 'lib/activerecord-import/value_sets_parser.rb', line 18 def self.parse(values, ) new(values, ).parse end |
Instance Method Details
#parse ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/activerecord-import/value_sets_parser.rb', line 28 def parse value_sets = [] arr = [] current_size = 0 values.each_with_index do |val, i| comma_bytes = arr.size insert_size = reserved_bytes + val.bytesize if insert_size > max_bytes raise ValueSetTooLargeError.new("#{insert_size} bytes exceeds the max allowed for an insert [#{@max_bytes}]", insert_size) end 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 |