Module: NamedRoutes::Routes::Definition::ClassMethods

Defined in:
lib/named-routes/routes.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/named-routes/routes.rb', line 86

def method_missing(method_name, *args, &block)
  if instance.respond_to?(method_name)
    instance.send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#_defined_routesObject



50
51
52
# File 'lib/named-routes/routes.rb', line 50

def _defined_routes
  @_defined_routes ||= {}
end

#as_json(*args) ⇒ Object



82
83
84
# File 'lib/named-routes/routes.rb', line 82

def as_json(*args)
  instance.as_json(*args)
end

#defined_routesObject



43
44
45
46
47
48
# File 'lib/named-routes/routes.rb', line 43

def defined_routes
  (ancestors.reverse + [self]).inject({}) do |memo, klass|
    memo.merge!(klass._defined_routes) if klass.respond_to?(:_defined_routes)
    memo
  end
end

#eval(definition, params_arg = {}, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/named-routes/routes.rb', line 54

def eval(definition, params_arg={}, options={})
  full_definition = (options[:prefix] && prefix) ? File.join("", prefix, definition) : definition
  params = HashWithIndifferentAccess.new(params_arg)
  uri_string = if params.empty?
    full_definition
  else
    full_definition.split("/").map do |segment|
      segment_value = segment[/^:(.*)/, 1]
      segment_value_parts = segment_value.to_s.split(".")
      segment_name = segment_value_parts[0]
      if segment_name
        param_name = params.delete(File.basename(segment_name, '.*').to_s)
        URI.escape([param_name, *segment_value_parts[1..-1]].join("."))
      else
        segment
      end
    end.join("/")
  end
  unless params.empty?
    uri_string << "?#{params.to_param.gsub("%5B", "[").gsub("%5D", "]")}"
  end
  uri_string
end

#httpObject



23
24
25
# File 'lib/named-routes/routes.rb', line 23

def http
  SchemedUri.new(self, "http")
end

#httpsObject



27
28
29
# File 'lib/named-routes/routes.rb', line 27

def https
  SchemedUri.new(self, "https")
end

#instanceObject



19
20
21
# File 'lib/named-routes/routes.rb', line 19

def instance
  @instance ||= new
end

#normalize(uri) ⇒ Object



78
79
80
# File 'lib/named-routes/routes.rb', line 78

def normalize(uri)
  uri.gsub(Regexp.new("^#{File.join("", prefix.to_s)}"), "/").gsub("//", "/")
end

#route(name, definition, include_prefix = true) {|full_definition| ... } ⇒ Object Also known as: path, uri

Yields:

  • (full_definition)


31
32
33
34
35
36
37
38
39
# File 'lib/named-routes/routes.rb', line 31

def route(name, definition, include_prefix=true)
  full_definition = eval(definition, {}, :prefix => include_prefix)
  _defined_routes[name.to_s] = full_definition
  define_method name do |*args|
    self.class.eval(full_definition, [args.first].compact.first || {})
  end
  yield full_definition if block_given?
  full_definition
end