cachafla-yard-sinatra 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ copyright (c) 2010 Konstantin Haase. All rights reserved.
2
+
3
+ Developed by: Konstantin Haase
4
+ http://github.com/rkh/big_band
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to
8
+ deal with the Software without restriction, including without limitation the
9
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
+ sell copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+ 1. Redistributions of source code must retain the above copyright notice,
13
+ this list of conditions and the following disclaimers.
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimers in the
16
+ documentation and/or other materials provided with the distribution.
17
+ 3. Neither the name of Konstantin Haase, nor the names of other contributors
18
+ may be used to endorse or promote products derived from this Software without
19
+ specific prior written permission.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27
+ WITH THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ YARD::Sinatra
2
+ =============
3
+
4
+ This plugin adds [Sinatra](http://sinatrarb.com) routes to [YARD](http://yardoc.org/) output.
5
+
6
+ BigBand
7
+ -------
8
+
9
+ YARD::Sinatra is part of the [BigBand](http://github.com/rkh/big_band) stack.
10
+ Check it out if you are looking for fancy Sinatra extensions.
11
+
12
+ Usage
13
+ -----
14
+
15
+ Install via gemcutter:
16
+
17
+ gem install yard-sinatra
18
+
19
+ Add comments to your routes (well, that's optional):
20
+
21
+ require "sinatra/base"
22
+ require "user"
23
+
24
+ class ExampleApp < Sinatra::Base
25
+
26
+ # Settings for a given user
27
+ #
28
+ # @param [User] some user
29
+ # @return [Hash] settings for that user
30
+ def settings(some_user)
31
+ raise NotImplementedMethod
32
+ end
33
+
34
+ # Displays a settings page for the current user
35
+ #
36
+ # @see ExampleApp#settings
37
+ get "/settings" do
38
+ haml :settings, {}, :settings => settings(current_user)
39
+ end
40
+
41
+ # Error 404 Page Not Found
42
+ not_found do
43
+ haml :'404'
44
+ end
45
+
46
+ end
47
+
48
+ The you're ready to go:
49
+
50
+ yardoc example_app.rb
51
+
52
+ YARD will automatically detect the yard-sinatra plugin and load it.
53
+
54
+ Other use cases
55
+ ---------------
56
+
57
+ As with yard, this can be used for other means besides documentation.
58
+ For instance, you might want a list of all routes defined in a given list of files without executing those files:
59
+
60
+ require "yard/sinatra"
61
+ YARD::Registry.load Dir.glob("lib/**/*.rb")
62
+ YARD::Sinatra.routes.each do |route|
63
+ puts route.http_verb, route.http_path, route.file, route.docstring
64
+ end
65
+
66
+ Thanks
67
+ ------
68
+
69
+ * Ryan Sobol for implementing `not_found` documentation.
70
+ * Loren Segal for making it seamlessly work as YARD plugin.
71
+ Well, and for YARD.
@@ -0,0 +1,151 @@
1
+ require "yard"
2
+
3
+ module YARD
4
+
5
+ module Sinatra
6
+ def self.routes
7
+ YARD::Handlers::Sinatra::AbstractRouteHandler.routes
8
+ end
9
+
10
+ def self.error_handlers
11
+ YARD::Handlers::Sinatra::AbstractRouteHandler.error_handlers
12
+ end
13
+ end
14
+
15
+ module CodeObjects
16
+ class RouteObject < MethodObject
17
+ attr_accessor :http_verb, :http_path, :real_name
18
+
19
+ def name(prefix = false)
20
+ return super unless show_real_name?
21
+ prefix ? (sep == ISEP ? "#{sep}#{real_name}" : real_name.to_s) : real_name.to_sym
22
+ end
23
+
24
+ # @see YARD::Handlers::Sinatra::AbstractRouteHandler#register_route
25
+ # @see #name
26
+ def show_real_name?
27
+ real_name and caller[1] =~ /`signature'/
28
+ end
29
+ end
30
+ end
31
+
32
+ module Handlers
33
+
34
+ # Displays Sinatra routes in YARD documentation.
35
+ # Can also be used to parse routes from files without executing those files.
36
+ module Sinatra
37
+
38
+ # Logic both handlers have in common.
39
+ module AbstractRouteHandler
40
+ def self.routes
41
+ @routes ||= []
42
+ end
43
+
44
+ def self.error_handlers
45
+ @error_handlers ||= []
46
+ end
47
+
48
+ def process
49
+ case http_verb
50
+ when 'NOT_FOUND'
51
+ register_error_handler(http_verb)
52
+ else
53
+ path = http_path
54
+ path = $1 if path =~ /^"(.*)"$/
55
+ register_route(http_verb, path)
56
+ end
57
+ end
58
+
59
+ def register_route(verb, path, doc = nil)
60
+ # HACK: Removing some illegal letters.
61
+ method_name = "" << verb << "_" << path.gsub(/[^\w_]/, "_")
62
+ real_name = "" << verb << " " << path
63
+ route = register CodeObjects::RouteObject.new(namespace, method_name, :instance) do |o|
64
+ o.visibility = "public"
65
+ o.source = statement.source
66
+ o.signature = real_name
67
+ o.explicit = true
68
+ o.scope = scope
69
+ o.docstring = statement.comments
70
+ o.http_verb = verb
71
+ o.http_path = path
72
+ o.real_name = real_name
73
+ o.add_file(parser.file, statement.line)
74
+ end
75
+ AbstractRouteHandler.routes << route
76
+ yield(route) if block_given?
77
+ end
78
+
79
+ def register_error_handler(verb, doc = nil)
80
+ error_handler = register CodeObjects::RouteObject.new(namespace, verb, :instance) do |o|
81
+ o.visibility = "public"
82
+ o.source = statement.source
83
+ o.signature = verb
84
+ o.explicit = true
85
+ o.scope = scope
86
+ o.docstring = statement.comments
87
+ o.http_verb = verb
88
+ o.real_name = verb
89
+ o.add_file(parser.file, statement.line)
90
+ end
91
+ AbstractRouteHandler.error_handlers << error_handler
92
+ yield(error_handler) if block_given?
93
+ end
94
+ end
95
+
96
+ # Route handler for YARD's source parser.
97
+ class RouteHandler < Ruby::Base
98
+ include AbstractRouteHandler
99
+
100
+ handles method_call(:get)
101
+ handles method_call(:post)
102
+ handles method_call(:put)
103
+ handles method_call(:delete)
104
+ handles method_call(:head)
105
+ handles method_call(:not_found)
106
+
107
+ def http_verb
108
+ statement.method_name(true).to_s.upcase
109
+ end
110
+
111
+ def http_path
112
+ statement.parameters.first.source
113
+ end
114
+ end
115
+
116
+ # Route handler for YARD's legacy parser.
117
+ module Legacy
118
+ class RouteHandler < Ruby::Legacy::Base
119
+ VERBS = %w[get head post put delete not_found]
120
+ include AbstractRouteHandler
121
+ handles /\A.*(get|post|put|delete|head|not_found.*)[\s\(].*/m
122
+
123
+ def http_verb
124
+ method_call_string = ''
125
+ space = YARD::Parser::Ruby::Legacy::RubyToken::TkSPACE
126
+ lparen = YARD::Parser::Ruby::Legacy::RubyToken::TkLPAREN
127
+
128
+ for token in statement.tokens
129
+ if token.is_a?(space) || token.is_a?(lparen)
130
+ break
131
+ else
132
+ method_call_string << token.text
133
+ end
134
+ end
135
+
136
+ VERBS.detect{|v| method_call_string.include?(v)}.upcase
137
+ end
138
+
139
+ def http_path
140
+ path = statement.tokens.detect do |t|
141
+ t.is_a?(YARD::Parser::Ruby::Legacy::RubyToken::TkSTRING)
142
+ end
143
+
144
+ path.text
145
+ end
146
+ end
147
+ end
148
+
149
+ end
150
+ end
151
+ end
@@ -0,0 +1 @@
1
+ require 'yard/sinatra'
@@ -0,0 +1,31 @@
1
+ require "sinatra/base"
2
+ require "user"
3
+
4
+ class ExampleApp < Sinatra::Base
5
+
6
+ # Settings for a given user
7
+ #
8
+ # @param [User] some user
9
+ # @return [Hash] settings for that user
10
+ def settings(some_user)
11
+ raise NotImplementedMethod
12
+ end
13
+
14
+ # Displays a settings page for the current user
15
+ #
16
+ # @see ExampleApp#settings
17
+ get "/settings" do
18
+ haml :settings, {}, :settings => settings(current_user)
19
+ end
20
+
21
+ # Error 404 Page Not Found
22
+ not_found do
23
+ haml :'404'
24
+ end
25
+
26
+ put("/settings") { }
27
+ delete("/settings") { }
28
+ post("/settings") { }
29
+ head("/settings") { }
30
+
31
+ end
@@ -0,0 +1,42 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module SessionAuth
5
+
6
+ def self.registered(app)
7
+ app.set :username, 'frank'
8
+ app.set :password, 'changeme'
9
+
10
+ # Displays a login form for the user
11
+ app.get '/login' do
12
+ "<form method='POST' action='/login'>" +
13
+ "<input type='text' name='user'>" +
14
+ "<input type='text' name='pass'>" +
15
+ "</form>"
16
+ end
17
+
18
+ # POST /login
19
+ app.post '/login' do
20
+ if params[:user] == options.username && params[:pass] == options.password
21
+ session[:authorized] = true
22
+ redirect '/'
23
+ else
24
+ session[:authorized] = false
25
+ redirect '/login'
26
+ end
27
+ end
28
+
29
+ # Error 404 Page Not Found
30
+ app.not_found '/test' do
31
+ haml :'404'
32
+ end
33
+
34
+ end
35
+ end
36
+
37
+ register SessionAuth
38
+ end
39
+
40
+ class MyApp < Sinatra::Base
41
+ register Sinatra::SessionAuth
42
+ end
@@ -0,0 +1,38 @@
1
+ require "yard/sinatra"
2
+
3
+ describe YARD::Sinatra do
4
+
5
+ before(:all) do
6
+ $NO_CONTINUATION_WARNING = true
7
+ YARD::Registry.load [File.expand_path("../../example_extension.rb", __FILE__)], true
8
+ end
9
+
10
+ describe "Sinatra Extensions" do
11
+
12
+ it "reads sinatra routes" do
13
+ YARD::Sinatra.routes.size.should == 2
14
+ end
15
+
16
+ it "sets properties correctly" do
17
+ YARD::Sinatra.routes.each do |route|
18
+ %w[GET HEAD POST PUT DELETE].should include(route.http_verb)
19
+ route.http_path.should =~ /login/
20
+ route.file.should =~ /example_extension\.rb$/
21
+ route.docstring.should =~ /Displays a login form for the user/ if route.http_verb == "GET"
22
+ end
23
+ end
24
+
25
+ it "reads sinatra error handlers" do
26
+ YARD::Sinatra.error_handlers.size.should == 1
27
+ end
28
+
29
+ it "sets error handlers correctly" do
30
+ YARD::Sinatra.error_handlers.each do |error_handler|
31
+ %w[NOT_FOUND].should include(error_handler.http_verb)
32
+ error_handler.file.should =~ /example_extension\.rb$/
33
+ error_handler.docstring.should =~ /Error 404 Page Not Found/ if error_handler.http_verb == "NOT_FOUND"
34
+ end
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,38 @@
1
+ require "yard/sinatra"
2
+
3
+ describe YARD::Sinatra do
4
+
5
+ before(:all) do
6
+ $NO_CONTINUATION_WARNING = true
7
+ YARD::Registry.load [File.expand_path("../../example_app.rb", __FILE__)], true
8
+ end
9
+
10
+ describe "Sinatra Apps" do
11
+
12
+ it "reads sinatra routes" do
13
+ YARD::Sinatra.routes.size.should == 5
14
+ end
15
+
16
+ it "sets properties correctly" do
17
+ YARD::Sinatra.routes.each do |route|
18
+ %w[GET HEAD POST PUT DELETE].should include(route.http_verb)
19
+ route.http_path.should =~ /settings/
20
+ route.file.should =~ /example_app\.rb$/
21
+ route.docstring.should =~ /Displays a settings page for the current user/ if route.http_verb == "GET"
22
+ end
23
+ end
24
+
25
+ it "reads sinatra error handlers" do
26
+ YARD::Sinatra.error_handlers.size.should == 1
27
+ end
28
+
29
+ it "sets error handlers correctly" do
30
+ YARD::Sinatra.error_handlers.each do |error_handler|
31
+ %w[NOT_FOUND].should include(error_handler.http_verb)
32
+ error_handler.file.should =~ /example_app\.rb$/
33
+ error_handler.docstring.should =~ /Error 404 Page Not Found/ if error_handler.http_verb == "NOT_FOUND"
34
+ end
35
+ end
36
+ end
37
+
38
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cachafla-yard-sinatra
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
+ platform: ruby
12
+ authors:
13
+ - Konstantin Haase
14
+ - Andres Rodriguez
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-02-14 00:00:00 -08:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: yard
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 0
33
+ - 6
34
+ - 0
35
+ version: 0.6.0
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 27
47
+ segments:
48
+ - 1
49
+ - 3
50
+ - 0
51
+ version: 1.3.0
52
+ type: :development
53
+ version_requirements: *id002
54
+ description: Displays Sinatra routes (including comments) in YARD output (part of BigBand).
55
+ email: andres@joyent.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - lib/yard/sinatra.rb
64
+ - lib/yard-sinatra.rb
65
+ - spec/example_app.rb
66
+ - spec/example_extension.rb
67
+ - spec/yard/sinatra_extension_spec.rb
68
+ - spec/yard/sinatra_spec.rb
69
+ - README.md
70
+ - LICENSE
71
+ has_rdoc: yard
72
+ homepage: http://github.com/cachafla/cachafla-yard-sinatra
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.7
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Displays Sinatra routes (including comments) in YARD output (part of BigBand).
105
+ test_files: []
106
+