Module: GTFS::Model::ClassMethods

Defined in:
lib/gtfs/model.rb

Instance Method Summary collapse

Instance Method Details

#attrsObject



41
42
43
# File 'lib/gtfs/model.rb', line 41

def attrs
  required_attrs + optional_attrs
end

#collection_name(collection_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/gtfs/model.rb', line 65

def collection_name(collection_name)
  self.define_singleton_method(:name) {collection_name}

  self.define_singleton_method(:singular_name) {
    self.to_s.split('::').last.
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").downcase
  }
end

#column_prefix(prefix) ⇒ Object



57
58
59
# File 'lib/gtfs/model.rb', line 57

def column_prefix(prefix)
  self.class_variable_set('@@prefix', prefix)
end

#each(filename, options) ⇒ Object



80
81
82
83
84
# File 'lib/gtfs/model.rb', line 80

def each(filename, options)
  CSV.foreach(filename, :headers => true, :encoding => options[:encoding]) do |row|
    yield parse_model(row.to_hash)
  end
end

#has_optional_attrs(*attrs) ⇒ Object



53
54
55
# File 'lib/gtfs/model.rb', line 53

def has_optional_attrs(*attrs)
  self.class_variable_set('@@optional_attrs', attrs)
end

#has_required_attrs(*attrs) ⇒ Object

Helper methods for setting up class variables



49
50
51
# File 'lib/gtfs/model.rb', line 49

def has_required_attrs(*attrs)
  self.class_variable_set('@@required_attrs', attrs)
end

#optional_attrsObject



33
34
35
# File 'lib/gtfs/model.rb', line 33

def optional_attrs
  self.class_variable_get('@@optional_attrs')
end

#parse_model(attr_hash, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/gtfs/model.rb', line 86

def parse_model(attr_hash, options={})
  unprefixed_attr_hash = {}

  attr_hash.each do |key, val|
    unprefixed_attr_hash[key.gsub(/^#{prefix}/, '')] = val
  end

  model = self.new(unprefixed_attr_hash)
end

#parse_models(data, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/gtfs/model.rb', line 96

def parse_models(data, options={})
  return [] if data.nil? || data.empty?

  models = []
  CSV.parse(data, :headers => true) do |row|
    model = parse_model(row.to_hash, options)
    models << model if options[:strict] == false || model.valid?
  end
  models
end

#prefixObject

Getters for class variables



29
30
31
# File 'lib/gtfs/model.rb', line 29

def prefix
  self.class_variable_get('@@prefix')
end

#required_attrsObject



37
38
39
# File 'lib/gtfs/model.rb', line 37

def required_attrs
  self.class_variable_get('@@required_attrs')
end

#required_file(required) ⇒ Object



61
62
63
# File 'lib/gtfs/model.rb', line 61

def required_file(required)
  self.define_singleton_method(:required_file?) {required}
end

#uses_filename(filename) ⇒ Object



76
77
78
# File 'lib/gtfs/model.rb', line 76

def uses_filename(filename)
  self.define_singleton_method(:filename) {filename}
end