Class: Fresco::Router
- Inherits:
-
Object
- Object
- Fresco::Router
- Defined in:
- lib/fresco/router.rb
Constant Summary collapse
- RESOURCE_ROUTES =
Rails-style CRUD expansion table. Verb / suffix / class suffix for each of the seven canonical resource actions.
[ [:index, "GET", "", "Index"], [:new, "GET", "/new", "New"], [:create, "POST", "", "Create"], [:show, "GET", "/:id", "Show"], [:edit, "GET", "/:id/edit", "Edit"], [:update, "PATCH", "/:id", "Update"], [:destroy, "DELETE", "/:id", "Destroy"], ].freeze
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Each entry: [verb, pattern, klass, constraints].
Instance Method Summary collapse
-
#initialize ⇒ Router
constructor
A new instance of Router.
-
#resources(name, only: nil, except: nil) ⇒ Object
Expand ‘resources :users` into the seven CRUD routes.
-
#root(to:) ⇒ Object
Sugar for ‘get “/”, to: …`.
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
18 19 20 |
# File 'lib/fresco/router.rb', line 18 def initialize @entries = [] end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Each entry: [verb, pattern, klass, constraints]. ‘fresco build` reads this and emits generated/dispatch.rb.
16 17 18 |
# File 'lib/fresco/router.rb', line 16 def entries @entries end |
Instance Method Details
#resources(name, only: nil, except: nil) ⇒ Object
Expand ‘resources :users` into the seven CRUD routes. `only:` / `except:` filter the set. Each generated route expects a matching action file under app/actions/ — missing files fail the build with a path-and-suggestion message rather than emitting a dispatch line that references a non-existent class. `resources :users` resolves to namespaced classes — `Users::Index` at `app/actions/users/index.rb`, not `UsersIndex` flat. Mixing `resources` with flat files isn’t supported on purpose: if you opt into the resources DSL, you opt into the folder convention.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/fresco/router.rb', line 42 def resources(name, only: nil, except: nil) base_path = "/#{name}" base_class = camelize(name) RESOURCE_ROUTES.each do |action, verb, suffix, class_suffix| next if only && !only.include?(action) next if except && except.include?(action) class_name = "#{base_class}::#{class_suffix}" file_path = "app/actions/#{Fresco.class_to_path(class_name)}.rb" unless File.exist?(file_path) raise "[build] resources :#{name} expects #{file_path} for :#{action} " \ "(add the file or use `only:`/`except:`)" end Fresco.const_set_path(class_name, Class.new) @entries << [verb, base_path + suffix, Fresco.const_get_path(class_name), {}] end end |
#root(to:) ⇒ Object
Sugar for ‘get “/”, to: …`. Mirrors Rails’ ‘root to: Foo`.
29 30 31 |
# File 'lib/fresco/router.rb', line 29 def root(to:) @entries << ["GET", "/", to, {}] end |