Module: GtfsEngine::Concerns::Controllers::DataAccess

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
lib/gtfs_engine/concerns/controllers/data_access.rb

Overview

A Controller Concern for convenient access to the DataSet specified in the URL.

Instance Method Summary collapse

Instance Method Details

#data(param_key = :data_set_id) ⇒ DataSet

Parameters:

  • param_key (Symbol) (defaults to: :data_set_id)

    The key of the URL param to use as the feed’s ID

Returns:



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gtfs_engine/concerns/controllers/data_access.rb', line 8

def data(param_key=:data_set_id)
  key = params[param_key]
  (@data_sets ||= {})[key] =
      Rails.cache.fetch "data_set_#{key}" do
        if param_is_data_set_name? param_key
          GtfsEngine::DataSet.where(name: key).newest
        else
          GtfsEngine::DataSet.find params[param_key]
        end
      end
end

#data_cache(key) ⇒ Object



20
21
22
23
24
# File 'lib/gtfs_engine/concerns/controllers/data_access.rb', line 20

def data_cache(key)
  Rails.cache.fetch "#{data.id}::#{key}" do
    yield
  end
end

#param_is_data_set_name?(param_key) ⇒ Bool

instead of its ID

Returns:

  • (Bool)

    true if the key is the name of the GTFS feed,



28
29
30
# File 'lib/gtfs_engine/concerns/controllers/data_access.rb', line 28

def param_is_data_set_name?(param_key)
  not /[[:digit:]]/ === params[param_key].try(:first)
end