Class: Crystal::Router
Instance Attribute Summary collapse
-
#format_processor ⇒ Object
readonly
Returns the value of attribute format_processor.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #decode(path, params) ⇒ Object
- #dont_persist_params(&block) ⇒ Object
- #encode(klass, method, params = {}) ⇒ Object
- #global_persistent_params ⇒ Object
-
#initialize(class_variable, routes = [[:default_router, DefaultRouter.new]], format_processor = DefaultFormatProcessor.new) ⇒ Router
constructor
A new instance of Router.
- #persist_params(&block) ⇒ Object
- #persist_params? ⇒ Boolean
- #url_for(*args) ⇒ Object
Constructor Details
#initialize(class_variable, routes = [[:default_router, DefaultRouter.new]], format_processor = DefaultFormatProcessor.new) ⇒ Router
Returns a new instance of Router.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/crystal/router/router.rb', line 11 def initialize class_variable, routes = [[:default_router, DefaultRouter.new]], format_processor = DefaultFormatProcessor.new @class_variable, @format_processor = class_variable, format_processor @routes = Dictionary.new routes.each do |k, v| k.must_be.present v.must_be.present v.must.respond_to(:decode) v.must.respond_to(:encode) @routes[k] = v end end |
Instance Attribute Details
#format_processor ⇒ Object (readonly)
Returns the value of attribute format_processor.
9 10 11 |
# File 'lib/crystal/router/router.rb', line 9 def format_processor @format_processor end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
9 10 11 |
# File 'lib/crystal/router/router.rb', line 9 def routes @routes end |
Instance Method Details
#decode(path, params) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/crystal/router/router.rb', line 43 def decode path, params path.first.must == '/' params = params.to_openobject path, format = format_processor.remove_format path if format_processor result = nil routes.each do |name, route| result = route.decode path, params break if result end raise "No route for '#{workspace.path}' request!" unless result klass, method, params = result method ||= config.default_method params.must_be.defined raise "Invalid route! No method '#{method}' for #{klass}!" unless klass.instance_methods.include? method raise "Invalid route! You try to call protected method '#{method}' for '#{klass}'!" unless klass.public_instance_methods.include? method return klass, method, params end |
#dont_persist_params(&block) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/crystal/router/router.rb', line 103 def dont_persist_params &block before = Thread.current[:persist_params] begin Thread.current[:persist_params] = false block.call ensure Thread.current[:persist_params] = before end end |
#encode(klass, method, params = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/crystal/router/router.rb', line 67 def encode klass, method, params = {} klass.must_be.defined method, params = method.to_s, params.to_openobject format = params.delete :format raise "Invalid route! No method '#{method}' for #{klass}!" unless klass.instance_methods.include? method raise "Invalid route! You try to call protected method '#{method}' for '#{klass}'!" unless klass.public_instance_methods.include? method result = nil routes.each do |name, route| result = route.encode klass, method, params break if result end raise "No route for '#{klass}.#{method}'!" unless result path, params = result path.must_be.defined params.must_be.defined path = format_processor.add_format path, format if format_processor and format return path, params end |
#global_persistent_params ⇒ Object
117 |
# File 'lib/crystal/router/router.rb', line 117 def global_persistent_params; @global_persistent_params ||= [] end |
#persist_params(&block) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/crystal/router/router.rb', line 93 def persist_params &block before = Thread.current[:persist_params] begin Thread.current[:persist_params] = true block.call ensure Thread.current[:persist_params] = before end end |
#persist_params? ⇒ Boolean
113 114 115 |
# File 'lib/crystal/router/router.rb', line 113 def persist_params? !!Thread.current[:persist_params] end |
#url_for(*args) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/crystal/router/router.rb', line 24 def url_for *args if args.first.is_a?(Class) args.size.must_be.in 2..3 klass, method, = args url_for_class(klass, method, ( || {})) else first = args.first.to_s if first !~ /^\// args.size.must_be.in 1..2 method, = args url_for_class(nil, method, ( || {})) else args.size.must_be.in 1..2 path, = args url_for_path(path, ( || {})) end end end |