Class: GoogleDistanceMatrix::Place

Inherits:
Object
  • Object
show all
Defined in:
lib/google_distance_matrix/place.rb

Overview

Public: Represents a place and knows how to convert it to param.

Examples

GoogleDistanceMatrix::Place.new address: "My address"
GoogleDistanceMatrix::Place.new lat: 1, lng: 3

You may also build places by other objects responding to lat and lng or address.
If your object responds to all of the attributes we'll use lat and lng as data
for the Place.

GoogleDistanceMatrix::Place.new object

Constant Summary collapse

ATTRIBUTES =
%w[address lat lng].freeze

Instance Method Summary collapse

Constructor Details

#initialize(attributes_or_object) ⇒ Place

Returns a new instance of Place.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/google_distance_matrix/place.rb', line 21

def initialize(attributes_or_object)
  if respond_to_needed_attributes? attributes_or_object
    extract_and_assign_attributes_from_object attributes_or_object
  elsif attributes_or_object.is_a? Hash
    @extracted_attributes_from = attributes_or_object.with_indifferent_access
    assign_attributes attributes_or_object
  else
    raise ArgumentError, 'Must be either hash or object responding to lat, lng or address. '
  end

  validate_attributes
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'lib/google_distance_matrix/place.rb', line 39

def eql?(other)
  return false unless other.is_a? self.class

  if address.present?
    address == other.address
  else
    lat_lng == other.lat_lng
  end
end

#inspectObject



65
66
67
68
69
70
71
# File 'lib/google_distance_matrix/place.rb', line 65

def inspect
  inspection =  (ATTRIBUTES | [:extracted_attributes_from])
                .reject { |a| public_send(a).blank? }
                .map { |a| "#{a}: #{public_send(a).inspect}" }.join ', '

  "#<#{self.class} #{inspection}>"
end

#lat_lng(scale = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/google_distance_matrix/place.rb', line 54

def lat_lng(scale = nil)
  [lat, lng].map do |v|
    if scale
      v = v.to_f.round scale
      v == v.to_i ? v.to_i : v
    else
      v
    end
  end
end

#lat_lng?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/google_distance_matrix/place.rb', line 50

def lat_lng?
  lat.present? && lng.present?
end

#to_param(options = {}) ⇒ Object



34
35
36
37
# File 'lib/google_distance_matrix/place.rb', line 34

def to_param(options = {})
  options = options.with_indifferent_access
  address.present? ? address : lat_lng(options[:lat_lng_scale]).join(',')
end