class Sinatra::Browse::ParameterType
Attributes
default[R]
description[R]
name[R]
validators[R]
Public Class Methods
inherited(subclass)
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 9 def self.inherited(subclass) subclass.class_eval do # Initialize the validator hash @validator_declarations ||= {} # # Global validators # validator(:in) { |possible_values| possible_values.member?(@value) } # We need a to_s here because the user should be allowed to define dependencies # using symbols while the actual keys of the params hash are strings validator(:depends_on) { |dep| @params.has_key?(dep.to_s) } end end
new(name, map)
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 25 def initialize(name, map) @name = name @default = map.delete(:default) @description = map.delete(:description) || map.delete(:desc) @transform = map.delete(:transform) @transform = @transform.to_proc if @transform @required = !! map[:required] @on_error = map.delete(:on_error) @validators = [] map.each do |key, value| if val_blk = validator_declarations[key] @validators << Validator.new( name: key, criteria: map[key], validation_blk: val_blk ) end end end
validator(name, &blk)
click to toggle source
DSL
# File lib/sinatra/browse/parameter_type.rb, line 117 def self.validator(name, &blk) @validator_declarations[name] = blk end
validator_declarations()
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 121 def self.validator_declarations @validator_declarations end
Public Instance Methods
build_error_hash(reason, value)
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 79 def build_error_hash(reason, value) { reason: reason, parameter: self.name, value: value, on_error: @on_error } end
coerce(value)
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 75 def coerce(value) raise NotImplementedError end
default_set?()
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 55 def default_set? ! @default.nil? end
required?()
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 59 def required? @required end
to_hash(options = {})
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 93 def to_hash(options = {}) h = { name: @name, type: type, required: required?, } if @default h[:default] = if @default.is_a?(Proc) && options[:noprocs] "dynamically generated" else @default end end @validators.each { |v| h[v.name.to_sym] = v.criteria } h end
transform(value)
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 71 def transform(value) @transform ? @transform.call(value) : value end
type()
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 88 def type type_string = self.class.to_s.split("::").last type_string.to_sym end
validate(params)
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 63 def validate(params) @validators.each do |v| return false, build_error_hash(v.name, v.value) unless v.validate(self.name, params) end true end
validator_declarations()
click to toggle source
# File lib/sinatra/browse/parameter_type.rb, line 125 def validator_declarations self.class.validator_declarations end