toy-locomotive 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
1
- module ToyLocomotive::Router::Block
2
-
3
- def draw *args
4
- (@_draws ||= []).push args
5
- end
6
-
7
- def draws
8
- @_draws || []
9
- end
10
-
11
- end
12
-
13
- ActionController::Base.extend ToyLocomotive::Router::Block
1
+ module ToyLocomotive::Router::Block
2
+
3
+ def draw *args
4
+ (@_draws ||= []).push args
5
+ end
6
+
7
+ def draws
8
+ @_draws || []
9
+ end
10
+
11
+ end
12
+
13
+ ActionController::Base.extend ToyLocomotive::Router::Block
@@ -1,134 +1,134 @@
1
- module ToyLocomotive::Router::Controller
2
-
3
- module ClassMethods
4
-
5
- %w(get put post delete).each {|via| eval "def #{via} path, opts={}, &blk; match_action \"#{via}\", path, opts, blk; end"}
6
-
7
- def nested *args
8
- @_nested = *args
9
- end
10
-
11
- def belongs_chain
12
- (@_nested || []).map{|m| m.to_s.classify.constantize}
13
- end
14
-
15
- def route_chain
16
- belongs_chain.map{|m| m.to_route}.join
17
- end
18
-
19
- def route_as
20
- return "" unless extract_model
21
- belongs_chain.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? extract_model.to_s.underscore : "_#{extract_model.to_s.underscore}")
22
- end
23
-
24
- def match_action method, path, opts, blk
25
- action = extract_action path, opts, method
26
- extract_filter action, path, opts, method
27
- as = extract_as path, opts, method
28
- path = extract_path path, opts
29
- controller = extract_controller
30
- add_route method, action, path, as, controller
31
- define_method action, blk
32
- end
33
-
34
- def add_route method, action, path, as, controller
35
- ToyLocomotive.routes ||= []
36
- ToyLocomotive.routes << {method: method, action: action, path: path, controller: controller, as: as}
37
- puts ({method: method, action: action, path: path, controller: controller, as: as}.inspect)
38
- end
39
-
40
- def add_member_filter action
41
- @member_filters ||= []
42
- @member_filters << action.to_sym
43
- end
44
-
45
- def add_collection_filter action
46
- @collection_filters ||= []
47
- @collection_filters << action.to_sym
48
- end
49
-
50
- def extract_path path, opts={}
51
- return '/' if path == 'root'
52
- return path if path[0] == '/'
53
- return "/#{path.to_s.gsub('_','-').parameterize}" unless extract_model
54
- return "#{route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || path == 'edit'
55
- return "#{route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
56
- return "#{route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
57
- "#{route_chain}/#{extract_model.to_s.underscore.pluralize}/#{path.parameterize}"
58
- end
59
-
60
- def extract_as path, opts={}, method='get'
61
- return route_as.pluralize if path == 'index'
62
- return route_as if path == 'show'
63
- return nil if method != 'get'
64
- action = extract_action path, opts
65
- as = path[0] == '/' ? action : "#{action}"
66
- as << "_#{route_as}" if extract_model
67
- as
68
- end
69
-
70
- def extract_action path, opts={}, method='get'
71
- (opts[:as] || (path == '/' ? 'root' : path)).parameterize.underscore
72
- end
73
-
74
- def extract_controller
75
- to_s.gsub('Controller', '').downcase
76
- end
77
-
78
- def extract_model
79
- extract_controller.singularize.camelize.constantize rescue nil
80
- end
81
-
82
- def extract_filter action, path, opts, method
83
- return if path[0] == '/'
84
- return if %w(index show new edit destroy update create).include? path
85
- send :"add_#{opts[:on]}_filter", action if opts[:on]
86
- end
87
-
88
- def append_filters!
89
- before_filter :extract_parent_vars, only: ((@member_filters || []) + (@collection_filters || []))
90
- before_filter :extract_member_var, only: (@member_filters || [])
91
- before_filter :extract_collection_var, only: (@collection_filters || [])
92
- end
93
-
94
- end
95
-
96
- module InstanceMethods
97
-
98
- def extract_parent_vars
99
- chain = self.class.belongs_chain.reverse.clone
100
- vars = []
101
- if chain.any?
102
- root = chain.pop
103
- parent = root.find(params[root.to_params])
104
- instance_variable_set root.to_member_var, parent
105
- vars << parent
106
- chain.each do |model|
107
- parent = parent.send(model.to_s.underscore.pluralize).find(params[model.to_params])
108
- instance_variable_set model.to_member_var, parent
109
- vars << parent
110
- end
111
- end
112
- vars
113
- end
114
-
115
- def extract_member_var
116
- parent = self.class.belongs_chain.pop#reverse.pop
117
- model = self.class.extract_model
118
- parent = parent ? instance_variable_get(parent.to_member_var) : nil
119
- instance_variable_set(model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).find(params[model.to_params]))
120
- end
121
-
122
- def extract_collection_var
123
- parent = self.class.belongs_chain.pop#reverse.pop
124
- model = self.class.extract_model
125
- parent = parent ? instance_variable_get(parent.to_member_var) : nil
126
- instance_variable_set(model.to_collection_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model.all))
127
- end
128
-
129
- end
130
-
131
- end
132
-
133
- ActionController::Base.extend ToyLocomotive::Router::Controller::ClassMethods
134
- ActionController::Base.send :include, ToyLocomotive::Router::Controller::InstanceMethods
1
+ module ToyLocomotive::Router::Controller
2
+
3
+ module ClassMethods
4
+
5
+ %w(get put post delete).each {|via| eval "def #{via} path, opts={}, &blk; match_action \"#{via}\", path, opts, blk; end"}
6
+
7
+ def nested *args
8
+ @_nested = *args
9
+ end
10
+
11
+ def belongs_chain
12
+ (@_nested || []).map{|m| m.to_s.classify.constantize}
13
+ end
14
+
15
+ def route_chain
16
+ belongs_chain.map{|m| m.to_route}.join
17
+ end
18
+
19
+ def route_as
20
+ return "" unless extract_model
21
+ belongs_chain.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? extract_model.to_s.underscore : "_#{extract_model.to_s.underscore}")
22
+ end
23
+
24
+ def match_action method, path, opts, blk
25
+ action = extract_action path, opts, method
26
+ extract_filter action, path, opts, method
27
+ as = extract_as path, opts, method
28
+ path = extract_path path, opts
29
+ controller = extract_controller
30
+ add_route method, action, path, as, controller
31
+ define_method action, blk
32
+ end
33
+
34
+ def add_route method, action, path, as, controller
35
+ ToyLocomotive.routes ||= []
36
+ ToyLocomotive.routes << {method: method, action: action, path: path, controller: controller, as: as}
37
+ puts ({method: method, action: action, path: path, controller: controller, as: as}.inspect)
38
+ end
39
+
40
+ def add_member_filter action
41
+ @member_filters ||= []
42
+ @member_filters << action.to_sym
43
+ end
44
+
45
+ def add_collection_filter action
46
+ @collection_filters ||= []
47
+ @collection_filters << action.to_sym
48
+ end
49
+
50
+ def extract_path path, opts={}
51
+ return '/' if path == 'root'
52
+ return path if path[0] == '/'
53
+ return "/#{path.to_s.gsub('_','-').parameterize}" unless extract_model
54
+ return "#{route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || path == 'edit'
55
+ return "#{route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
56
+ return "#{route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
57
+ "#{route_chain}/#{extract_model.to_s.underscore.pluralize}/#{path.parameterize}"
58
+ end
59
+
60
+ def extract_as path, opts={}, method='get'
61
+ return route_as.pluralize if path == 'index'
62
+ return route_as if path == 'show'
63
+ return nil if method != 'get'
64
+ action = extract_action path, opts
65
+ as = path[0] == '/' ? action : "#{action}"
66
+ as << "_#{route_as}" if extract_model
67
+ as
68
+ end
69
+
70
+ def extract_action path, opts={}, method='get'
71
+ (opts[:as] || (path == '/' ? 'root' : path)).parameterize.underscore
72
+ end
73
+
74
+ def extract_controller
75
+ to_s.gsub('Controller', '').downcase
76
+ end
77
+
78
+ def extract_model
79
+ extract_controller.singularize.camelize.constantize rescue nil
80
+ end
81
+
82
+ def extract_filter action, path, opts, method
83
+ return if path[0] == '/'
84
+ return if %w(index show new edit destroy update create).include? path
85
+ send :"add_#{opts[:on]}_filter", action if opts[:on]
86
+ end
87
+
88
+ def append_filters!
89
+ before_filter :extract_parent_vars, only: ((@member_filters || []) + (@collection_filters || []))
90
+ before_filter :extract_member_var, only: (@member_filters || [])
91
+ before_filter :extract_collection_var, only: (@collection_filters || [])
92
+ end
93
+
94
+ end
95
+
96
+ module InstanceMethods
97
+
98
+ def extract_parent_vars
99
+ chain = self.class.belongs_chain.reverse.clone
100
+ vars = []
101
+ if chain.any?
102
+ root = chain.pop
103
+ parent = root.find(params[root.to_params])
104
+ instance_variable_set root.to_member_var, parent
105
+ vars << parent
106
+ chain.each do |model|
107
+ parent = parent.send(model.to_s.underscore.pluralize).find(params[model.to_params])
108
+ instance_variable_set model.to_member_var, parent
109
+ vars << parent
110
+ end
111
+ end
112
+ vars
113
+ end
114
+
115
+ def extract_member_var
116
+ parent = self.class.belongs_chain.pop#reverse.pop
117
+ model = self.class.extract_model
118
+ parent = parent ? instance_variable_get(parent.to_member_var) : nil
119
+ instance_variable_set(model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).find(params[model.to_params]))
120
+ end
121
+
122
+ def extract_collection_var
123
+ parent = self.class.belongs_chain.pop#reverse.pop
124
+ model = self.class.extract_model
125
+ parent = parent ? instance_variable_get(parent.to_member_var) : nil
126
+ instance_variable_set(model.to_collection_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model.all))
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+
133
+ ActionController::Base.extend ToyLocomotive::Router::Controller::ClassMethods
134
+ ActionController::Base.send :include, ToyLocomotive::Router::Controller::InstanceMethods
@@ -1,40 +1,40 @@
1
- module ToyLocomotive::Router::Model
2
-
3
- #def belongs_to_route
4
- # reflections = reflect_on_all_associations(:belongs_to)
5
- # reflections.any? ? reflections.first.name.to_s.camelize.constantize : nil
6
- #end
7
-
8
- #def belongs_chain chain=[]
9
- # parent_route = (chain.last || self).belongs_to_route
10
- # parent_route ? belongs_chain(chain << parent_route) : chain
11
- #end
12
-
13
- #def route_chain
14
- # belongs_chain.reverse.map{|m| m.to_route}.join
15
- #end
16
-
17
- def to_route
18
- s = to_s.parameterize.downcase
19
- "/#{s.pluralize}/:#{s}_id"
20
- end
21
-
22
- def to_member_var
23
- "@#{to_s.underscore}"
24
- end
25
-
26
- def to_collection_var
27
- to_member_var.pluralize
28
- end
29
-
30
- def to_params
31
- :"#{to_s.underscore}_id"
32
- end
33
-
34
- #def to_as
35
- # belongs_chain.reverse!.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? to_s.underscore : "_#{to_s.underscore}")
36
- #end
37
-
38
- end
39
-
40
- ActiveRecord::Base.extend ToyLocomotive::Router::Model
1
+ module ToyLocomotive::Router::Model
2
+
3
+ #def belongs_to_route
4
+ # reflections = reflect_on_all_associations(:belongs_to)
5
+ # reflections.any? ? reflections.first.name.to_s.camelize.constantize : nil
6
+ #end
7
+
8
+ #def belongs_chain chain=[]
9
+ # parent_route = (chain.last || self).belongs_to_route
10
+ # parent_route ? belongs_chain(chain << parent_route) : chain
11
+ #end
12
+
13
+ #def route_chain
14
+ # belongs_chain.reverse.map{|m| m.to_route}.join
15
+ #end
16
+
17
+ def to_route
18
+ s = to_s.parameterize.downcase
19
+ "/#{s.pluralize}/:#{s}_id"
20
+ end
21
+
22
+ def to_member_var
23
+ "@#{to_s.underscore}"
24
+ end
25
+
26
+ def to_collection_var
27
+ to_member_var.pluralize
28
+ end
29
+
30
+ def to_params
31
+ :"#{to_s.underscore}_id"
32
+ end
33
+
34
+ #def to_as
35
+ # belongs_chain.reverse!.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? to_s.underscore : "_#{to_s.underscore}")
36
+ #end
37
+
38
+ end
39
+
40
+ ActiveRecord::Base.extend ToyLocomotive::Router::Model
@@ -1,3 +1,3 @@
1
1
  module ToyLocomotive
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/readme CHANGED
@@ -1,15 +1,15 @@
1
- TOY LOCOMOTIVE?!
2
- ================
3
-
4
- Toy locomotive is a gem that runs over Rails....but Why?
5
-
6
- TO-DO
7
- =====
8
-
9
- * Better Documentation
10
- * Add models like data_mapper
11
- * Add a generator
12
- * Simplify folder structure
13
- * Block for extra routes
14
- * Basic crud
15
- * Lots more to make rails as simple as sinatra
1
+ TOY LOCOMOTIVE?!
2
+ ================
3
+
4
+ Toy locomotive is a gem that runs over Rails....but Why?
5
+
6
+ TO-DO
7
+ =====
8
+
9
+ * Better Documentation
10
+ * Add models like data_mapper
11
+ * Add a generator
12
+ * Simplify folder structure
13
+ * Block for extra routes
14
+ * Basic crud
15
+ * Lots more to make rails as simple as sinatra
@@ -1,40 +1,40 @@
1
- require 'spec_helper'
2
-
3
- describe ToyLocomotive::Attributes::AttributeChain do
4
-
5
- subject{ ToyLocomotive::Attributes::AttributeChain.new :title, Dog }
6
-
7
- its(:parent){should == Dog}
8
- its(:column){should == :title}
9
-
10
- it "defaults as string" do
11
- subject._as.should == :string
12
- end
13
-
14
- it "changes the type" do
15
- subject.as(:text)._as == :text
16
- end
17
-
18
- it "use text_field as helper for strings as default" do
19
- subject.as(:string).to_helper.should == :text_field
20
- end
21
-
22
- it "use text_area as helper for text as default" do
23
- subject.as(:text).to_helper.should == :text_area
24
- end
25
-
26
- it "use hidden_field as helper for ids as default" do
27
- attribute = ToyLocomotive::Attributes::AttributeChain.new :id, Dog
28
- attribute.to_helper.should == :hidden_field
29
- end
30
-
31
- it "use hidden_field as helper for relational ids as default" do
32
- attribute = ToyLocomotive::Attributes::AttributeChain.new :human_id, Dog
33
- attribute.to_helper.should == :hidden_field
34
- end
35
-
36
- it "acepts new helper" do
37
- subject.helper(:text_area).to_helper.should == :text_area
38
- end
39
-
40
- end
1
+ require 'spec_helper'
2
+
3
+ describe ToyLocomotive::Attributes::AttributeChain do
4
+
5
+ subject{ ToyLocomotive::Attributes::AttributeChain.new :title, Dog }
6
+
7
+ its(:parent){should == Dog}
8
+ its(:column){should == :title}
9
+
10
+ it "defaults as string" do
11
+ subject._as.should == :string
12
+ end
13
+
14
+ it "changes the type" do
15
+ subject.as(:text)._as == :text
16
+ end
17
+
18
+ it "use text_field as helper for strings as default" do
19
+ subject.as(:string).to_helper.should == :text_field
20
+ end
21
+
22
+ it "use text_area as helper for text as default" do
23
+ subject.as(:text).to_helper.should == :text_area
24
+ end
25
+
26
+ it "use hidden_field as helper for ids as default" do
27
+ attribute = ToyLocomotive::Attributes::AttributeChain.new :id, Dog
28
+ attribute.to_helper.should == :hidden_field
29
+ end
30
+
31
+ it "use hidden_field as helper for relational ids as default" do
32
+ attribute = ToyLocomotive::Attributes::AttributeChain.new :human_id, Dog
33
+ attribute.to_helper.should == :hidden_field
34
+ end
35
+
36
+ it "acepts new helper" do
37
+ subject.helper(:text_area).to_helper.should == :text_area
38
+ end
39
+
40
+ end
@@ -1,10 +1,10 @@
1
- require 'spec_helper'
2
-
3
- describe Dog do
4
-
5
- it "acepts attributes" do
6
- Dog.attribute(:title)
7
- Dog.toy_attributes.first.column.should == :title
8
- end
9
-
10
- end
1
+ require 'spec_helper'
2
+
3
+ describe Dog do
4
+
5
+ it "acepts attributes" do
6
+ Dog.attribute(:title)
7
+ Dog.toy_attributes.first.column.should == :title
8
+ end
9
+
10
+ end
@@ -1,39 +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
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