Class: TLAW::ParamSet

Inherits:
Object
  • Object
show all
Defined in:
lib/tlaw/param_set.rb

Overview

Represents set of param current API endpoint or namespace have. You'll never instantiate it directly, just look at DSL#param for param creation. But probably you could make use of knowledge of this class' API when deep investigating what's going on, like:

params = api.namespaces[:my_namespace].endpoints[:my_endpoint].param_set
p [params.count, params.names, params.describe]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParamSet

Returns a new instance of ParamSet.



14
15
16
# File 'lib/tlaw/param_set.rb', line 14

def initialize
  @params = {}
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/tlaw/param_set.rb', line 12

def parent
  @parent
end

Instance Method Details

#[](name) ⇒ Object



30
31
32
# File 'lib/tlaw/param_set.rb', line 30

def [](name)
  @params[name]
end

#add(name, **opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tlaw/param_set.rb', line 18

def add(name, **opts)
  # Not updating parent param, just make sure it exists
  return if @parent && @parent.all_params[name]

  @params[name] =
    if @params[name]
      @params[name].merge(**opts)
    else
      Param.make(name, **opts)
    end
end

#all_paramsObject



77
78
79
# File 'lib/tlaw/param_set.rb', line 77

def all_params
  (@parent ? @parent.all_params : {}).merge(@params)
end

#describeObject



62
63
64
# File 'lib/tlaw/param_set.rb', line 62

def describe
  Util::Description.new(ordered.map(&:describe).join("\n"))
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @params.empty? && (!@parent || @parent.empty?)
end

#inspectObject Also known as: to_s



81
82
83
84
# File 'lib/tlaw/param_set.rb', line 81

def inspect
  "#<#{self.class.name} #{names.join(', ')}"\
    "#{" (parent=#{parent.inspect})" if parent && !parent.empty?}>"
end

#namesObject



42
43
44
# File 'lib/tlaw/param_set.rb', line 42

def names
  @params.keys
end

#process(**input) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/tlaw/param_set.rb', line 66

def process(**input)
  validate_unknown(input)

  all_params
    .map { |name, dfn| [name, dfn, input[name]] }
    .tap(&method(:validate_required))
    .reject { |*, val| val.nil? }
    .map { |_name, dfn, val| [dfn.field, dfn.convert_and_format(val)] }
    .to_h
end

#to_aObject



34
35
36
# File 'lib/tlaw/param_set.rb', line 34

def to_a
  @params.values
end

#to_codeObject



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

def to_code
  ordered.map(&:to_code).join(', ')
end

#to_hObject



38
39
40
# File 'lib/tlaw/param_set.rb', line 38

def to_h
  @params
end

#to_hash_code(values = nil) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/tlaw/param_set.rb', line 54

def to_hash_code(values = nil)
  if values
    names.map { |n| "#{n}: #{values[n].inspect}" }.join(', ')
  else
    names.map { |n| "#{n}: #{n}" }.join(', ')
  end
end