class Perpetuity::Postgres::SerializedData

Attributes

column_names[R]
values[R]

Public Class Methods

new(column_names, *values) click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 8
def initialize column_names, *values
  @column_names = column_names.map(&:to_s)
  @values = values
end

Public Instance Methods

+(other) click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 34
def + other
  combined = self.class.new(column_names.dup, *(values.dup))
  combined.values << other.values

  combined
end
-(other) click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 51
def - other
  values = self.values.first
  modified_values = values - other.values.first
  modified_columns = column_names.select.with_index { |col, index|
    values[index] != other.values.first[index]
  }

  SerializedData.new(modified_columns, modified_values)
end
==(other) click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 61
def == other
  other.is_a? SerializedData and
  other.column_names == column_names and
  other.values == values
end
[](key) click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 18
def [] key
  index = column_names.index(key.to_s)
  values.first[index]
end
[]=(column, value) click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 23
def []= column, value
  value = TextValue.new(value)
  if column_names.include? column
    index = column_names.index(column)
    values.first[index] = value
  else
    column_names << column
    values.first << value
  end
end
any?() click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 41
def any?
  values.flatten.any?
end
each() { |column, data| ... } click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 45
def each
  data = values.first
  column_names.each_with_index { |column, index| yield(column, data[index]) }
  self
end
to_s() click to toggle source
# File lib/perpetuity/postgres/serialized_data.rb, line 13
def to_s
  value_strings = values.map { |data| "(#{data.join(',')})" }.join(',')
  "(#{column_names.join(',')}) VALUES #{value_strings}"
end