Class: TLAW::Param
- Inherits:
-
Object
- Object
- TLAW::Param
- Defined in:
- lib/tlaw/param.rb,
lib/tlaw/param/type.rb
Overview
Base parameter class for working with parameters validation and converting. You'll never instantiate it directly, just see DSL#param for parameters definition.
Constant Summary collapse
- Nonconvertible =
This error is thrown when some value could not be converted to what this parameter inspects. For example:
# 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 :)
Class.new(ArgumentError)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
(also: #to_h)
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #convert(value) ⇒ Object
- #convert_and_format(value) ⇒ Object
- #default ⇒ Object
- #describe ⇒ Object
- #description ⇒ Object
- #field ⇒ Object
- #format(value) ⇒ Object
-
#initialize(name, **options) ⇒ Param
constructor
A new instance of Param.
- #merge(**new_options) ⇒ Object
- #required? ⇒ Boolean
Constructor Details
#initialize(name, **options) ⇒ Param
Returns a new instance of Param.
37 38 39 40 41 42 43 44 |
# File 'lib/tlaw/param.rb', line 37 def initialize(name, **) @name = name @options = @type = Type.parse() @options[:desc] ||= @options[:description] @options[:desc].gsub!(/\n( *)/, "\n ") if @options[:desc] @formatter = make_formatter end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
35 36 37 |
# File 'lib/tlaw/param.rb', line 35 def name @name end |
#options ⇒ Object (readonly) Also known as: to_h
Returns the value of attribute options.
35 36 37 |
# File 'lib/tlaw/param.rb', line 35 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
35 36 37 |
# File 'lib/tlaw/param.rb', line 35 def type @type end |
Class Method Details
Instance Method Details
#convert(value) ⇒ Object
62 63 64 |
# File 'lib/tlaw/param.rb', line 62 def convert(value) type.convert(value) end |
#convert_and_format(value) ⇒ Object
70 71 72 |
# File 'lib/tlaw/param.rb', line 70 def convert_and_format(value) format(convert(value)) end |
#default ⇒ Object
50 51 52 |
# File 'lib/tlaw/param.rb', line 50 def default [:default] end |
#describe ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# 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 ⇒ Object
76 77 78 |
# File 'lib/tlaw/param.rb', line 76 def description [:desc] end |
#field ⇒ Object
58 59 60 |
# File 'lib/tlaw/param.rb', line 58 def field [:field] || name end |
#format(value) ⇒ Object
66 67 68 |
# File 'lib/tlaw/param.rb', line 66 def format(value) to_url_part(formatter.call(value)) end |
#merge(**new_options) ⇒ Object
54 55 56 |
# File 'lib/tlaw/param.rb', line 54 def merge(**) Param.make(name, @options.merge()) end |
#required? ⇒ Boolean
46 47 48 |
# File 'lib/tlaw/param.rb', line 46 def required? [:required] end |