Module: Jets::Router::Dsl
- Included in:
- Jets::Router
- Defined in:
- lib/jets/router/dsl.rb
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
Instance Method Details
#collection ⇒ Object
114 115 116 117 118 |
# File 'lib/jets/router/dsl.rb', line 114 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.
123 124 125 126 |
# File 'lib/jets/router/dsl.rb', line 123 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
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jets/router/dsl.rb', line 94 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jets/router/dsl.rb', line 67 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
108 109 110 111 112 |
# File 'lib/jets/router/dsl.rb', line 108 def member @on_option = :member yield @on_option = nil end |
#namespace(ns, &block) ⇒ Object
10 11 12 |
# File 'lib/jets/router/dsl.rb', line 10 def namespace(ns, &block) scope(module: ns, prefix: ns, as: ns, from: :namespace, &block) end |
#prefix(prefix, &block) ⇒ Object
14 15 16 |
# File 'lib/jets/router/dsl.rb', line 14 def prefix(prefix, &block) scope(prefix: prefix, &block) end |
#resource(*items, **options) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jets/router/dsl.rb', line 83 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
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/jets/router/dsl.rb', line 40 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”
129 130 131 132 133 134 |
# File 'lib/jets/router/dsl.rb', line 129 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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jets/router/dsl.rb', line 23 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jets/router/dsl.rb', line 51 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, # module: options.delete(:module) || item, # NOTE: resources does not automatically set module, but namespace does } end |