Class: GoogleDistanceMatrix::Place

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

Constant Summary collapse

ATTRIBUTES =
%w[address lat lng]

Instance Method Summary collapse

Constructor Details

#initialize(attributes_or_object) ⇒ Place

Returns a new instance of Place.



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

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
    assign_attributes attributes_or_object
  else
    fail ArgumentError, "Must be either hash or object responding to lat, lng or address. "
  end

  validate_attributes
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/google_distance_matrix/place.rb', line 37

def eql?(other)
  if address.present?
    address == other.address
  else
    lat_lng == other.lat_lng
  end
end

#inspectObject



56
57
58
59
60
# File 'lib/google_distance_matrix/place.rb', line 56

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/google_distance_matrix/place.rb', line 45

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

#to_param(options = {}) ⇒ Object



31
32
33
34
# File 'lib/google_distance_matrix/place.rb', line 31

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