Class: HrtBus::Rider

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Validations
Defined in:
lib/hrt_bus/rider.rb

Constant Summary collapse

ATTRIBUTES =
[ :id, :direction, :time, :lat, :lon, :route_id ]
DIRECTIONS =
{ "1" => "inbound",
"2" => "outbound" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Rider

Returns a new instance of Rider.



20
21
22
# File 'lib/hrt_bus/rider.rb', line 20

def initialize(attributes={})
  self.attributes = attributes
end

Instance Method Details

#attributesObject



24
25
26
27
28
29
# File 'lib/hrt_bus/rider.rb', line 24

def attributes
  ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key|
    result[key] = read_attribute_for_validation(key)
    result
  end
end

#attributes=(attrs) ⇒ Object



31
32
33
34
35
# File 'lib/hrt_bus/rider.rb', line 31

def attributes=(attrs)
  attrs.each_pair do |k, v|
    send("#{k}=", v)
  end
end

#busObject



47
48
49
50
# File 'lib/hrt_bus/rider.rb', line 47

def bus
  @bus ||= HrtBus::Bus.active_buses.select { |bus| (bus.route_id  == self.route_id) &&
                                                   (bus.direction == self.direction) }.last
end

#distance_to_busObject



41
42
43
44
45
# File 'lib/hrt_bus/rider.rb', line 41

def distance_to_bus
  return 999999999 unless bus.present? && bus.valid?
  distance = Haversine.distance(self.lat.to_f, self.lon.to_f, bus.lat.to_f, bus.lon.to_f)
  distance.to_miles
end

#read_attribute_for_validation(key) ⇒ Object



37
38
39
# File 'lib/hrt_bus/rider.rb', line 37

def read_attribute_for_validation(key)
  send(key)
end