Sha256: abdc8fa3135f0a00722d476f8d5ce5bc13b52654d6055b5fbaffdc99fee03689

Contents?: true

Size: 1.82 KB

Versions: 31

Compression:

Stored size: 1.82 KB

Contents

require "sequel/extensions/pg_json"

package_require("ree_object/functions/to_hash")

class ReeDao::PgJsonb < ReeMapper::AbstractWrapper
  contract(
    Any,
    Kwargs[
      name: String,
      role: Nilor[Symbol, ArrayOf[Symbol]],
      fields_filters: ArrayOf[ReeMapper::FieldsFilter]
    ] => Or[
      Sequel::Postgres::JSONBHash,
      Sequel::Postgres::JSONBArray,
      Sequel::Postgres::JSONBInteger,
      Sequel::Postgres::JSONBTrue,
      Sequel::Postgres::JSONBFalse,
      Sequel::Postgres::JSONBString,
      Sequel::Postgres::JSONBFloat,
      Sequel::Postgres::JSONBNull
    ]
  ).throws(ReeMapper::TypeError)
  def db_dump(value, name:, role: nil, fields_filters: [])
    value = subject.type.db_dump(
      value,
      name: name,
      role: role,
      fields_filters: fields_filters + [subject.fields_filter]
    )

    begin
      Sequel.pg_jsonb_wrap(value)
    rescue Sequel::Error
      raise ReeMapper::TypeError, "`#{name}` should be an jsonb primitive"
    end
  end

  contract(
    Any,
    Kwargs[
      name: String,
      role: Nilor[Symbol, ArrayOf[Symbol]],
      fields_filters: ArrayOf[ReeMapper::FieldsFilter]
    ] => Or[
      Hash,
      Array,
      Integer,
      Float,
      String,
      Bool,
      NilClass
    ]
  ).throws(ReeMapper::TypeError)
  def db_load(value, name:, role: nil, fields_filters: [])
    value = case value
    when Sequel::Postgres::JSONBHash
      ReeObject::ToHash.new.call(value.to_h)
    when Sequel::Postgres::JSONBArray
      ReeObject::ToHash.new.call(value.to_a)
    when Numeric, String, TrueClass, FalseClass, NilClass
      value
    else
      raise ReeMapper::TypeError, "`#{name}` is not Sequel::Postgres::JSONB"
    end

    subject.type.db_load(
      value,
      name: name,
      role: role,
      fields_filters: fields_filters + [subject.fields_filter]
    )
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ree_lib-1.0.70 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.65 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.64 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.63 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.62 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.61 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.60 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.59 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.58 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.57 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.56 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.55 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.54 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.53 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.52 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
ree_lib-1.0.51 lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb