toy-verbs 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +84 -0
- data/Rakefile +27 -0
- data/lib/tasks/toy-verbs_tasks.rake +4 -0
- data/lib/toy-verbs.rb +8 -0
- data/lib/toy-verbs/matcher.rb +59 -0
- data/lib/toy-verbs/railtie.rb +13 -0
- data/lib/toy-verbs/version.rb +3 -0
- metadata +103 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
Toy Verbs
|
2
|
+
=========
|
3
|
+
|
4
|
+
Toy Verbs is a component of [Toy Locomotive](https://github.com/Mortaro/Toy-Locomotive]), but can be used independently outside Toy Locomotive.
|
5
|
+
|
6
|
+
|
7
|
+
Getting Started
|
8
|
+
---------------
|
9
|
+
|
10
|
+
All you need to start using Toy Attributes is to add the following line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem "toy-verbs"
|
13
|
+
|
14
|
+
Routing with Verbs
|
15
|
+
------------------
|
16
|
+
|
17
|
+
This gem allows you to declare a route and an action inside your controller. In order to make it happen, your controller will have access to the following class methods:
|
18
|
+
|
19
|
+
get, post, put, delete, match
|
20
|
+
|
21
|
+
Each verb is followed by a route, which will be mapped to a generated action. A block is accepted to define the action behavior, but it can also use a clean default:
|
22
|
+
|
23
|
+
class SiteController < ApplicationController
|
24
|
+
|
25
|
+
get '/' do
|
26
|
+
render text: 'welcome'
|
27
|
+
end
|
28
|
+
|
29
|
+
get '/contact'
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
In this example, accessing `http://localhost:3000/` in your browser will render the text "welcome", and accessing `http://localhost:300/contact` will invoke the view `site/contact`
|
34
|
+
|
35
|
+
The verb `get` also accepts multiple routes if they have empty actions, as many symbols and/or strings can be passed to this verb and each will generate a route.
|
36
|
+
|
37
|
+
class SiteController < ApplicationController
|
38
|
+
|
39
|
+
get '/', '/terms-of-service'
|
40
|
+
|
41
|
+
get :root, :terms_of_service
|
42
|
+
|
43
|
+
get '/', :terms_of_service
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
*Note that `:root` is the symbolic representation of the special path `'/'`*
|
48
|
+
|
49
|
+
Verbs Options
|
50
|
+
-------------
|
51
|
+
|
52
|
+
Besides the path, each verb also accepts a hash with options.
|
53
|
+
|
54
|
+
Every route declared using a verb will generate a path helper, just like the named routes defined in `config/routes.rb`. You can see the pattern that generates the helpers below:
|
55
|
+
|
56
|
+
get '/' # => root_path
|
57
|
+
get '/terms-of-service' # => terms_of_service_path
|
58
|
+
get :posts, :tags # => posts_path, tags_path
|
59
|
+
get '/posts/:post_id/:comments/:comment_id' # => post_comment_path
|
60
|
+
|
61
|
+
This behavior can be overwritten by passing the ':as' option to the verb:
|
62
|
+
|
63
|
+
get '/terms-of-service', as: 'tos' # => tos_path
|
64
|
+
|
65
|
+
As said before, an action will be generated, but this behavior can also be overwritten by passing the option ':action' to the verb. It is usefull for inherited actions or actions that you do not have explicit access to.
|
66
|
+
|
67
|
+
class ApplicationController < ActionController:Base
|
68
|
+
def save_contact
|
69
|
+
# save the contact from a form and send copy via e-mail
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class SiteController < ApplicationController
|
74
|
+
post '/contact', action: 'save_contact'
|
75
|
+
end
|
76
|
+
|
77
|
+
Drawing Routes
|
78
|
+
--------------
|
79
|
+
|
80
|
+
Even if you still can write routes normaly on your `config/routes.rb`, it's possible to evaluate directly from inside your controller using `draw`, which may turn up being useful if you are using gems like devise, or even writing a plugin for rails:
|
81
|
+
|
82
|
+
class UsersController < ApplicationController
|
83
|
+
draw :devise_for, :users
|
84
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'ToyVerbs'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
data/lib/toy-verbs.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module ToyVerbs::Matcher
|
2
|
+
|
3
|
+
ToyVerbs::VERBS.each do |verb|
|
4
|
+
define_method verb do |path, opts={}, &blk|
|
5
|
+
route_action verb, path, opts, &blk
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
alias :_get :get
|
10
|
+
|
11
|
+
def get *args, &blk
|
12
|
+
return _get *args, &blk unless args[1].is_a?(Symbol) || args[1].is_a?(String)
|
13
|
+
args.each { |action| _get action }
|
14
|
+
end
|
15
|
+
|
16
|
+
def draw *args
|
17
|
+
router = Rails.application.routes
|
18
|
+
router.disable_clear_and_finalize = true
|
19
|
+
router.draw { send(*args) }
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def route_action verb, path, opts, &blk
|
25
|
+
path = extract_path path
|
26
|
+
draw verb, path => "#{extract_controller}##{extract_action(path, opts)}", as: extract_name(path, opts)
|
27
|
+
return if opts[:action]
|
28
|
+
blk = blk || Proc.new {}
|
29
|
+
define_method extract_action(path, opts).to_sym, &blk
|
30
|
+
end
|
31
|
+
|
32
|
+
def extract_controller
|
33
|
+
to_s.gsub('Controller', '').underscore
|
34
|
+
end
|
35
|
+
|
36
|
+
def extract_path path
|
37
|
+
path.is_a?(Symbol) ? "/#{path.to_s.gsub('_','-')}" : path
|
38
|
+
end
|
39
|
+
|
40
|
+
def extract_action path, opts
|
41
|
+
opts[:action] || path_to_method(path)
|
42
|
+
end
|
43
|
+
|
44
|
+
def extract_name path, opts
|
45
|
+
opts[:as] || path_to_method(path)
|
46
|
+
end
|
47
|
+
|
48
|
+
def path_to_method path
|
49
|
+
return 'root' if path == '/'
|
50
|
+
fragments = path.split('/')
|
51
|
+
fragments.each_with_index.map do |fragment, index|
|
52
|
+
next if fragment[0] == ':'
|
53
|
+
((fragments[index+1] && fragments[index+1][0] == ':') ? fragment.singularize : fragment).parameterize.underscore
|
54
|
+
end.delete_if{ |fragment| fragment.blank? }.join('_')
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
ActionController::Base.extend ToyVerbs::Matcher
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ToyVerbs
|
2
|
+
|
3
|
+
def self.load! dir_path
|
4
|
+
Dir[dir_path].each { |file| load file }
|
5
|
+
end
|
6
|
+
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
loader = Proc.new { ToyVerbs.load! "#{Rails.root}/app/controllers/*.rb" }
|
9
|
+
config.to_prepare &loader
|
10
|
+
initializer 'toy_attributes.load_routes', :after => :disable_dependency_loading, &loader
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toy-verbs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christian Mortaro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &30142308 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *30142308
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec-rails
|
27
|
+
requirement: &30142056 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *30142056
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: shoulda
|
38
|
+
requirement: &30141804 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *30141804
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &30141540 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *30141540
|
58
|
+
description: Allows to use routing verbs directly inside a Rails controller
|
59
|
+
email:
|
60
|
+
- christian.mortaro@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- lib/tasks/toy-verbs_tasks.rake
|
66
|
+
- lib/toy-verbs/matcher.rb
|
67
|
+
- lib/toy-verbs/railtie.rb
|
68
|
+
- lib/toy-verbs/version.rb
|
69
|
+
- lib/toy-verbs.rb
|
70
|
+
- MIT-LICENSE
|
71
|
+
- Rakefile
|
72
|
+
- README.md
|
73
|
+
homepage: https://github.com/Mortaro/Toy-Verbs
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: -471493315
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
hash: -471493315
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.8.16
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Handles the routing verbs for Toy-Locomotive
|
103
|
+
test_files: []
|