Class: TLAW::Param

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, **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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/tlaw/param.rb', line 35

def name
  @name
end

#optionsObject (readonly) Also known as: to_h

Returns the value of attribute options.



35
36
37
# File 'lib/tlaw/param.rb', line 35

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



35
36
37
# File 'lib/tlaw/param.rb', line 35

def type
  @type
end

Class Method Details

.make(name, **options) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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

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

#defaultObject



50
51
52
# File 'lib/tlaw/param.rb', line 50

def default
  options[:default]
end

#describeObject



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

#descriptionObject



76
77
78
# File 'lib/tlaw/param.rb', line 76

def description
  options[:desc]
end

#fieldObject



58
59
60
# File 'lib/tlaw/param.rb', line 58

def field
  options[: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(**new_options)
  Param.make(name, @options.merge(new_options))
end

#required?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/tlaw/param.rb', line 46

def required?
  options[:required]
end