rails-dsl 0.3.3 → 0.4.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.
- checksums.yaml +4 -4
- data/README.md +62 -1
- data/VERSION +1 -1
- data/lib/rails-dsl/routes_ext.rb +125 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e002eeafc2ccfe491ccb4c9f159e88086a35a510
|
4
|
+
data.tar.gz: 1d972d46a8325b825d544781d2ef61b232b55408
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1628d1b0e72ddd6714c076e3b4eeb181d4a89d7490a0ce1736b2df6b1cb6d3b452c9284f6ae4787713ce5015d1a901258369121028ad2cbd5170d53e40c5faf6
|
7
|
+
data.tar.gz: b5c0ca8fef074376e9903610c36e2d2ab6efc1403d282e37e8fd1f21bf7ead550015cb3f2523bd02643f3a321f1fee1c40f7db7aa732352a332c1284eef7f818
|
data/README.md
CHANGED
@@ -21,7 +21,9 @@ if you call rails with 'kill' / 'k' command from now on, it will kill the applic
|
|
21
21
|
|
22
22
|
### Routing
|
23
23
|
|
24
|
-
####
|
24
|
+
#### Mount controller
|
25
|
+
|
26
|
+
##### As pages
|
25
27
|
|
26
28
|
* mount a controller public methods as routes.
|
27
29
|
* the method name will be the path
|
@@ -61,4 +63,63 @@ you can use the following options (name: [aliases])
|
|
61
63
|
|
62
64
|
#> mount not private methods from PagesController
|
63
65
|
|
66
|
+
```
|
67
|
+
|
68
|
+
##### As API behavor
|
69
|
+
|
70
|
+
in this mount options
|
71
|
+
|
72
|
+
* the methods return value will be sent back parsed object to the requester
|
73
|
+
* you dont have to set up render options
|
74
|
+
|
75
|
+
* arguments will be parsed into route :params so the othere side can cache based on url
|
76
|
+
* the method actualy receive the passed parameter so you can use like in the example
|
77
|
+
|
78
|
+
* by extend the method name you can set the methods REST method by the followind endings:
|
79
|
+
* _get
|
80
|
+
* _post
|
81
|
+
* _put
|
82
|
+
* _delete
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
|
86
|
+
|
87
|
+
#> controller
|
88
|
+
class PagesController < ApplicationController
|
89
|
+
|
90
|
+
#> this generate /test1/:hello.:format path
|
91
|
+
def test1 hello
|
92
|
+
{hello: hello }
|
93
|
+
end
|
94
|
+
|
95
|
+
#> this generate /test2/:hello.:format path
|
96
|
+
def test2
|
97
|
+
{hello: 'hello' }
|
98
|
+
end
|
99
|
+
|
100
|
+
def test2_post
|
101
|
+
{hello: 'blabla' }
|
102
|
+
end
|
103
|
+
#> POST /test.:format -> default json
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
#> routes.rb
|
108
|
+
|
109
|
+
HeartShoot::Application.routes.draw do
|
110
|
+
|
111
|
+
# you can still use the Class name too
|
112
|
+
mount_controller_as_api :pages
|
113
|
+
|
114
|
+
# or as alias
|
115
|
+
mount_api :pages
|
116
|
+
|
117
|
+
# or you can use well defaults key to set up parameters
|
118
|
+
# if you prefer the XML as default over JSON output
|
119
|
+
mount_api :pages, defaults: { format: :xml }
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
#> mount not private methods as apit from PagesController
|
124
|
+
|
64
125
|
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.1
|
data/lib/rails-dsl/routes_ext.rb
CHANGED
@@ -6,6 +6,28 @@ module Rails
|
|
6
6
|
module Helpers
|
7
7
|
class << self
|
8
8
|
|
9
|
+
def array_to_url_params array,method_name
|
10
|
+
|
11
|
+
return "" unless array.class <= ::Array
|
12
|
+
|
13
|
+
var=nil
|
14
|
+
array.each do |ary|
|
15
|
+
|
16
|
+
if ary[0].to_s == method_name.to_s
|
17
|
+
var = ary[1]
|
18
|
+
break
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
if !var.nil?
|
24
|
+
return "/:#{var.join('/:')}"
|
25
|
+
else
|
26
|
+
return ""
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
9
31
|
def process_args *args
|
10
32
|
|
11
33
|
opts= args.select{|e|(e.class <= ::Hash)}.reduce( {}, :merge! )
|
@@ -13,9 +35,12 @@ module Rails
|
|
13
35
|
# search for alt keys
|
14
36
|
{
|
15
37
|
|
16
|
-
|
38
|
+
urls: [:p,:paths,:url,:u],
|
39
|
+
scope: [:s],
|
17
40
|
resource: [:r,:class],
|
18
41
|
defaults: [:d,:default],
|
42
|
+
params: [:url_params],
|
43
|
+
ne: [:exception,:exceptions,:ex],
|
19
44
|
|
20
45
|
get: [:read],
|
21
46
|
post: [:create],
|
@@ -32,14 +57,57 @@ module Rails
|
|
32
57
|
opts[:defaults] ||= {}
|
33
58
|
opts[:resource] ||= args.select{|e|([::Class,::String,::Symbol].include?(e.class))}[0]
|
34
59
|
|
60
|
+
opts[:params] ||= args.select{|e|(e.class <= ::Array)}
|
61
|
+
opts[:params] ||= []
|
62
|
+
|
63
|
+
opts[:urls] ||= {}
|
64
|
+
opts[:ne] ||= []
|
65
|
+
|
66
|
+
opts[:ne].map!{|method_name_to_sym| method_name_to_sym.to_s.to_sym }
|
67
|
+
|
35
68
|
[:get,:post,:put,:delete,:options].each{|sym| opts[sym] ||= [] ; opts[sym]= [opts[sym]] unless opts[sym].class <= ::Array }
|
36
69
|
|
70
|
+
unless opts[:params].class <= ::Array
|
71
|
+
raise(ArgumentError,"invalid argument for url params: #{opts[:params]}.\nmust be something like [:method_name,[:url_params]] || [:method_name,:url_params]")
|
72
|
+
end
|
73
|
+
|
74
|
+
unless opts[:urls].class <= ::Hash
|
75
|
+
raise(ArgumentError,"invalid argument for urls group: #{opts[:urls]}.\nmust be something like {method_name: '/path'}")
|
76
|
+
end
|
77
|
+
|
78
|
+
unless opts[:params].empty?
|
79
|
+
|
80
|
+
opts[:params].each{|ary| raise unless ary.size == 2 }
|
81
|
+
opts[:params].each{|ary| ary[1]= [ary[1]] unless ary[1].class <= ::Array }
|
82
|
+
|
83
|
+
end
|
84
|
+
|
37
85
|
# validations
|
38
86
|
raise(ArgumentError,"Invalid defaults given") unless opts[:defaults].class <= ::Hash
|
39
87
|
|
40
88
|
opts[:short_class_name]= opts[:resource].to_s.underscore.split('_')[0]
|
41
|
-
opts[:class]
|
89
|
+
opts[:class] = ( opts[:resource].class == Class ? opts[:resource] : opts[:resource].to_s.concat('_controller').classify.constantize )
|
90
|
+
opts[:pim] = opts[:class].public_instance_methods(false).select{|e|(e.to_s.last != '?')} - opts[:ne]
|
91
|
+
|
92
|
+
# make setup able method configs
|
93
|
+
opts[:pim].each do |sym|
|
94
|
+
|
95
|
+
sym_str= sym.to_s
|
96
|
+
{
|
97
|
+
get: /_get$/,
|
98
|
+
post: /_post$/,
|
99
|
+
put: /_put$/,
|
100
|
+
delete: /_delete$/
|
101
|
+
}.each do |type,regex|
|
102
|
+
|
103
|
+
if sym_str =~ regex
|
104
|
+
opts[type].push(sym)
|
105
|
+
opts[:urls][sym] ||= "/#{sym_str.gsub(regex,"")}"
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
42
109
|
|
110
|
+
end
|
43
111
|
|
44
112
|
return opts
|
45
113
|
|
@@ -51,13 +119,18 @@ module Rails
|
|
51
119
|
|
52
120
|
def mount_controller *args
|
53
121
|
|
54
|
-
opts=
|
122
|
+
opts= nil
|
123
|
+
if args.size == 1 && args[0].class <= ::Hash
|
124
|
+
opts= args[0]
|
125
|
+
else
|
126
|
+
opts= Rails::DSL::ActionDispatchRouteEXT::Helpers.process_args(*args)
|
127
|
+
end
|
55
128
|
|
56
129
|
# helper lambdas
|
57
130
|
|
58
131
|
create_mapping= lambda do
|
59
132
|
|
60
|
-
opts[:
|
133
|
+
opts[:pim].each do |method_name|
|
61
134
|
|
62
135
|
method_to_use= nil
|
63
136
|
[:get,:post,:put,:delete,:options].each do |pre_spec_method_call_type|
|
@@ -67,7 +140,11 @@ module Rails
|
|
67
140
|
end
|
68
141
|
method_to_use ||= :get
|
69
142
|
|
70
|
-
|
143
|
+
url_path = opts[:urls][method_name].nil? ? "/#{method_name}" : opts[:urls][method_name].to_s
|
144
|
+
|
145
|
+
self.__send__ method_to_use,
|
146
|
+
"#{url_path}#{Rails::DSL::ActionDispatchRouteEXT::Helpers.array_to_url_params(opts[:params],method_name)}",
|
147
|
+
{to: "#{opts[:short_class_name]}##{method_name}", defaults: opts[:defaults].dup }
|
71
148
|
|
72
149
|
end
|
73
150
|
|
@@ -82,20 +159,51 @@ module Rails
|
|
82
159
|
create_mapping.call
|
83
160
|
end
|
84
161
|
|
162
|
+
return nil
|
163
|
+
|
85
164
|
end
|
86
165
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
166
|
+
def mount_controller_as_api *args
|
167
|
+
|
168
|
+
opts= Rails::DSL::ActionDispatchRouteEXT::Helpers.process_args(*args)
|
169
|
+
conv_params= []
|
170
|
+
|
171
|
+
# make last value return as value object rendered as the specific format
|
172
|
+
opts[:class].class_eval do
|
173
|
+
|
174
|
+
opts[:pim].each do |sym|
|
175
|
+
|
176
|
+
var= self.instance_method(sym)
|
177
|
+
parameters= []
|
178
|
+
|
179
|
+
unless var.parameters.select{|ary|(ary[0] ==:req)}.empty?
|
180
|
+
parameters += var.parameters.select{|ary|(ary[0] ==:req)}.map{|ary| ary[1] }
|
181
|
+
conv_params.push [ sym, parameters ]
|
182
|
+
end
|
183
|
+
|
184
|
+
define_method(sym) do
|
185
|
+
|
186
|
+
value= var.bind(self).call(*parameters.map{ |param_key| params[param_key] })
|
187
|
+
|
188
|
+
respond_to do |format|
|
189
|
+
format.html
|
190
|
+
format.json { render json: value }
|
191
|
+
format.xml { render xml: value }
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
# mount controller methods
|
201
|
+
mount_controller *args,*conv_params, { defaults: {format: :json} }
|
202
|
+
|
203
|
+
return nil
|
204
|
+
|
205
|
+
end
|
206
|
+
alias mount_api mount_controller_as_api
|
99
207
|
|
100
208
|
end
|
101
209
|
end
|