Class: Torque::PostgreSQL::GeometryBuilder

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/torque/postgresql/geometry_builder.rb

Constant Summary collapse

DESTRUCTOR =
/[<>{}()]/.freeze
NUMBER_SERIALIZER =
->(num) { num.to_s.gsub(/\.0$/, '') }

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/torque/postgresql/geometry_builder.rb', line 21

def cast(value)
  case value
  when ::String
    return if value.blank?
    value.gsub!(DESTRUCTOR, '')
    build_klass(*value.split(','))
  when ::Hash
    build_klass(*value.symbolize_keys.slice(*pieces).values)
  when ::Array
    build_klass(*(value.flatten))
  else
    value
  end
end

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/torque/postgresql/geometry_builder.rb', line 66

def changed_in_place?(raw_old_value, new_value)
  raw_old_value != serialize(new_value)
end

#deserialize(value) ⇒ Object



54
55
56
# File 'lib/torque/postgresql/geometry_builder.rb', line 54

def deserialize(value)
  build_klass(*value.gsub(DESTRUCTOR, '').split(',')) unless value.nil?
end

#formationObject



17
18
19
# File 'lib/torque/postgresql/geometry_builder.rb', line 17

def formation
  self.class.const_get('FORMATION')
end

#piecesObject



13
14
15
# File 'lib/torque/postgresql/geometry_builder.rb', line 13

def pieces
  self.class.const_get('PIECES')
end

#serialize(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/torque/postgresql/geometry_builder.rb', line 36

def serialize(value)
  parts =
    case value
    when config_class
      pieces.map { |piece| value.public_send(piece) }
    when ::Hash
      value.symbolize_keys.slice(*pieces).values
    when ::Array
      value.flatten
    end

  parts = parts&.compact&.flatten
  return if parts.blank?

  raise 'Invalid format' if parts.size < pieces.size
  format(formation, *parts.first(pieces.size).map(&number_serializer))
end

#typeObject



8
9
10
11
# File 'lib/torque/postgresql/geometry_builder.rb', line 8

def type
  return self.class.const_get('TYPE') if self.class.const_defined?('TYPE')
  self.class.const_set('TYPE', self.class.name.demodulize.underscore.to_sym)
end

#type_cast_for_schema(value) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/torque/postgresql/geometry_builder.rb', line 58

def type_cast_for_schema(value)
  if config_class === value
    pieces.map { |piece| value.public_send(piece) }
  else
    super
  end
end