Module: Bank::Serialize

Defined in:
lib/bank/serialize.rb

Constant Summary collapse

PackTimeStamps =
->(attrs, config) do
  now, model = Time.now, config.model.new

  attrs[:updated_at] = now if model.respond_to?(:updated_at=)
  attrs[:created_at] = now if model.respond_to?(:created_at=) &&
    !model.send(config.primary_key)
end
PackIntegers =
->(attrs, config) do
  config.columns_of_type(:integer).each { |column, opts|
    attrs[column] = attrs[column].to_i if attrs[column]
  }
end
PackBooleans =
->(attrs, config) do
  config.columns_of_type(:boolean).each { |column, opts|
    false_values = [false, nil, 0, 'false', 'nil', '0']
    value = !false_values.include?(attrs[column])
    attrs[column] = value ? 1 : 0
  }
end
UnpackTime =
->(attrs, config) do
  config.columns_of_type(:datetime).each { |column, opts|
    time = attrs[column]
    attrs[column] = case time
                    when NilClass
                      nil
                    when String
                      Time.at(Time.parse(time).to_i).utc
                    when DateTime
                      Time.at(time.to_time.to_i).utc
                    else
                      Time.at(time.to_i).utc
                    end
  }
end
UnpackDate =
->(attrs, config) do
  config.columns_of_type(:date).each { |col, _|
      attrs[col] = Date.parse(attrs[col]) if attrs[col].is_a?(String)
  }
end
UnpackBoolean =
->(attrs, config) do
  config.columns.select { |col, opts|
    !attrs[col].nil? && opts[:type] == :boolean
  }.each { |col, opts| attrs[col] = [1, true].include?(attrs[col]) }
end

Class Method Summary collapse

Class Method Details

.pack(config, model) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/bank/serialize.rb', line 3

def self.pack(config, model)
  attrs = model.to_hash

  config.packers.each { |p| p.call(attrs, config) }
  model.set(Serialize.unpack(config, attrs.dup))
  attrs
end

.unpack(config, attrs) ⇒ Object



11
12
13
14
# File 'lib/bank/serialize.rb', line 11

def self.unpack(config, attrs)
  config.unpackers.each {|p| p.call(attrs, config) }
  attrs
end