Class: RailwayJp::Station

Inherits:
Object
  • Object
show all
Defined in:
lib/railway_jp/station.rb

Constant Summary collapse

MAPPINGS =
{
  id: 'station_cd',
  name: 'station_name',
  line_id: 'line_cd',
  prefecture_id: 'pref_cd',
  postcode: 'post',
  address: 'address',
  longitude: 'lon',
  latitude: 'lat',
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row) ⇒ Station

Returns a new instance of Station.



41
42
43
44
45
# File 'lib/railway_jp/station.rb', line 41

def initialize(row)
  MAPPINGS.each do |attr, column_name|
    instance_variable_set("@#{attr}", row[column_name])
  end
end

Class Method Details

.allObject



17
18
19
# File 'lib/railway_jp/station.rb', line 17

def all
  @all ||= data.map { |row| new(row) }
end

.find(id) ⇒ Object



21
22
23
24
# File 'lib/railway_jp/station.rb', line 21

def find(id)
  row = data_index_by_id[id.to_s]
  row ? new(row) : nil
end

Instance Method Details

#lineObject



47
48
49
# File 'lib/railway_jp/station.rb', line 47

def line
  @line ||= Line.find(line_id)
end

#line_nameObject



51
52
53
# File 'lib/railway_jp/station.rb', line 51

def line_name
  line&.name
end