Module: Rails::DSL::ActionDispatchRouteEXT::Helpers

Defined in:
lib/rails-dsl/routes_ext.rb

Class Method Summary collapse

Class Method Details

.array_to_url_params(array, method_name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rails-dsl/routes_ext.rb', line 93

def array_to_url_params array,method_name

  return "" unless array.class <= ::Array

  var=nil
  array.each do |ary|

    if ary[0].to_s == method_name.to_s
      var = ary[1]
      break
    end

  end

  if !var.nil?
    return "/:#{var.join('/:')}"
  else
    return ""
  end

end

.process_opts(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails-dsl/routes_ext.rb', line 8

def process_opts opts={}

  opts = {class: opts} unless opts.class <= ::Hash

  # search for alt keys
  {

      urls:     [:url,:path,{}],
      defaults: [:default,{}],
      ne:       [:exception,:exceptions,:ex,[]],

      get:      [:read],
      post:     [:create],
      put:      [:update],
      delete:   [:delete],

      class:    [:klass,:resource,:resources,:controller]

  }.each do |opts_sym,aliases|
    aliases.each do |alias_sym|
      opts[opts_sym] ||= alias_sym.class == ::Symbol ? opts.delete(alias_sym) || opts.delete(alias_sym.to_s) : alias_sym
    end
    if aliases.last.class != ::Symbol
      raise(ArgumentError,"Invalid object given for #{opts_sym}, should be: #{aliases.last.class}") unless opts[opts_sym].class <= aliases.last.class
    end
  end

  # set defaults
  opts[:ne].map!{|method_name_to_sym| method_name_to_sym.to_s.to_sym }
  [:get,:post,:put,:delete,:options].each{|sym| opts[sym] ||= [] ; opts[sym]= [opts[sym]]  unless opts[sym].class <= ::Array }

  opts[:short_class_name]= opts[:class].to_s.underscore.split('_')[0]
  opts[:class] = if  opts[:class].class == Class
                   opts[:class]

                 else

                   if opts[:class].to_s.include?('_controller')
                     opts[:class].to_s.classify.constantize

                   elsif opts[:class].to_s.include?('Controller')
                     opts[:class].to_s.constantize

                   else
                     begin
                       opts[:class].to_s.concat('_controller').classify.constantize
                     rescue NameError
                       if opts[:class].to_s == opts[:class].to_s.downcase
                         opts[:class].to_s.classify.constantize

                       else
                         opts[:class].to_s.constantize

                       end
                     end

                   end
                 end

  opts[:pim] = opts[:class].public_instance_methods(false).select{|e| !(%W[ ? ! _ ].include?(e.to_s.last)) } - opts[:ne]

  # make setup able method configs
  opts[:pim].each do |sym|

    sym_str= sym.to_s
    {
        get:    [/_get$/,/^get_/],
        post:   [/_post$/,/^post_/],
        put:    [/_put$/,/^put_/],
        delete: [/_delete$/,/^delete_/]
    }.each do |type,regular_expressions|
      regular_expressions.each do |regex|
        if sym_str =~ regex
          opts[type].push(sym)
          opts[:urls][sym] ||= "/#{sym_str.gsub(regex,"")}"
        end
      end
    end

  end

  return opts

end