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



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

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)


68
69
70
# File 'lib/torque/postgresql/geometry_builder.rb', line 68

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

#deserialize(value) ⇒ Object



56
57
58
# File 'lib/torque/postgresql/geometry_builder.rb', line 56

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

#formationObject



19
20
21
# File 'lib/torque/postgresql/geometry_builder.rb', line 19

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

#piecesObject



15
16
17
# File 'lib/torque/postgresql/geometry_builder.rb', line 15

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

#serialize(value) ⇒ Object



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

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



10
11
12
13
# File 'lib/torque/postgresql/geometry_builder.rb', line 10

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



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

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