Module: SgMRT

Defined in:
lib/sg_mrt.rb,
lib/sg_mrt/version.rb

Overview

SgMRT is a collection of Singapore’s MRT Lines and Stations for use in Ruby. The list of Singapore MRT Stations is taken from:

en.wikipedia.org/wiki/List_of_Singapore_MRT_stations

Usage:

# Get all MRT Lines
SgMRT::Line.all               # => [<SgMRT::Line @name='North East Line' @code='nsl' @stations=[...]>]

# Get all MRT Stations
SgMRT::Station.all            # => [<SgMRT::Station @name='Paya Lebar' @codes=[...]>]

# Find a station by name
SgMRT::Station.find_by_name('City Hall') # => <SgMRT::Station @name='City Hall', @codes=[...]>

# Find station by code
SgMRT::Station.find_by_code('EW13')      # => <SgMRT::Station @name='City Hall', @codes=[...]>

Defined Under Namespace

Classes: Line, Station

Constant Summary collapse

DATA_PATH =
File.join(File.dirname(__FILE__), '..', 'data')
VERSION =
"0.1.1"
@@data =
{}

Class Method Summary collapse

Class Method Details

.linesObject



67
68
69
70
# File 'lib/sg_mrt.rb', line 67

def lines
  load_data if @@data.empty?
  @@data[:lines]
end

.load_dataObject

Load all files from the data path



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sg_mrt.rb', line 35

def load_data

  @@data[:stations] = []
  @@data[:lines] = []

  find_station = lambda do |name|
    @@data[:stations].find{|x| x.name == name} || Station.new(name)
  end

  Dir[File.join(DATA_PATH, '*.yml')].each do |file_name|
    if mrt = YAML.load_file(file_name)
      mrt_name = mrt['name']
      mrt_code = mrt['code']
      mrt_stations = mrt['stations']
      line = Line.new(mrt_code)
      line.name = mrt_name
      mrt_stations.each do |station|
        station_name, station_code = station
        new_station = find_station.call(station_name)
        new_station.codes << station_code
        new_station.lines << line.code
        line.stations << new_station 
        @@data[:stations] << new_station
      end
      @@data[:lines] << line
    end
  end

  Station.define_helpers(@@data[:lines].collect(&:code))
  
end

.stationsObject



72
73
74
75
# File 'lib/sg_mrt.rb', line 72

def stations
  load_data if @@data.empty?
  @@data[:stations]
end