Class: ReeDao::PgJsonb

Inherits:
ReeMapper::AbstractWrapper show all
Defined in:
lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb

Instance Attribute Summary

Attributes inherited from ReeMapper::AbstractWrapper

#subject

Instance Method Summary collapse

Methods inherited from ReeMapper::AbstractWrapper

#initialize

Constructor Details

This class inherits a constructor from ReeMapper::AbstractWrapper

Instance Method Details

#db_dump(value, role: nil, fields_filters: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb', line 22

def db_dump(value, role: nil, fields_filters: nil)
  if subject.fields_filter
    fields_filters = if fields_filters
      fields_filters + [subject.fields_filter]
    else
      [subject.fields_filter]
    end
  end

  value = begin
    subject.type.db_dump(value, role:, fields_filters:)
  rescue ReeMapper::ErrorWithLocation => e
    e.location ||= subject.location
    raise e
  end

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

#db_load(value, role: nil, fields_filters: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb', line 61

def db_load(value, role: nil, fields_filters: nil)
  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.new("should be a Sequel::Postgres::JSONB, got `#{truncate(value.inspect)}`")
  end

  if subject.fields_filter
    fields_filters = if fields_filters
      fields_filters + [subject.fields_filter]
    else
      [subject.fields_filter]
    end
  end

  begin
    subject.type.db_load(value, role:, fields_filters:)
  rescue ReeMapper::ErrorWithLocation => e
    e.location ||= subject.location
    raise e
  end
end