Class: INat::Data::Types::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/inat/data/types/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(latitude, longitude) ⇒ Location

Returns a new instance of Location.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
# File 'lib/inat/data/types/location.rb', line 43

def initialize latitude, longitude
  raise ArgumentError, "Latitude must be a Numeric!", caller  unless Numeric === latitude  || latitude == nil
  raise ArgumentError, "Longitude must be a Numeric!", caller unless Numeric === longitude || latitude == nil
  @latitude = latitude
  @longitude = longitude
end

Instance Attribute Details

#latitudeObject (readonly)

Returns the value of attribute latitude.



41
42
43
# File 'lib/inat/data/types/location.rb', line 41

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



41
42
43
# File 'lib/inat/data/types/location.rb', line 41

def longitude
  @longitude
end

Class Method Details

.ddlObject



32
33
34
35
36
37
# File 'lib/inat/data/types/location.rb', line 32

def ddl
  {
    latitude: :REAL,
    longitude: :REAL
  }
end

.from_db(src) ⇒ Object



28
29
30
# File 'lib/inat/data/types/location.rb', line 28

def from_db src
  new(src[:latitude] || src['latitude'], src[:longitude] || src['longitude']).freeze
end

.parse(src) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/inat/data/types/location.rb', line 10

def parse src
  case src
  when nil
    nil
  when String
    parse src.split(',').map(&:to_f)
  when Array
    new(src[0], src[1]).freeze
  when Hash
    latitude = src[:latitude] || src['latitude']
    longitude = src[:longitude] || src['longitude']
    return nil if latitude == nil && longitude == nil
    new(latitude, longitude).freeze
  else
    raise ArgumentError, "Source must be a String or Array or Hash!", caller
  end
end

Instance Method Details

#to_dbObject



50
51
52
53
54
55
# File 'lib/inat/data/types/location.rb', line 50

def to_db
  {
    latitude: latitude,
    longitude: longitude
  }
end