Sha256: 6fdb2635e38710af8ff6ac551de30e942b913a2e751d34438216ced2fb82cd91

Contents?: true

Size: 1 KB

Versions: 7

Compression:

Stored size: 1 KB

Contents

# TODO: remove this once my PR is accepted and a gtfs gem update is released
# (see https://github.com/nerdEd/gtfs/pull/33)
module GTFS
  module Model
    module ClassMethods
      def each(filename)
        headers = nil
        CSV.foreach(filename, :headers => true) do |row|
          headers ||= unprefixed_headers(row.headers)
          yield parse_model(headers, row.fields)
        end
      end

      def parse_model(headers, fields, options={})
        self.new(Hash[headers.zip(fields)])
      end

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

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

      def unprefixed_headers(headers)
        headers.collect{|h| h.gsub(/^#{prefix}/, '')}
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gtfs-realtime-0.4.0 lib/gtfs/gtfs_gem_patch.rb
gtfs-realtime-0.3.0 lib/gtfs/gtfs_gem_patch.rb
gtfs-realtime-0.2.3 lib/gtfs/gtfs_gem_patch.rb
gtfs-realtime-0.2.2 lib/gtfs/gtfs_gem_patch.rb
gtfs-realtime-0.2.1 lib/gtfs/gtfs_gem_patch.rb
gtfs-realtime-0.2.0 lib/gtfs/gtfs_gem_patch.rb
gtfs-realtime-0.1.0 lib/gtfs/gtfs_gem_patch.rb