rails-dsl 0.6.0 → 0.7.0

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/rails-dsl/routes_ext.rb +95 -128
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98e3c0e380e01110c13f244d3c305baf997ea051
4
- data.tar.gz: a144f783e3fe53a04f41a6f2e692681541c0d165
3
+ metadata.gz: 04e1b3b946f8f2897ff8bc2be1fedc2b709bcfd7
4
+ data.tar.gz: 44fd71741007bc871a55801efd256d7239853d7b
5
5
  SHA512:
6
- metadata.gz: 8a1920286ea77abfb2e3dfe19b0b9db3f6448067cf37312e0f06f9f409c212890343d6c84b7ebe87cbe82f23c2d7964e24f2a3dfdd4170b585aa7657e1bfb901
7
- data.tar.gz: 4d548d8d1f6768bd4249274befaf4e040625694e666d9b13bb7dcce18daeba97ea31755b4dec2eb6634b749709128d45592cec1130e457619c6e24f4844dbc9e
6
+ metadata.gz: 026b66fb2b258b32855e9a88efd3039cecc73ccfb6a01d2ef96075bbdeb7f5ea76638c2b9b18e68df4515e23c63543c751cb4724b4562b00941e476ea8fe52e3
7
+ data.tar.gz: 678ac5e394393c751bf3a2349c39b0342ddebcef5c3cd69523555f81527eb2afb9bb9183ad0d4adc071413cf68a49afc4a9dd1ded3b20eafe54840a4c89dc4af
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -5,105 +5,83 @@ module Rails
5
5
  module Helpers
6
6
  class << self
7
7
 
8
- def array_to_url_params array,method_name
9
-
10
- return "" unless array.class <= ::Array
11
-
12
- var=nil
13
- array.each do |ary|
8
+ def process_opts opts={}
14
9
 
15
- if ary[0].to_s == method_name.to_s
16
- var = ary[1]
17
- break
18
- end
19
-
20
- end
21
-
22
- if !var.nil?
23
- return "/:#{var.join('/:')}"
24
- else
25
- return ""
26
- end
27
-
28
- end
29
-
30
- def process_args *args
31
-
32
- opts= args.select{|e|(e.class <= ::Hash)}.reduce( {}, :merge! )
10
+ opts = {class: opts} unless opts.class <= ::Hash
33
11
 
34
12
  # search for alt keys
35
13
  {
36
14
 
37
- urls: [:p,:paths,:url,:u],
38
- scope: [:s],
39
- resource: [:r,:class],
40
- defaults: [:d,:default],
41
- params: [:url_params],
42
- ne: [:exception,:exceptions,:ex],
15
+ urls: [:url,:path,{}],
16
+ defaults: [:default,{}],
17
+ ne: [:exception,:exceptions,:ex,[]],
43
18
 
44
19
  get: [:read],
45
20
  post: [:create],
46
21
  put: [:update],
47
- delete: [:delete]
22
+ delete: [:delete],
23
+
24
+ class: [:klass,:resource,:resources,:controller]
48
25
 
49
26
  }.each do |opts_sym,aliases|
50
27
  aliases.each do |alias_sym|
51
- opts[opts_sym] ||= opts.delete(alias_sym) || opts.delete(alias_sym.to_s)
28
+ opts[opts_sym] ||= alias_sym.class == ::Symbol ? opts.delete(alias_sym) || opts.delete(alias_sym.to_s) : alias_sym
29
+ end
30
+ if aliases.last.class != ::Symbol
31
+ raise(ArgumentError,"Invalid object given for #{opts_sym}, should be: #{aliases.last.class}") unless opts[opts_sym].class <= aliases.last.class
52
32
  end
53
33
  end
54
34
 
55
35
  # set defaults
56
- opts[:defaults] ||= {}
57
- opts[:resource] ||= args.select{|e|([::Class,::String,::Symbol].include?(e.class))}[0]
58
-
59
- opts[:params] ||= args.select{|e|(e.class <= ::Array)}
60
- opts[:params] ||= []
61
-
62
- opts[:urls] ||= {}
63
- opts[:ne] ||= []
64
-
65
36
  opts[:ne].map!{|method_name_to_sym| method_name_to_sym.to_s.to_sym }
66
-
67
37
  [:get,:post,:put,:delete,:options].each{|sym| opts[sym] ||= [] ; opts[sym]= [opts[sym]] unless opts[sym].class <= ::Array }
68
38
 
69
- unless opts[:params].class <= ::Array
70
- raise(ArgumentError,"invalid argument for url params: #{opts[:params]}.\nmust be something like [:method_name,[:url_params]] || [:method_name,:url_params]")
71
- end
39
+ opts[:short_class_name]= opts[:class].to_s.underscore.split('_')[0]
40
+ opts[:class] = if opts[:class].class == Class
41
+ opts[:class]
72
42
 
73
- unless opts[:urls].class <= ::Hash
74
- raise(ArgumentError,"invalid argument for urls group: #{opts[:urls]}.\nmust be something like {method_name: '/path'}")
75
- end
43
+ else
76
44
 
77
- unless opts[:params].empty?
45
+ if opts[:class].to_s.include?('_controller')
46
+ opts[:class].to_s.classify.constantize
78
47
 
79
- opts[:params].each{|ary| raise unless ary.size == 2 }
80
- opts[:params].each{|ary| ary[1]= [ary[1]] unless ary[1].class <= ::Array }
48
+ elsif opts[:class].to_s.include?('Controller')
49
+ opts[:class].to_s.constantize
81
50
 
82
- end
51
+ else
52
+ begin
53
+ opts[:class].to_s.concat('_controller').classify.constantize
54
+ rescue NameError
55
+ if opts[:class].to_s == opts[:class].to_s.downcase
56
+ opts[:class].to_s.classify.constantize
57
+
58
+ else
59
+ opts[:class].to_s.constantize
60
+
61
+ end
62
+ end
83
63
 
84
- # validations
85
- raise(ArgumentError,"Invalid defaults given") unless opts[:defaults].class <= ::Hash
64
+ end
65
+ end
86
66
 
87
- opts[:short_class_name]= opts[:resource].to_s.underscore.split('_')[0]
88
- opts[:class] = ( opts[:resource].class == Class ? opts[:resource] : opts[:resource].to_s.concat('_controller').classify.constantize )
89
- opts[:pim] = opts[:class].public_instance_methods(false).select{|e|(e.to_s.last != '?')} - opts[:ne]
67
+ opts[:pim] = opts[:class].public_instance_methods(false).select{|e| !(%W[ ? ! _ ].include?(e.to_s.last)) } - opts[:ne]
90
68
 
91
69
  # make setup able method configs
92
70
  opts[:pim].each do |sym|
93
71
 
94
72
  sym_str= sym.to_s
95
73
  {
96
- get: /_get$/,
97
- post: /_post$/,
98
- put: /_put$/,
99
- delete: /_delete$/
100
- }.each do |type,regex|
101
-
102
- if sym_str =~ regex
103
- opts[type].push(sym)
104
- opts[:urls][sym] ||= "/#{sym_str.gsub(regex,"")}"
74
+ get: [/_get$/,/^get_/],
75
+ post: [/_post$/,/^post_/],
76
+ put: [/_put$/,/^put_/],
77
+ delete: [/_delete$/,/^delete_/]
78
+ }.each do |type,regular_expressions|
79
+ regular_expressions.each do |regex|
80
+ if sym_str =~ regex
81
+ opts[type].push(sym)
82
+ opts[:urls][sym] ||= "/#{sym_str.gsub(regex,"")}"
83
+ end
105
84
  end
106
-
107
85
  end
108
86
 
109
87
  end
@@ -112,50 +90,69 @@ module Rails
112
90
 
113
91
  end
114
92
 
115
- end
116
- end
117
-
118
- def mount_controller *args
93
+ def array_to_url_params array,method_name
119
94
 
120
- opts= nil
121
- if args.size == 1 && args[0].class <= ::Hash
122
- opts= args[0]
123
- else
124
- opts= Rails::DSL::ActionDispatchRouteEXT::Helpers.process_args(*args)
125
- end
95
+ return "" unless array.class <= ::Array
126
96
 
127
- # helper lambdas
97
+ var=nil
98
+ array.each do |ary|
128
99
 
129
- create_mapping= lambda do
100
+ if ary[0].to_s == method_name.to_s
101
+ var = ary[1]
102
+ break
103
+ end
130
104
 
131
- opts[:pim].each do |method_name|
105
+ end
132
106
 
133
- method_to_use= nil
134
- [:get,:post,:put,:delete,:options].each do |pre_spec_method_call_type|
135
- if opts[pre_spec_method_call_type].include?(method_name)
136
- method_to_use ||= pre_spec_method_call_type
137
- end
107
+ if !var.nil?
108
+ return "/:#{var.join('/:')}"
109
+ else
110
+ return ""
138
111
  end
139
112
 
140
- method_to_use ||= :match
113
+ end
141
114
 
142
- url_path = opts[:urls][method_name].nil? ? "/#{method_name}" : opts[:urls][method_name].to_s
115
+ end
116
+ end
143
117
 
144
- self.__send__ method_to_use,
145
- "#{url_path}#{Rails::DSL::ActionDispatchRouteEXT::Helpers.array_to_url_params(opts[:params],method_name)}",
146
- {to: "#{opts[:short_class_name]}##{method_name}", defaults: opts[:defaults].dup }
147
-
148
- end
118
+ def mount_by(opts={})
119
+ opts = Helpers.process_opts(opts)
149
120
 
121
+ if opts[:class]
122
+ mount_controller opts
150
123
  end
151
124
 
152
- # make scope
153
- if !opts[:scope].nil? && opts[:scope].class <= String
154
- self.scope opts[:scope] do
155
- create_mapping.call
125
+ end
126
+
127
+ def mount_controller(opts={})
128
+
129
+ opts[:pim].each do |method_name|
130
+
131
+ # #> override methods if instance method variable detected, and will passed as input from params
132
+ # begin
133
+ # var= opts[:class].constantize.instance_method(method_name)
134
+ # unless var.parameters.select{|ary|(ary[0] ==:req)}.empty?
135
+ # define_method(method_name) do
136
+ # var.bind(self).call(*var.parameters.select{|ary|(ary[0] == :req)}.map{|ary| ary[1] }.map{ |param_key| params[param_key] })
137
+ # end
138
+ # end
139
+ # end
140
+
141
+ #> build path by method name
142
+ method_to_use= nil
143
+ [:get,:post,:put,:delete,:options].each do |pre_spec_method_call_type|
144
+ if opts[pre_spec_method_call_type].include?(method_name)
145
+ method_to_use ||= pre_spec_method_call_type
146
+ end
156
147
  end
157
- else
158
- create_mapping.call
148
+ method_to_use ||= :match
149
+
150
+ url_path = opts[:urls][method_name].nil? ? "/#{method_name}" : opts[:urls][method_name].to_s
151
+
152
+ self.__send__ method_to_use,
153
+ "#{url_path}#{Helpers.array_to_url_params(opts[:params],method_name)}",
154
+ {to: "#{opts[:short_class_name]}##{method_name}", defaults: opts[:defaults].dup }
155
+
159
156
  end
160
157
 
161
158
  return nil
@@ -172,25 +169,7 @@ module Rails
172
169
 
173
170
  opts[:pim].each do |sym|
174
171
 
175
- var= self.instance_method(sym)
176
- parameters= []
177
-
178
- unless var.parameters.select{|ary|(ary[0] ==:req)}.empty?
179
- parameters += var.parameters.select{|ary|(ary[0] ==:req)}.map{|ary| ary[1] }
180
- conv_params.push [ sym, parameters ]
181
- end
182
-
183
- define_method(sym) do
184
-
185
- value= var.bind(self).call(*parameters.map{ |param_key| params[param_key] })
186
172
 
187
- respond_to do |format|
188
- format.html
189
- format.json { render json: value }
190
- format.xml { render xml: value }
191
- end
192
-
193
- end
194
173
 
195
174
  end
196
175
 
@@ -204,19 +183,7 @@ module Rails
204
183
  end
205
184
  alias mount_rendered_controller mount_controller_with_render
206
185
 
207
- def get_controller_names *path
208
- path += ['app','controllers','*_controller.{ru,rb}'] if path.empty?
209
- return Dir.glob(::Rails.root.join(*path)).map{|p| p.split(File::Separator).last.split('_controller')[0].to_sym }
210
- end
211
-
212
- def mount_controllers
213
- get_controller_names.each { |cn| mount_controller(cn) }
214
- end
215
186
 
216
- def mount_controllers_with_render
217
- get_controller_names.each{ |cn| mount_controller_with_render(cn) }
218
- end
219
- alias mount_rendered_controllers mount_controllers_with_render
220
187
 
221
188
  end
222
189
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-07 00:00:00.000000000 Z
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler