Class: Polylines::Base
- Inherits:
-
Object
- Object
- Polylines::Base
- Defined in:
- lib/polylines/base.rb
Instance Attribute Summary collapse
-
#current_value ⇒ Object
readonly
Returns the value of attribute current_value.
-
#negative ⇒ Object
readonly
Returns the value of attribute negative.
Class Method Summary collapse
Instance Method Summary collapse
- #decode! ⇒ Object
- #decoding? ⇒ Boolean
- #encode! ⇒ Object
- #encoding? ⇒ Boolean
-
#initialize(current_value) ⇒ Base
constructor
A new instance of Base.
- #step_10 ⇒ Object
- #step_11 ⇒ Object
- #step_2 ⇒ Object
- #step_3 ⇒ Object
- #step_4 ⇒ Object
- #step_5 ⇒ Object
- #step_6 ⇒ Object
- #step_7 ⇒ Object
- #step_8 ⇒ Object
Constructor Details
#initialize(current_value) ⇒ Base
Returns a new instance of Base.
5 6 7 |
# File 'lib/polylines/base.rb', line 5 def initialize(current_value) @current_value = current_value end |
Instance Attribute Details
#current_value ⇒ Object (readonly)
Returns the value of attribute current_value.
3 4 5 |
# File 'lib/polylines/base.rb', line 3 def current_value @current_value end |
#negative ⇒ Object (readonly)
Returns the value of attribute negative.
3 4 5 |
# File 'lib/polylines/base.rb', line 3 def negative @negative end |
Class Method Details
.transform_to_array_of_lat_lng_and_deltas(value) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/polylines/base.rb', line 90 def self.transform_to_array_of_lat_lng_and_deltas(value) if self == Polylines::Encoder delta_latitude, delta_longitude = 0, 0 return value.inject([]) do |polyline, (latitude, longitude)| polyline << latitude - delta_latitude polyline << longitude - delta_longitude delta_latitude, delta_longitude = latitude, longitude polyline end end if self == Polylines::Decoder set = [] return value.split(//).inject([]) do |charset, char| set << char if ((char.unpack("U").first - 63) & 0x20).zero? charset << set.join set = [] end charset end.map {|charset| decode(charset) } end end |
Instance Method Details
#decode! ⇒ Object
76 77 78 79 80 |
# File 'lib/polylines/base.rb', line 76 def decode! if decoding? @current_value = yield end end |
#decoding? ⇒ Boolean
86 87 88 |
# File 'lib/polylines/base.rb', line 86 def decoding? self.is_a?(Polylines::Decoder) end |
#encode! ⇒ Object
70 71 72 73 74 |
# File 'lib/polylines/base.rb', line 70 def encode! if encoding? @current_value = yield end end |
#encoding? ⇒ Boolean
82 83 84 |
# File 'lib/polylines/base.rb', line 82 def encoding? self.is_a?(Polylines::Encoder) end |
#step_10 ⇒ Object
60 61 62 63 |
# File 'lib/polylines/base.rb', line 60 def step_10 encode! { current_value.map {|value| value + 63 } } decode! { current_value.map {|value| value - 63 } } end |
#step_11 ⇒ Object
65 66 67 68 |
# File 'lib/polylines/base.rb', line 65 def step_11 encode! { current_value.map(&:chr).join } decode! { current_value.split(//).map {|char| char.unpack("U").first } } end |
#step_2 ⇒ Object
9 10 11 12 13 14 |
# File 'lib/polylines/base.rb', line 9 def step_2 @negative = current_value < 0 if encoding? encode! { (current_value * 1e5).round } decode! { current_value.to_f/1e5 } end |
#step_3 ⇒ Object
16 17 18 19 20 |
# File 'lib/polylines/base.rb', line 16 def step_3 return unless negative encode! { ~(current_value * -1) + 1 } decode! { ~(current_value - 1) * -1 } end |
#step_4 ⇒ Object
22 23 24 25 |
# File 'lib/polylines/base.rb', line 22 def step_4 encode! { current_value << 1 } decode! { current_value >> 1 } end |
#step_5 ⇒ Object
27 28 29 30 31 |
# File 'lib/polylines/base.rb', line 27 def step_5 return unless negative encode! { ~current_value } decode! { ~current_value } end |
#step_6 ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/polylines/base.rb', line 33 def step_6 encode! do [].tap do |numbers| while current_value > 0 do numbers.unshift(current_value & 0x1f) @current_value >>= 5 end end end decode! do current_value.map {|chunk| "%05b" % chunk }.join.tap do |val| @negative = val[-1, 1] == "1" end.to_i(2) end end |
#step_7 ⇒ Object
50 51 52 53 |
# File 'lib/polylines/base.rb', line 50 def step_7 encode! { current_value.reverse } decode! { current_value.reverse } end |
#step_8 ⇒ Object
55 56 57 58 |
# File 'lib/polylines/base.rb', line 55 def step_8 encode! { current_value[0..-2].map {|item| item | 0x20 } << current_value.last } decode! { current_value[0..-2].map {|item| item ^ 0x20 } << current_value.last } end |