class TLAW::Param
Base parameter class for working with parameters validation and converting. You'll never instantiate it directly, just see {DSL#param} for parameters definition.
Constants
- Nonconvertible
This error is thrown when some value could not be converted to what this parameter inspects. For example:
“`ruby # definition: param :timestamp, :to_time, format: :to_i # this means: parameter, when passed, will first be converted with # method to_time, and then resulting time will be made into # unix timestamp with to_i before passing to
API
# usage: my_endpoint(timestamp: Time.now) # ok my_endpoint(timestamp: Date.today) # ok my_endpoint(timestamp: '2016-06-01') #
Nonconvertible
! …unless you've included ActiveSupport :) “`
Attributes
formatter[R]
name[R]
options[R]
to_h[R]
type[R]
Public Class Methods
make(name, **options)
click to toggle source
# File lib/tlaw/param.rb, line 25 def self.make(name, **options) # NB: Sic. :keyword is nil (not provided) should still # make a keyword argument. if options[:keyword] != false KeywordParam.new(name, **options) else ArgumentParam.new(name, **options) end end
new(name, **options)
click to toggle source
# File lib/tlaw/param.rb, line 37 def initialize(name, **options) @name = name @options = options @type = Type.parse(options) @options[:desc] ||= @options[:description] @options[:desc].gsub!(/\n( *)/, "\n ") if @options[:desc] @formatter = make_formatter end
Public Instance Methods
convert(value)
click to toggle source
# File lib/tlaw/param.rb, line 62 def convert(value) type.convert(value) end
convert_and_format(value)
click to toggle source
# File lib/tlaw/param.rb, line 70 def convert_and_format(value) format(convert(value)) end
default()
click to toggle source
# File lib/tlaw/param.rb, line 50 def default options[:default] end
describe()
click to toggle source
# File lib/tlaw/param.rb, line 80 def describe [ '@param', name, ("[#{doc_type}]" if doc_type), description, if @options[:enum] "\n Possible values: #{type.values.map(&:inspect).join(', ')}" end, ("(default = #{default.inspect})" if default) ].compact.join(' ') .derp(&Util::Description.method(:new)) end
description()
click to toggle source
# File lib/tlaw/param.rb, line 76 def description options[:desc] end
field()
click to toggle source
# File lib/tlaw/param.rb, line 58 def field options[:field] || name end
format(value)
click to toggle source
# File lib/tlaw/param.rb, line 66 def format(value) to_url_part(formatter.call(value)) end
merge(**new_options)
click to toggle source
# File lib/tlaw/param.rb, line 54 def merge(**new_options) Param.make(name, @options.merge(new_options)) end
required?()
click to toggle source
# File lib/tlaw/param.rb, line 46 def required? options[:required] end
Private Instance Methods
default_to_code()
click to toggle source
# File lib/tlaw/param.rb, line 118 def default_to_code # FIXME: this `inspect` will fail with, say, Time default.inspect end
doc_type()
click to toggle source
# File lib/tlaw/param.rb, line 97 def doc_type type.to_doc_type end
make_formatter()
click to toggle source
# File lib/tlaw/param.rb, line 110 def make_formatter options[:format].derp { |f| return ->(v) { v } unless f return f.to_proc if f.respond_to?(:to_proc) fail ArgumentError, "#{self}: unsupporter formatter #{f}" } end
to_url_part(value)
click to toggle source
# File lib/tlaw/param.rb, line 101 def to_url_part(value) case value when Array value.join(',') else value.to_s end end