toy-locomotive 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/toy-locomotive/resources/controller.rb +76 -0
- data/lib/toy-locomotive/router/controller.rb +16 -7
- data/lib/toy-locomotive/version.rb +1 -1
- data/lib/toy-locomotive.rb +2 -0
- data/spec/lib/resources_controller_spec.rb +39 -0
- data/spec/lib/router_controller_spec.rb +37 -0
- metadata +12 -10
data/Gemfile.lock
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
module ToyLocomotive::Resources::Controller
|
2
|
+
|
3
|
+
module ClassMethods
|
4
|
+
def extract_resources args
|
5
|
+
actions = [:index, :new, :create, :show, :edit, :update, :destroy]
|
6
|
+
return actions if args == :all
|
7
|
+
return args[:only] if args[:only]
|
8
|
+
actions - args[:except]
|
9
|
+
end
|
10
|
+
|
11
|
+
def resources args
|
12
|
+
extract_resources(args).each{|action| send :"set_action_#{action}"}
|
13
|
+
end
|
14
|
+
|
15
|
+
def set_action_new
|
16
|
+
get 'new' do
|
17
|
+
extract_parent_vars
|
18
|
+
parent = instance_variable_get (model = self.class.extract_model).belongs_chain.reverse.pop.to_member_var
|
19
|
+
instance_variable_set model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_action_index
|
24
|
+
get 'index' do
|
25
|
+
extract_parent_vars
|
26
|
+
extract_collection_var
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_action_show
|
31
|
+
get 'show' do
|
32
|
+
extract_parent_vars
|
33
|
+
extract_member_var
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_action_edit
|
38
|
+
get 'edit' do
|
39
|
+
extract_parent_vars
|
40
|
+
extract_member_var
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_action_create
|
45
|
+
post 'create' do
|
46
|
+
vars = extract_parent_vars
|
47
|
+
parent = instance_variable_get (model = self.class.extract_model).belongs_chain.reverse.pop.to_member_var
|
48
|
+
member = (parent ? parent.send(model.to_s.underscore.pluralize) : model).new(params[model.to_params])
|
49
|
+
instance_variable_set model.to_member_var, member
|
50
|
+
vars = vars << member
|
51
|
+
return redirect_to vars, notice: 'Burrito was successfully created.' if member.save
|
52
|
+
render action: 'new'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_action_update
|
57
|
+
put 'update' do
|
58
|
+
vars = extract_parent_vars
|
59
|
+
vars = vars << (member = extract_member_var)
|
60
|
+
return redirect_to vars, notice: 'Burrito was successfully updated.' if member.update_attributes(params[member.class.to_s.underscore.to_sym])
|
61
|
+
render action: 'edit'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_action_destroy
|
66
|
+
delete 'destroy' do
|
67
|
+
vars = extract_parent_vars
|
68
|
+
extract_member_var.destroy
|
69
|
+
redirect_to action: :index, notice: 'Burrito was successfully deleted'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
ActionController::Base.extend ToyLocomotive::Resources::Controller::ClassMethods
|
@@ -5,10 +5,10 @@ module ToyLocomotive::Router::Controller
|
|
5
5
|
%w(get put post delete).each {|via| eval "def #{via} path, opts={}, &blk; match_action \"#{via}\", path, opts, blk; end"}
|
6
6
|
|
7
7
|
def match_action method, path, opts, blk
|
8
|
-
action = extract_action path, opts
|
9
|
-
extract_filter action, path, opts
|
8
|
+
action = extract_action path, opts, method
|
9
|
+
extract_filter action, path, opts, method
|
10
|
+
as = extract_as path, opts, method
|
10
11
|
path = extract_path path, opts
|
11
|
-
as = extract_as path, opts
|
12
12
|
controller = extract_controller
|
13
13
|
add_route method, action, path, as, controller
|
14
14
|
define_method action, blk
|
@@ -17,6 +17,7 @@ module ToyLocomotive::Router::Controller
|
|
17
17
|
def add_route method, action, path, as, controller
|
18
18
|
ToyLocomotive.routes ||= []
|
19
19
|
ToyLocomotive.routes << {method: method, action: action, path: path, controller: controller, as: as}
|
20
|
+
puts ({method: method, action: action, path: path, controller: controller, as: as}.inspect)
|
20
21
|
end
|
21
22
|
|
22
23
|
def add_member_filter action
|
@@ -30,15 +31,22 @@ module ToyLocomotive::Router::Controller
|
|
30
31
|
end
|
31
32
|
|
32
33
|
def extract_path path, opts={}
|
33
|
-
path
|
34
|
+
return path if path[0] == '/'
|
35
|
+
return "#{extract_model.route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || ['edit','new'].include?(path)
|
36
|
+
return "#{extract_model.route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
|
37
|
+
return "#{extract_model.route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
|
38
|
+
"#{extract_model.route_chain}/#{extract_model.to_s.underscore.pluralize}/#{path.parameterize}"
|
34
39
|
end
|
35
40
|
|
36
|
-
def extract_as path, opts={}
|
41
|
+
def extract_as path, opts={}, method='get'
|
42
|
+
return extract_model.to_as.pluralize if path == 'index'
|
43
|
+
return extract_model.to_as if path == 'show'
|
44
|
+
return nil if method != 'get'
|
37
45
|
action = extract_action path, opts
|
38
46
|
path[0] == '/' ? action : "#{action}_#{extract_model.to_as}"
|
39
47
|
end
|
40
48
|
|
41
|
-
def extract_action path, opts={}
|
49
|
+
def extract_action path, opts={}, method='get'
|
42
50
|
(opts[:as] || (path == '/' ? 'root' : path)).parameterize.underscore
|
43
51
|
end
|
44
52
|
|
@@ -50,8 +58,9 @@ module ToyLocomotive::Router::Controller
|
|
50
58
|
extract_controller.singularize.camelize.constantize
|
51
59
|
end
|
52
60
|
|
53
|
-
def extract_filter action, path, opts
|
61
|
+
def extract_filter action, path, opts, method
|
54
62
|
return if path[0] == '/'
|
63
|
+
return if %w(index show new edit destroy update create).include? path
|
55
64
|
send :"add_#{opts[:on]}_filter", action
|
56
65
|
end
|
57
66
|
|
data/lib/toy-locomotive.rb
CHANGED
@@ -4,10 +4,12 @@ require "toy-locomotive/version"
|
|
4
4
|
module ToyLocomotive
|
5
5
|
|
6
6
|
module Router; end
|
7
|
+
module Resources; end
|
7
8
|
mattr_accessor :routes
|
8
9
|
|
9
10
|
end
|
10
11
|
|
11
12
|
require "toy-locomotive/router/model"
|
12
13
|
require "toy-locomotive/router/controller"
|
14
|
+
require "toy-locomotive/resources/controller"
|
13
15
|
require "toy-locomotive/initializer"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DogsController do
|
4
|
+
describe 'extract_resources' do
|
5
|
+
|
6
|
+
it 'acepts :all as param' do
|
7
|
+
DogsController.extract_resources(:all).should == [:index, :new, :create, :show, :edit, :update, :destroy]
|
8
|
+
end
|
9
|
+
it 'acepts :only as param' do
|
10
|
+
DogsController.extract_resources(only: [:index, :show]).should == [:index, :show]
|
11
|
+
end
|
12
|
+
it 'acepts :except as param' do
|
13
|
+
DogsController.extract_resources(except: [:index, :destroy]).should == [:new, :create, :show, :edit, :update]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'set_action' do
|
18
|
+
it "executes index" do
|
19
|
+
DogsController.resources(only: [:index])
|
20
|
+
DogsController.new.index.class.should == Array
|
21
|
+
end
|
22
|
+
|
23
|
+
it "executes new" do
|
24
|
+
DogsController.resources(only: [:new])
|
25
|
+
DogsController.new.new.human.should == Human.first
|
26
|
+
end
|
27
|
+
|
28
|
+
it "executes show" do
|
29
|
+
DogsController.resources(only: [:show])
|
30
|
+
DogsController.new.show.should == Dog.first
|
31
|
+
end
|
32
|
+
|
33
|
+
it "executes edit" do
|
34
|
+
DogsController.resources(only: [:edit])
|
35
|
+
DogsController.new.show.should == Dog.first
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -68,4 +68,41 @@ describe DogsController do
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
describe 'resources' do
|
72
|
+
it "gets an index" do
|
73
|
+
DogsController.get('index') { 'index' }
|
74
|
+
DogsController.new.index.should == 'index'
|
75
|
+
end
|
76
|
+
|
77
|
+
it "gets a show" do
|
78
|
+
DogsController.get('show') { 'show' }
|
79
|
+
DogsController.new.show.should == 'show'
|
80
|
+
end
|
81
|
+
|
82
|
+
it "gets a new" do
|
83
|
+
DogsController.get('new') { 'new' }
|
84
|
+
DogsController.new.new.should == 'new'
|
85
|
+
end
|
86
|
+
|
87
|
+
it "gets a edit" do
|
88
|
+
DogsController.get('edit') { 'edit' }
|
89
|
+
DogsController.new.edit.should == 'edit'
|
90
|
+
end
|
91
|
+
|
92
|
+
it "puts a update" do
|
93
|
+
DogsController.put('update') { 'update' }
|
94
|
+
DogsController.new.update.should == 'update'
|
95
|
+
end
|
96
|
+
|
97
|
+
it "posts a create" do
|
98
|
+
DogsController.post('create') { 'create' }
|
99
|
+
DogsController.new.create.should == 'create'
|
100
|
+
end
|
101
|
+
|
102
|
+
it "deletes a destroy" do
|
103
|
+
DogsController.delete('destroy') { 'destroy' }
|
104
|
+
DogsController.new.destroy.should == 'destroy'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
71
108
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toy-locomotive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &25866492 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *25866492
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec-rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &25865832 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *25865832
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &25865424 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *25865424
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: shoulda
|
49
|
-
requirement: &
|
49
|
+
requirement: &25864668 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *25864668
|
58
58
|
description: a Different aproach to Rails applications
|
59
59
|
email:
|
60
60
|
- mortaro@towsta.com
|
@@ -68,10 +68,12 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- lib/toy-locomotive.rb
|
70
70
|
- lib/toy-locomotive/initializer.rb
|
71
|
+
- lib/toy-locomotive/resources/controller.rb
|
71
72
|
- lib/toy-locomotive/router/controller.rb
|
72
73
|
- lib/toy-locomotive/router/model.rb
|
73
74
|
- lib/toy-locomotive/version.rb
|
74
75
|
- readme
|
76
|
+
- spec/lib/resources_controller_spec.rb
|
75
77
|
- spec/lib/router_controller_spec.rb
|
76
78
|
- spec/lib/router_model_spec.rb
|
77
79
|
- spec/lib/toy_locomotive_spec.rb
|