sinatra-scopes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /pkg
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ Sinatra Scopes
2
+ ==============
3
+
4
+ Simple scoping for your request handlers. From `example_app.rb`:
5
+
6
+ scope :project, '/projects/*' do |project_id|
7
+ @project = Project[project_id]
8
+ end
9
+
10
+ project.get '/users/*' do |user_id|
11
+ @user = User[user_id]
12
+ "#{@project}, #{@user}"
13
+ end
14
+
15
+ Scopes don't have to specify a path pattern:
16
+
17
+ scope(:authorized) { auth }
18
+
19
+ authorized.get '/sekret/*' do |page|
20
+ page
21
+ end
22
+
23
+ Similar Implementations
24
+ -----------------------
25
+
26
+ [dcparker](http://github.com/dcparker) - [cilantro/lib/cilantro/controller.rb](http://github.com/dcparker/cilantro/blob/d6adede43e380688349ffecd7189e8a2d2b82df3/lib/cilantro/controller.rb)
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |s|
4
+ s.name = "sinatra-scopes"
5
+ s.homepage = "http://github.com/quackingduck/sinatra-scopes"
6
+ s.summary = "Simple request handler scoping for sinatra"
7
+ s.email = "myles@myles.id.au"
8
+ s.authors = ["Myles Byrne"]
9
+
10
+ s.add_dependency 'sinatra', '>= 0.9.4'
11
+ s.add_development_dependency 'exemplor', '>= 2000.0.0'
12
+ s.add_development_dependency 'rack-test', '0.4.0'
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Install jeweler to build gem"
17
+ end
18
+
19
+ task :default => [:test]
20
+
21
+ task :examples do
22
+ ruby "examples.rb"
23
+ end
24
+
25
+ task :test => :examples
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/example_app.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'sinatra'
2
+ require 'sinatra/scopes'
3
+
4
+ Project = {'a' => 'Project A', 'b' => 'Project B'}
5
+ User = {'1' => 'User 1', '2' => 'User 2'}
6
+
7
+ scope :project, '/projects/*' do |project_id|
8
+ @project = Project[project_id]
9
+ end
10
+
11
+ project.get '/users/*' do |user_id|
12
+ @user = User[user_id]
13
+ "#{@project}, #{@user}"
14
+ end
15
+
16
+ helpers do
17
+ def auth; true end
18
+ end
19
+
20
+ scope(:authorized) { auth }
21
+
22
+ authorized.get '/sekret/*' do |page|
23
+ page
24
+ end
data/examples.rb ADDED
@@ -0,0 +1,20 @@
1
+ $:.unshift 'lib'
2
+ require 'example_app'
3
+ set :environment, :test
4
+
5
+ require 'exemplor'
6
+ require 'rack/test'
7
+
8
+ eg.helpers do
9
+ include Rack::Test::Methods
10
+ def app; Sinatra::Application end
11
+ end
12
+
13
+ eg "Requesting an action built from a scope" do
14
+ Check(get('/projects/a/users/2').body).is('Project A, User 2')
15
+ Check(get('/projects/b/users/1').body).is('Project B, User 1')
16
+ end
17
+
18
+ eg "Scope with no path pattern" do
19
+ Check(get('/sekret/squirrel').body).is('squirrel')
20
+ end
@@ -0,0 +1,49 @@
1
+ module Sinatra
2
+
3
+ class Scope
4
+
5
+ def initialize(app, pattern, &blk)
6
+ @app = app
7
+ @pattern = pattern || ''
8
+ @block = blk
9
+ end
10
+
11
+ attr_reader :pattern, :block
12
+
13
+ def create_action(http_method, pattern, &blk)
14
+ scope = self
15
+ @app.send(http_method, scope.pattern + pattern) do |*args|
16
+ # run the scope block
17
+ self.instance_exec(*args[0...scope.block.arity], &scope.block)
18
+ # run the action block
19
+ self.instance_exec(*args[scope.block.arity..-1], &blk)
20
+ end
21
+ end
22
+
23
+ def get(pattern, &blk)
24
+ create_action :get, pattern, &blk
25
+ end
26
+
27
+ def post(pattern, &blk)
28
+ create_action :post, pattern, &blk
29
+ end
30
+
31
+ end
32
+
33
+ module ScopeBuilder
34
+
35
+ def scope(name, pattern = nil, &blk)
36
+ @scopes ||= {}
37
+ @scopes[name] = Scope.new Sinatra::Application, pattern, &blk
38
+ end
39
+
40
+ # hacky? I can't figure out which object to #define_method on
41
+ def method_missing(name)
42
+ return super unless @scopes.has_key? name
43
+ @scopes[name]
44
+ end
45
+
46
+ end
47
+
48
+ register ScopeBuilder
49
+ end
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sinatra-scopes}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Myles Byrne"]
12
+ s.date = %q{2009-11-04}
13
+ s.email = %q{myles@myles.id.au}
14
+ s.extra_rdoc_files = [
15
+ "README.md"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "README.md",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "example_app.rb",
23
+ "examples.rb",
24
+ "lib/sinatra/scopes.rb",
25
+ "sinatra-scopes.gemspec"
26
+ ]
27
+ s.homepage = %q{http://github.com/quackingduck/sinatra-scopes}
28
+ s.rdoc_options = ["--charset=UTF-8"]
29
+ s.require_paths = ["lib"]
30
+ s.rubygems_version = %q{1.3.5}
31
+ s.summary = %q{Simple request handler scoping for sinatra}
32
+
33
+ if s.respond_to? :specification_version then
34
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
35
+ s.specification_version = 3
36
+
37
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
38
+ s.add_runtime_dependency(%q<sinatra>, [">= 0.9.4"])
39
+ s.add_development_dependency(%q<exemplor>, [">= 2000.0.0"])
40
+ s.add_development_dependency(%q<rack-test>, ["= 0.4.0"])
41
+ else
42
+ s.add_dependency(%q<sinatra>, [">= 0.9.4"])
43
+ s.add_dependency(%q<exemplor>, [">= 2000.0.0"])
44
+ s.add_dependency(%q<rack-test>, ["= 0.4.0"])
45
+ end
46
+ else
47
+ s.add_dependency(%q<sinatra>, [">= 0.9.4"])
48
+ s.add_dependency(%q<exemplor>, [">= 2000.0.0"])
49
+ s.add_dependency(%q<rack-test>, ["= 0.4.0"])
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Myles Byrne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-04 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.4
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: exemplor
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2000.0.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack-test
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.4.0
44
+ version:
45
+ description:
46
+ email: myles@myles.id.au
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README.md
53
+ files:
54
+ - .gitignore
55
+ - README.md
56
+ - Rakefile
57
+ - VERSION
58
+ - example_app.rb
59
+ - examples.rb
60
+ - lib/sinatra/scopes.rb
61
+ - sinatra-scopes.gemspec
62
+ has_rdoc: true
63
+ homepage: http://github.com/quackingduck/sinatra-scopes
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Simple request handler scoping for sinatra
90
+ test_files: []
91
+