Module: Jets::Router::Dsl
- Includes:
- Mount
- Included in:
- Jets::Router
- Defined in:
- lib/jets/router/dsl.rb,
lib/jets/router/dsl/mount.rb
Defined Under Namespace
Modules: Mount
Instance Method Summary collapse
- #collection ⇒ Object
-
#default_param(has_block, name, options) ⇒ Object
If a block has pass then we assume the resources will be nested and then prefix the param name with the resource.
- #each_resource(name, options = {}, has_block = nil) ⇒ Object
- #each_resources(name, options = {}, has_block = nil) ⇒ Object
- #member ⇒ Object
- #namespace(ns, &block) ⇒ Object
- #prefix(prefix, &block) ⇒ Object
- #resource(*items, **options) ⇒ Object
-
#resources(*items, **options) ⇒ Object
resources macro expands to all the routes.
-
#root(to, options = {}) ⇒ Object
root “posts#index”.
-
#scope(args) ⇒ Object
scope supports three options: module, prefix and as.
- #scope_options!(item, options) ⇒ Object
Methods included from Mount
Instance Method Details
#collection ⇒ Object
117 118 119 120 121 |
# File 'lib/jets/router/dsl.rb', line 117 def collection @on_option = :collection yield @on_option = nil end |
#default_param(has_block, name, options) ⇒ Object
If a block has pass then we assume the resources will be nested and then prefix the param name with the resource. IE: post_id instead of id This avoids an API Gateway parent sibling variable collision.
126 127 128 129 |
# File 'lib/jets/router/dsl.rb', line 126 def default_param(has_block, name, ) default_param = has_block ? "#{name.to_s.singularize}_id".to_sym : :id [:param] || default_param end |
#each_resource(name, options = {}, has_block = nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/jets/router/dsl.rb', line 97 def each_resource(name, ={}, has_block=nil) o = Resources::Options.new(name, .merge(singular_resource: true)) f = Resources::Filter.new(name, ) get "#{name}/new", o.build(:new) if f.yes?(:new) && !api_mode? get name, o.build(:show) if f.yes?(:show) post name, o.build(:create) if f.yes?(:create) get "#{name}/edit", o.build(:edit) if f.yes?(:edit) && !api_mode? put name, o.build(:update) if f.yes?(:update) post name, o.build(:update) if f.yes?(:update) # for binary uploads patch name, o.build(:update) if f.yes?(:update) delete name, o.build(:delete) if f.yes?(:delete) end |
#each_resources(name, options = {}, has_block = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jets/router/dsl.rb', line 70 def each_resources(name, ={}, has_block=nil) o = Resources::Options.new(name, ) f = Resources::Filter.new(name, ) param = default_param(has_block, name, ) get name, o.build(:index) if f.yes?(:index) get "#{name}/new", o.build(:new) if f.yes?(:new) && !api_mode? get "#{name}/:#{param}", o.build(:show) if f.yes?(:show) post name, o.build(:create) if f.yes?(:create) get "#{name}/:#{param}/edit", o.build(:edit) if f.yes?(:edit) && !api_mode? put "#{name}/:#{param}", o.build(:update) if f.yes?(:update) post "#{name}/:#{param}", o.build(:update) if f.yes?(:update) # for binary uploads patch "#{name}/:#{param}", o.build(:update) if f.yes?(:update) delete "#{name}/:#{param}", o.build(:delete) if f.yes?(:delete) end |
#member ⇒ Object
111 112 113 114 115 |
# File 'lib/jets/router/dsl.rb', line 111 def member @on_option = :member yield @on_option = nil end |
#namespace(ns, &block) ⇒ Object
12 13 14 |
# File 'lib/jets/router/dsl.rb', line 12 def namespace(ns, &block) scope(module: ns, prefix: ns, as: ns, from: :namespace, &block) end |
#prefix(prefix, &block) ⇒ Object
16 17 18 |
# File 'lib/jets/router/dsl.rb', line 16 def prefix(prefix, &block) scope(prefix: prefix, &block) end |
#resource(*items, **options) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/jets/router/dsl.rb', line 86 def resource(*items, **) items.each do |item| = (item, ) [:from] = :resource # flag for MethodCreator logic: to handle method_name_leaf and more scope() do each_resource(item, , block_given?) yield if block_given? end end end |
#resources(*items, **options) ⇒ Object
resources macro expands to all the routes
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jets/router/dsl.rb', line 42 def resources(*items, **) items.each do |item| = (item, ) [:from] = :resources # flag for MethodCreator logic: to handle method_name_leaf and more scope() do each_resources(item, , block_given?) yield if block_given? end end end |
#root(to, options = {}) ⇒ Object
root “posts#index”
132 133 134 135 136 137 |
# File 'lib/jets/router/dsl.rb', line 132 def root(to, ={}) default = {path: '', to: to, method: :get, root: true} = default.merge() MethodCreator.new(, @scope).create_root_helper @routes << Route.new(, @scope) end |
#scope(args) ⇒ Object
scope supports three options: module, prefix and as. Jets vs Rails:
module - module
prefix - path
as - as
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jets/router/dsl.rb', line 25 def scope(args) # normalizes `scope(:admin)` as `scope(prefix: :admin)` = case args when Hash args when String, Symbol { prefix: args } end root_level = @scope.nil? @scope = root_level ? Scope.new() : @scope.new() yield ensure @scope = @scope.parent if @scope end |
#scope_options!(item, options) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jets/router/dsl.rb', line 53 def (item, ) prefix = if [:prefix] # prefix given from the resources macro get automatically prepended to the item name p = .delete(:prefix) "#{p}/#{item}" else item end { as: .delete(:as) || item, # delete as or it messes with create_route prefix: prefix, param: [:param], # module: options.delete(:module) || item, # NOTE: resources does not automatically set module, but namespace does } end |