Class: ApipieDSL::ParameterDescription
- Inherits:
-
Object
- Object
- ApipieDSL::ParameterDescription
- Defined in:
- lib/apipie_dsl/parameter_description.rb
Overview
method parameter description
name - method name (show) desc - description validator - Validator::BaseValidator subclass
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#is_array ⇒ Object
(also: #is_array?)
readonly
Returns the value of attribute is_array.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#method_description ⇒ Object
readonly
Returns the value of attribute method_description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#show ⇒ Object
readonly
Returns the value of attribute show.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .from_dsl_data(method_description, args) ⇒ Object
- .merge(target_params, source_params) ⇒ Object
-
.unify(params) ⇒ Object
Merge param descriptions.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #docs(lang = nil) ⇒ Object
- #full_name ⇒ Object
-
#initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block) ⇒ ParameterDescription
constructor
A new instance of ParameterDescription.
- #merge_with(other_param_desc) ⇒ Object
-
#parents_and_self ⇒ Object
Returns an array of all the parents: starting with the root parent ending with itself.
- #validate(value) ⇒ Object
- #validator ⇒ Object
Constructor Details
#initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block) ⇒ ParameterDescription
Returns a new instance of ParameterDescription.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/apipie_dsl/parameter_description.rb', line 36 def initialize(method_description, name, validator, = nil, = {}, &block) if .is_a?(Hash) = .merge() elsif .is_a?(String) [:desc] = elsif !.nil? raise ArgumentError, 'Parameter description: expected description or options as 3rd parameter' end @options = .transform_keys(&:to_sym) @method_description = method_description @name = name @desc = @options[:desc] @type = @options[:type] || :required @schema = @options[:schema] @default_value = @options[:default] @parent = @options[:parent] @metadata = @options[:meta] @show = @options.key?(:show) ? @options[:show] : true return unless validator @validator = if validator.is_a?(String) ApipieDSL::Validator::Lazy.new(self, validator, @options, block) else ApipieDSL::Validator::BaseValidator.find(self, validator, @options, block) end raise StandardError, "Validator for #{validator} not found." unless @validator end |
Instance Attribute Details
#default_value ⇒ Object (readonly)
Returns the value of attribute default_value.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def default_value @default_value end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def desc @desc end |
#is_array ⇒ Object (readonly) Also known as: is_array?
Returns the value of attribute is_array.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def is_array @is_array end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def @metadata end |
#method_description ⇒ Object (readonly)
Returns the value of attribute method_description.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def method_description @method_description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def @options end |
#parent ⇒ Object
Returns the value of attribute parent.
12 13 14 |
# File 'lib/apipie_dsl/parameter_description.rb', line 12 def parent @parent end |
#show ⇒ Object (readonly)
Returns the value of attribute show.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def show @show end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/apipie_dsl/parameter_description.rb', line 10 def type @type end |
Class Method Details
.from_dsl_data(method_description, args) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/apipie_dsl/parameter_description.rb', line 16 def self.from_dsl_data(method_description, args) name, validator, , , block = args ApipieDSL::ParameterDescription.new(method_description, name, validator, , , &block) end |
.merge(target_params, source_params) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/apipie_dsl/parameter_description.rb', line 122 def self.merge(target_params, source_params) params_to_merge, params_to_add = source_params.partition do |source_param| target_params.any? { |target_param| source_param.name == target_param.name } end unify(target_params + params_to_merge) target_params.concat(params_to_add) end |
.unify(params) ⇒ Object
Merge param descriptions. Allows defining hash params on more places (e.g. in param_groups). For example:
def_param_group :user do
param :user, Hash do
param :name, String
end
end
param_group :user
param :user, Hash do
param :password, String
end
115 116 117 118 119 120 |
# File 'lib/apipie_dsl/parameter_description.rb', line 115 def self.unify(params) ordering = params.map(&:name) params.group_by(&:name).map do |_name, description| description.reduce(&:merge_with) end.sort_by { |param| ordering.index(param.name) } end |
Instance Method Details
#==(other) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/apipie_dsl/parameter_description.rb', line 26 def ==(other) return false unless self.class == other.class if method_description == other.method_description && @options == other. true else false end end |
#docs(lang = nil) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/apipie_dsl/parameter_description.rb', line 130 def docs(lang = nil) hash = { name: name.to_s, full_name: full_name, description: ApipieDSL.markup_to_html(ApipieDSL.translate(@options[:desc], lang)), type: type.to_s, default: default_value, validator: validator.to_s, expected_type: validator.expected_type, metadata: , show: show } hash.delete(:default) if type == :required hash[:schema] = @schema if type == :block return hash unless validator.sub_params hash[:params] = validator.sub_params.map { |param| param.docs(lang) } hash end |
#full_name ⇒ Object
77 78 79 80 81 82 |
# File 'lib/apipie_dsl/parameter_description.rb', line 77 def full_name name_parts = parents_and_self.map { |p| p.name if p.show }.compact return name.to_s if name_parts.empty? ([name_parts.first] + name_parts[1..-1].map { |n| "[#{n}]" }).join('') end |
#merge_with(other_param_desc) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/apipie_dsl/parameter_description.rb', line 93 def merge_with(other_param_desc) if validator && other_param_desc.validator validator.merge_with(other_param_desc.validator) else self.validator ||= other_param_desc.validator end self end |
#parents_and_self ⇒ Object
Returns an array of all the parents: starting with the root parent ending with itself
86 87 88 89 90 91 |
# File 'lib/apipie_dsl/parameter_description.rb', line 86 def parents_and_self ret = [] ret.concat(parent.parents_and_self) if parent ret << self ret end |
#validate(value) ⇒ Object
73 74 75 |
# File 'lib/apipie_dsl/parameter_description.rb', line 73 def validate(value) validator.valid?(value) end |