Class: RailwayJp::Station

Inherits:
Object
  • Object
show all
Includes:
Equality
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

Methods included from Equality

#==, #eql?, #hash

Constructor Details

#initialize(row) ⇒ Station

Returns a new instance of Station.



43
44
45
46
47
# File 'lib/railway_jp/station.rb', line 43

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

Class Method Details

.allObject



19
20
21
# File 'lib/railway_jp/station.rb', line 19

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

.find(id) ⇒ Object



23
24
25
26
# File 'lib/railway_jp/station.rb', line 23

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

Instance Method Details

#lineObject



49
50
51
# File 'lib/railway_jp/station.rb', line 49

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

#line_nameObject



53
54
55
# File 'lib/railway_jp/station.rb', line 53

def line_name
  line&.name
end