Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

stop using constants to store anything in GTFS::Model, switch to clas…

…s vars
  • Loading branch information...
commit 4f19f033352e50c4d627521267c930b5c0ed42d4 1 parent 07dbfc4
@nerdEd authored
View
4 lib/gtfs/agency.rb
@@ -2,8 +2,8 @@ module GTFS
class Agency
include GTFS::Model
- required_attrs :name, :url, :timezone
- optional_attrs :id, :lang, :phone, :fare_url
+ has_required_attrs :name, :url, :timezone
+ has_optional_attrs :id, :lang, :phone, :fare_url
attr_accessor *attrs
column_prefix :agency_
View
2  lib/gtfs/calendar.rb
@@ -2,7 +2,7 @@ module GTFS
class Calendar
include GTFS::Model
- required_attrs :service_id, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday, :start_date, :end_date
+ has_required_attrs :service_id, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday, :start_date, :end_date
attr_accessor *attrs
def self.parse_calendars(data)
View
2  lib/gtfs/calendar_date.rb
@@ -2,7 +2,7 @@ module GTFS
class CalendarDate
include GTFS::Model
- required_attrs :service_id, :date, :exception_type
+ has_required_attrs :service_id, :date, :exception_type
attr_accessor *attrs
def self.parse_calendar_dates(data)
View
39 lib/gtfs/model.rb
@@ -6,11 +6,11 @@ def self.included(base)
base.extend ClassMethods
base.class_variable_set('@@prefix', '')
- const_set('OPTIONAL_ATTRS', [])
- const_set('REQUIRED_ATTRS', [])
+ base.class_variable_set('@@optional_attrs', [])
+ base.class_variable_set('@@required_attrs', [])
def valid?
- !self.class::REQUIRED_ATTRS.any?{|f| self.send(f.to_sym).nil?}
+ !self.class.required_attrs.any?{|f| self.send(f.to_sym).nil?}
end
def initialize(attrs)
@@ -21,24 +21,41 @@ def initialize(attrs)
end
module ClassMethods
+
+ #####################################
+ # Getters for class variables
+ #####################################
+
def prefix
self.class_variable_get('@@prefix')
end
- def required_attrs(*attrs)
- const_set('REQUIRED_ATTRS', attrs)
+ def optional_attrs
+ self.class_variable_get('@@optional_attrs')
end
- def optional_attrs(*attrs)
- const_set('OPTIONAL_ATTRS', attrs)
+ def required_attrs
+ self.class_variable_get('@@required_attrs')
end
- def column_prefix(prefix)
- self.class_variable_set('@@prefix', prefix)
+ def attrs
+ required_attrs + optional_attrs
end
- def attrs
- self::REQUIRED_ATTRS + self::OPTIONAL_ATTRS
+ #####################################
+ # Helper methods for setting up class variables
+ #####################################
+
+ def has_required_attrs(*attrs)
+ self.class_variable_set('@@required_attrs', attrs)
+ end
+
+ def has_optional_attrs(*attrs)
+ self.class_variable_set('@@optional_attrs', attrs)
+ end
+
+ def column_prefix(prefix)
+ self.class_variable_set('@@prefix', prefix)
end
def parse_models(data)
View
4 lib/gtfs/route.rb
@@ -2,8 +2,8 @@ module GTFS
class Route
include GTFS::Model
- required_attrs :id, :short_name, :long_name, :type
- optional_attrs :agency_id, :desc, :url, :color, :text_color
+ has_required_attrs :id, :short_name, :long_name, :type
+ has_optional_attrs :agency_id, :desc, :url, :color, :text_color
attr_accessor *attrs
column_prefix :route_
View
4 lib/gtfs/shape.rb
@@ -2,8 +2,8 @@ module GTFS
class Shape
include GTFS::Model
- required_attrs :id, :pt_lat, :pt_lon, :pt_sequence
- optional_attrs :dist_traveled
+ has_required_attrs :id, :pt_lat, :pt_lon, :pt_sequence
+ has_optional_attrs :dist_traveled
attr_accessor *attrs
column_prefix :shape_
View
5 lib/gtfs/stop.rb
@@ -1,8 +1,9 @@
module GTFS
class Stop
include GTFS::Model
- required_attrs :id, :name, :lat, :lon
- optional_attrs :code, :desc, :zone_id, :url, :location_type, :parent_station, :timezone
+
+ has_required_attrs :id, :name, :lat, :lon
+ has_optional_attrs :code, :desc, :zone_id, :url, :location_type, :parent_station, :timezone
column_prefix :stop_
attr_accessor *attrs
View
4 lib/gtfs/stop_time.rb
@@ -2,8 +2,8 @@ module GTFS
class StopTime
include GTFS::Model
- required_attrs :trip_id, :arrival_time, :departure_time, :stop_id, :stop_sequence
- optional_attrs :stop_headsign, :pickup_type, :drop_off_type, :shape_dist_traveled
+ has_required_attrs :trip_id, :arrival_time, :departure_time, :stop_id, :stop_sequence
+ has_optional_attrs :stop_headsign, :pickup_type, :drop_off_type, :shape_dist_traveled
attr_accessor *attrs
def self.parse_stop_times(data)
View
4 lib/gtfs/trip.rb
@@ -2,8 +2,8 @@ module GTFS
class Trip
include GTFS::Model
- required_attrs :route_id, :service_id, :id
- optional_attrs :headsign, :short_name, :direction_id, :block_id, :shape_id
+ has_required_attrs :route_id, :service_id, :id
+ has_optional_attrs :headsign, :short_name, :direction_id, :block_id, :shape_id
attr_accessor *attrs
column_prefix :trip_
Please sign in to comment.
Something went wrong with that request. Please try again.