Skip to content

Commit

Permalink
Adding scoping to the model lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
robbielamb committed Oct 14, 2010
1 parent 2e34247 commit 3605f84
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require 'yogo/data_app'
APP_VERSION = IO.readlines(File.join(File.dirname(__FILE__), 'VERSION'))[0]

# Make the model lookup map
use Yogo::Rack::ModelLookup, :paths => ['schema', 'data']
use Yogo::Rack::ModelLookup, :paths => ['schema', 'data'], :scope => lambda{ |*args| return Schema }

# Run the app
run Rack::Cascade.new([Yogo::SchemaApp, Yogo::DataApp])
7 changes: 4 additions & 3 deletions lib/yogo/rack/model_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ class ModelLookup
def initialize(app, options = {})
paths = options[:paths] || ['data']
@app = app
@scope = options[:scope] || lambda { |*args| return Schema }
@base_regexp = /^\/(#{paths.join('|')})\/(([a-zA-Z0-9]|-|_)+)/
end

def call(env)
if env['PATH_INFO'] =~ @base_regexp
model_id = $2
env['yogo.schema'], env['yogo.resource'] = get_model(model_id)
env['yogo.schema'], env['yogo.resource'] = get_model(model_id, env)
return [ 404, {'Content-Type' => 'text/plain'}, ["#{model_id} not found"] ] if env['yogo.schema'].nil?
end

Expand All @@ -23,8 +24,8 @@ def call(env)

private

def get_model(model_id)
config = Schema.first(:name => model_id)
def get_model(model_id, env)
config = @scope.call(env).first(:name => model_id)

unless config.nil?
return config, config.data_model
Expand Down

0 comments on commit 3605f84

Please sign in to comment.