Class: RubyRoutes::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_routes/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Router

Returns a new instance of Router.



5
6
7
8
9
10
# File 'lib/ruby_routes/router.rb', line 5

def initialize(&block)
  @route_set = RouteSet.new
  @scope_stack = []
  @concerns = {}
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#route_setObject (readonly)

Returns the value of attribute route_set.



3
4
5
# File 'lib/ruby_routes/router.rb', line 3

def route_set
  @route_set
end

Instance Method Details

#concern(name, &block) ⇒ Object



134
135
136
# File 'lib/ruby_routes/router.rb', line 134

def concern(name, &block)
  @concerns[name] = block
end

#concerns(*names, &block) ⇒ Object

Concerns (reusable route groups)



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby_routes/router.rb', line 121

def concerns(*names, &block)
  names.each do |name|
    concern = @concerns[name]
    raise "Concern '#{name}' not found" unless concern

    instance_eval(&concern)
  end

  if block_given?
    instance_eval(&block)
  end
end

#constraints(constraints = {}, &block) ⇒ Object

Route constraints



139
140
141
142
143
144
145
146
147
# File 'lib/ruby_routes/router.rb', line 139

def constraints(constraints = {}, &block)
  @scope_stack.push({ constraints: constraints })

  if block_given?
    instance_eval(&block)
  end

  @scope_stack.pop
end

#defaults(defaults = {}, &block) ⇒ Object

Defaults



150
151
152
153
154
155
156
157
158
# File 'lib/ruby_routes/router.rb', line 150

def defaults(defaults = {}, &block)
  @scope_stack.push({ defaults: defaults })

  if block_given?
    instance_eval(&block)
  end

  @scope_stack.pop
end

#delete(path, options = {}) ⇒ Object



29
30
31
# File 'lib/ruby_routes/router.rb', line 29

def delete(path, options = {})
  add_route(path, options.merge(via: :delete))
end

#get(path, options = {}) ⇒ Object

Basic route definition



13
14
15
# File 'lib/ruby_routes/router.rb', line 13

def get(path, options = {})
  add_route(path, options.merge(via: :get))
end

#match(path, options = {}) ⇒ Object



33
34
35
# File 'lib/ruby_routes/router.rb', line 33

def match(path, options = {})
  add_route(path, options)
end

#mount(app, at: nil) ⇒ Object

Mount other applications



161
162
163
164
# File 'lib/ruby_routes/router.rb', line 161

def mount(app, at: nil)
  path = at || "/#{app}"
  add_route("#{path}/*path", to: app, via: :all)
end

#namespace(name, options = {}, &block) ⇒ Object

Namespace support



94
95
96
97
98
99
100
101
102
# File 'lib/ruby_routes/router.rb', line 94

def namespace(name, options = {}, &block)
  @scope_stack.push({ path: "/#{name}", module: name })

  if block_given?
    instance_eval(&block)
  end

  @scope_stack.pop
end

#patch(path, options = {}) ⇒ Object



25
26
27
# File 'lib/ruby_routes/router.rb', line 25

def patch(path, options = {})
  add_route(path, options.merge(via: :patch))
end

#post(path, options = {}) ⇒ Object



17
18
19
# File 'lib/ruby_routes/router.rb', line 17

def post(path, options = {})
  add_route(path, options.merge(via: :post))
end

#put(path, options = {}) ⇒ Object



21
22
23
# File 'lib/ruby_routes/router.rb', line 21

def put(path, options = {})
  add_route(path, options.merge(via: :put))
end

#resource(name, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_routes/router.rb', line 81

def resource(name, options = {})
  singular = name.to_s.singularize

  get "/#{singular}", options.merge(to: "#{singular}#show")
  get "/#{singular}/new", options.merge(to: "#{singular}#new")
  post "/#{singular}", options.merge(to: "#{singular}#create")
  get "/#{singular}/edit", options.merge(to: "#{singular}#edit")
  put "/#{singular}", options.merge(to: "#{singular}#update")
  patch "/#{singular}", options.merge(to: "#{singular}#update")
  delete "/#{singular}", options.merge(to: "#{singular}#destroy")
end

#resources(name, options = {}, &block) ⇒ Object

Resources routing (Rails-like)



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
# File 'lib/ruby_routes/router.rb', line 38

def resources(name, options = {}, &block)
  singular = name.to_s.singularize
  plural = (options[:path] || name.to_s.pluralize)
  controller = options[:controller] || plural

  # Collection routes
  get "/#{plural}", options.merge(to: "#{controller}#index")
  get "/#{plural}/new", options.merge(to: "#{controller}#new")
  post "/#{plural}", options.merge(to: "#{controller}#create")

  # Member routes
  get "/#{plural}/:id", options.merge(to: "#{controller}#show")
  get "/#{plural}/:id/edit", options.merge(to: "#{controller}#edit")
  put "/#{plural}/:id", options.merge(to: "#{controller}#update")
  patch "/#{plural}/:id", options.merge(to: "#{controller}#update")
  delete "/#{plural}/:id", options.merge(to: "#{controller}#destroy")

  # Nested resources if specified
  if options[:nested]
    nested_name = options[:nested]
    nested_singular = nested_name.to_s.singularize
    nested_plural = nested_name.to_s.pluralize

    get "/#{plural}/:id/#{nested_plural}", options.merge(to: "#{nested_plural}#index")
    get "/#{plural}/:id/#{nested_plural}/new", options.merge(to: "#{nested_plural}#new")
    post "/#{plural}/:id/#{nested_plural}", options.merge(to: "#{nested_plural}#create")
    get "/#{plural}/:id/#{nested_plural}/:nested_id", options.merge(to: "#{nested_plural}#show")
    get "/#{plural}/:id/#{nested_plural}/:nested_id/edit", options.merge(to: "#{nested_plural}#edit")
    put "/#{plural}/:id/#{nested_plural}/:nested_id", options.merge(to: "#{nested_plural}#update")
    patch "/#{plural}/:id/#{nested_plural}/:nested_id", options.merge(to: "#{nested_plural}#update")
    delete "/#{plural}/:id/#{nested_plural}/:nested_id", options.merge(to: "#{nested_plural}#destroy")
  end

  # Handle concerns if block is given
  if block_given?
    # Push a scope for nested resources
    @scope_stack.push({ path: "/#{plural}/:id" })
    # Execute the block in the context of this router instance
    instance_eval(&block)
    @scope_stack.pop
  end
end

#root(options = {}) ⇒ Object

Root route



116
117
118
# File 'lib/ruby_routes/router.rb', line 116

def root(options = {})
  add_route("/", options.merge(via: :get))
end

#scope(options = {}, &block) ⇒ Object

Scope support



105
106
107
108
109
110
111
112
113
# File 'lib/ruby_routes/router.rb', line 105

def scope(options = {}, &block)
  @scope_stack.push(options)

  if block_given?
    instance_eval(&block)
  end

  @scope_stack.pop
end