Class: Sinatra::Browse::Route
- Inherits:
-
Object
- Object
- Sinatra::Browse::Route
- Defined in:
- lib/sinatra/browse/route.rb
Defined Under Namespace
Classes: ValidationError
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#match ⇒ Object
readonly
Returns the value of attribute match.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Class Method Summary collapse
-
.build_name(request_method, path_info) ⇒ Object
This is here because we’re using the name as the keys for the _browse_routes hash.
Instance Method Summary collapse
- #coerce_type(params) ⇒ Object
- #delete_undefined(params, system_params) ⇒ Object
- #has_parameter?(parameter) ⇒ Boolean
-
#initialize(request_method, path_info, description, parameters = nil) ⇒ Route
constructor
A new instance of Route.
- #matches?(request_method, path_info) ⇒ Boolean
- #set_defaults(params) ⇒ Object
- #to_hash ⇒ Object
- #transform(params) ⇒ Object
- #validate(params) ⇒ Object
Constructor Details
#initialize(request_method, path_info, description, parameters = nil) ⇒ Route
Returns a new instance of Route.
18 19 20 21 22 23 |
# File 'lib/sinatra/browse/route.rb', line 18 def initialize(request_method, path_info, description, parameters = nil) @name = build_name(request_method, path_info) @match = build_match(request_method, path_info) @description = description @parameters = parameters || {} end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
8 9 10 |
# File 'lib/sinatra/browse/route.rb', line 8 def description @description end |
#match ⇒ Object (readonly)
Returns the value of attribute match.
7 8 9 |
# File 'lib/sinatra/browse/route.rb', line 7 def match @match end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/sinatra/browse/route.rb', line 6 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
5 6 7 |
# File 'lib/sinatra/browse/route.rb', line 5 def parameters @parameters end |
Class Method Details
.build_name(request_method, path_info) ⇒ Object
This is here because we’re using the name as the keys for the _browse_routes hash. We want to build it outside of this class for that.
14 15 16 |
# File 'lib/sinatra/browse/route.rb', line 14 def self.build_name(request_method, path_info) "#{request_method} #{path_info}" end |
Instance Method Details
#coerce_type(params) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sinatra/browse/route.rb', line 37 def coerce_type(params) @parameters.each { |k,v| params[k] &&= case v[:type] when :Boolean cast_to_boolean(params[k]) else send(v[:type], params[k]) end } end |
#delete_undefined(params, system_params) ⇒ Object
56 57 58 |
# File 'lib/sinatra/browse/route.rb', line 56 def delete_undefined(params, system_params) params.delete_if { |i| !(self.has_parameter?(i) || system_params.member?(i)) } end |
#has_parameter?(parameter) ⇒ Boolean
33 34 35 |
# File 'lib/sinatra/browse/route.rb', line 33 def has_parameter?(parameter) @parameters.has_key?(parameter.to_sym) end |
#matches?(request_method, path_info) ⇒ Boolean
29 30 31 |
# File 'lib/sinatra/browse/route.rb', line 29 def matches?(request_method, path_info) !! (build_name(request_method,path_info) =~ @match) end |
#set_defaults(params) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/sinatra/browse/route.rb', line 48 def set_defaults(params) @parameters.each { |k,v| unless params[k] || v[:default].nil? params[k] = v[:default].is_a?(Proc) ? v[:default].call(params[k]) : v[:default] end } end |
#to_hash ⇒ Object
25 26 27 |
# File 'lib/sinatra/browse/route.rb', line 25 def to_hash {name: @name, description: @description}.merge @parameters end |
#transform(params) ⇒ Object
73 74 75 76 77 |
# File 'lib/sinatra/browse/route.rb', line 73 def transform(params) @parameters.each { |k,v| params[k] = v[:transform].to_proc.call(params[k]) if params[k] && v[:transform] } end |
#validate(params) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sinatra/browse/route.rb', line 60 def validate(params) @parameters.each { |k,v| return fail_validation v, :required if !params[k] && v[:required] if params[k] return fail_validation v, :depends_on if v[:depends_on] && !params[v[:depends_on]] return fail_validation v, :in if v[:in] && !v[:in].member?(params[k]) return fail_validation v, :format if v[:type] == :String && v[:format] && !(params[k] =~ v[:format]) end } {success: true} end |