Class: ReeDao::PgArray

Inherits:
ReeMapper::AbstractWrapper show all
Defined in:
lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb', line 13

def db_dump(value, role: nil, fields_filters: nil)
  if !value.is_a?(Array)
    raise ReeMapper::TypeError.new("should be an array, 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

  value = value.map.with_index do |item, idx|
    subject.type.db_dump(item, role:, fields_filters:)
  rescue ReeMapper::ErrorWithLocation => e
    e.location ||= subject.location
    e.prepend_field_name(idx.to_s)
    raise e
  end

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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb', line 48

def db_load(value, role: nil, fields_filters: nil)
  if !value.is_a?(Sequel::Postgres::PGArray)
    raise ReeMapper::TypeError.new("should be a Sequel::Postgres::PGArray, 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

  value.map.with_index do |item, idx|
    subject.type.db_load(item, role:, fields_filters:)
  rescue ReeMapper::ErrorWithLocation => e
    e.location ||= subject.location
    e.prepend_field_name(idx.to_s)
    raise e
  end
end