Sha256: c125d164cb3b4a029cc0ee430b8e6333dcb33d23dc9b3076e7783b2f672c886b

Contents?: true

Size: 1.38 KB

Versions: 45

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "sequel/extensions/pg_array"

class ReeDao::PgArray < ReeMapper::AbstractWrapper
  contract(
    Any,
    Kwargs[
      name: String,
      role: Nilor[Symbol, ArrayOf[Symbol]],
      fields_filters: ArrayOf[ReeMapper::FieldsFilter]
    ] => Or[Sequel::Postgres::PGArray, String]
  )
  def db_dump(value, name:, role: nil, fields_filters: [])
    if !value.is_a?(Array)
      raise ReeMapper::TypeError, "`#{name}` should be an array"
    end

    value = value.map.with_index do |el, index|
      subject.type.db_dump(
        el,
        name: "#{name}[#{index}]",
        role: role,
        fields_filters: fields_filters + [subject.fields_filter]
      )
    end

    if value.empty?
      "{}"
    else
      Sequel.pg_array(value)
    end
  end

  contract(
    Any,
    Kwargs[
      name: String,
      role: Nilor[Symbol, ArrayOf[Symbol]],
      fields_filters: ArrayOf[ReeMapper::FieldsFilter]
    ] => Array
  ).throws(ReeMapper::TypeError)
  def db_load(value, name:, role: nil, fields_filters: [])
    if !value.is_a?(Sequel::Postgres::PGArray)
      raise ReeMapper::TypeError, "`#{name}` is not Sequel::Postgres::PGArray"
    end

    value.map.with_index do |val, index|
      subject.type.db_load(
        val,
        name: "#{name}[#{index}]",
        role: role,
        fields_filters: fields_filters + [subject.fields_filter]
      )
    end
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
ree_lib-1.0.85 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.84 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.83 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.82 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.81 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.80 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.79 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.72 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.71 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.70 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb