Class: ActiveRecordPolyline::Polyline

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_polyline/polyline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points = [], **options) ⇒ Polyline

Returns a new instance of Polyline.



9
10
11
12
13
14
15
16
17
# File 'lib/active_record_polyline/polyline.rb', line 9

def initialize(points = [], **options)
  points = FastPolylines.decode(points, 5) if points.is_a?(String)
  points = points.map(&method(:cast_array_to_hash)) if points.first.is_a?(Array)
  @points = points

  compressor_name = options.delete(:name)
  @compressor = Compaction.lookup(compressor_name).new(**options)
  @compressor.apply self
end

Instance Attribute Details

#pointsObject (readonly)

Returns the value of attribute points.



7
8
9
# File 'lib/active_record_polyline/polyline.rb', line 7

def points
  @points
end

Instance Method Details

#push(longitude:, latitude:, skip_compaction: false) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/active_record_polyline/polyline.rb', line 23

def push(longitude:, latitude:, skip_compaction: false)
  @compressor.push(
    location: {
      longitude: longitude,
      latitude: latitude,
    },
    skip_compaction: skip_compaction
  )
end

#to_sObject



19
20
21
# File 'lib/active_record_polyline/polyline.rb', line 19

def to_s
  FastPolylines.encode(@points.map { |point| [point[:latitude], point[:longitude]] }, 5)
end