Class: TLAW::ParamSet
- Inherits:
-
Object
- Object
- TLAW::ParamSet
- 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
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add(name, **opts) ⇒ Object
- #all_params ⇒ Object
- #describe ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ ParamSet
constructor
A new instance of ParamSet.
- #inspect ⇒ Object (also: #to_s)
- #names ⇒ Object
- #process(**input) ⇒ Object
- #to_a ⇒ Object
- #to_code ⇒ Object
- #to_h ⇒ Object
- #to_hash_code(values = nil) ⇒ Object
Constructor Details
#initialize ⇒ ParamSet
Returns a new instance of ParamSet.
14 15 16 |
# File 'lib/tlaw/param_set.rb', line 14 def initialize @params = {} end |
Instance Attribute Details
#parent ⇒ Object
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_params ⇒ Object
77 78 79 |
# File 'lib/tlaw/param_set.rb', line 77 def all_params (@parent ? @parent.all_params : {}).merge(@params) end |
#describe ⇒ Object
62 63 64 |
# File 'lib/tlaw/param_set.rb', line 62 def describe Util::Description.new(ordered.map(&:describe).join("\n")) end |
#empty? ⇒ Boolean
46 47 48 |
# File 'lib/tlaw/param_set.rb', line 46 def empty? @params.empty? && (!@parent || @parent.empty?) end |
#inspect ⇒ Object 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 |
#names ⇒ Object
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_a ⇒ Object
34 35 36 |
# File 'lib/tlaw/param_set.rb', line 34 def to_a @params.values end |
#to_code ⇒ Object
50 51 52 |
# File 'lib/tlaw/param_set.rb', line 50 def to_code ordered.map(&:to_code).join(', ') end |
#to_h ⇒ Object
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 |