Class: Apipie::ParamDescription
- Inherits:
-
Object
- Object
- Apipie::ParamDescription
- Defined in:
- lib/apipie/param_description.rb
Overview
method parameter description
name - method name (show) desc - description required - boolean if required validator - Validator::BaseValidator subclass
Instance Attribute Summary collapse
-
#allow_nil ⇒ Object
readonly
Returns the value of attribute allow_nil.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#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.
-
#required ⇒ Object
Returns the value of attribute required.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Class Method Summary collapse
- .from_dsl_data(method_description, args) ⇒ Object
-
.unify(params) ⇒ Object
merge param descripsiont.
Instance Method Summary collapse
-
#action_aware? ⇒ Boolean
action awareness is being inherited from ancestors (in terms of nested params).
-
#action_awareness ⇒ Object
makes modification that are based on the action that the param is defined for.
- #as_action ⇒ Object
- #concern_subst(string) ⇒ Object
- #full_name ⇒ Object
-
#initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block) ⇒ ParamDescription
constructor
A new instance of ParamDescription.
- #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.
- #to_json ⇒ Object
- #validate(value) ⇒ Object
Constructor Details
#initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block) ⇒ ParamDescription
Returns a new instance of ParamDescription.
25 26 27 28 29 30 31 32 33 34 35 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 |
# File 'lib/apipie/param_description.rb', line 25 def initialize(method_description, name, validator, = nil, = {}, &block) if .is_a?(Hash) = .merge() elsif .is_a?(String) [:desc] = elsif !.nil? raise ArgumentError.new("param description: expected description or options as 3rd parameter") end .symbolize_keys! # we save options to know what was passed in DSL = @method_description = method_description @name = concern_subst(name) @desc = concern_subst(Apipie.markup_to_html([:desc] || '')) @parent = [:parent] @required = if .has_key? :required [:required] else Apipie.configuration.required_by_default? end @allow_nil = [:allow_nil] || false action_awareness @validator = nil unless validator.nil? @validator = Validator::BaseValidator.find(self, validator, , block) raise "Validator not found." unless validator end end |
Instance Attribute Details
#allow_nil ⇒ Object (readonly)
Returns the value of attribute allow_nil.
11 12 13 |
# File 'lib/apipie/param_description.rb', line 11 def allow_nil @allow_nil end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
11 12 13 |
# File 'lib/apipie/param_description.rb', line 11 def desc @desc end |
#method_description ⇒ Object (readonly)
Returns the value of attribute method_description.
11 12 13 |
# File 'lib/apipie/param_description.rb', line 11 def method_description @method_description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/apipie/param_description.rb', line 11 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/apipie/param_description.rb', line 11 def end |
#parent ⇒ Object
Returns the value of attribute parent.
13 14 15 |
# File 'lib/apipie/param_description.rb', line 13 def parent @parent end |
#required ⇒ Object
Returns the value of attribute required.
13 14 15 |
# File 'lib/apipie/param_description.rb', line 13 def required @required end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
11 12 13 |
# File 'lib/apipie/param_description.rb', line 11 def validator @validator end |
Class Method Details
.from_dsl_data(method_description, args) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/apipie/param_description.rb', line 15 def self.from_dsl_data(method_description, args) param_name, validator, , , block = args Apipie::ParamDescription.new(method_description, param_name, validator, , , &block) end |
.unify(params) ⇒ Object
merge param descripsiont. 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
134 135 136 137 138 139 |
# File 'lib/apipie/param_description.rb', line 134 def self.unify(params) ordering = params.map(&:name) params.group_by(&:name).map do |name, param_descs| param_descs.reduce(&:merge_with) end.sort_by { |param| ordering.index(param.name) } end |
Instance Method Details
#action_aware? ⇒ Boolean
action awareness is being inherited from ancestors (in terms of nested params)
143 144 145 146 147 148 149 150 151 |
# File 'lib/apipie/param_description.rb', line 143 def action_aware? if .has_key?(:action_aware) return [:action_aware] elsif @parent @parent.action_aware? else false end end |
#action_awareness ⇒ Object
makes modification that are based on the action that the param is defined for. Typical for required and allow_nil variations in crate/update actions.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/apipie/param_description.rb', line 167 def action_awareness if action_aware? if !.has_key?(:allow_nil) if @required @allow_nil = false else @allow_nil = true end end if as_action != "create" @required = false end end end |
#as_action ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/apipie/param_description.rb', line 153 def as_action if [:param_group] && [:param_group][:options] && [:param_group][:options][:as] [:param_group][:options][:as].to_s elsif @parent @parent.as_action else @method_description.method end end |
#concern_subst(string) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/apipie/param_description.rb', line 182 def concern_subst(string) return if string.nil? original = string if method_description.from_concern? string = ":#{original}" if original.is_a? Symbol ret = method_description.resource.controller._apipie_perform_concern_subst(string) ret = ret.to_sym if original.is_a? Symbol ret else string end end |
#full_name ⇒ Object
71 72 73 74 |
# File 'lib/apipie/param_description.rb', line 71 def full_name name_parts = parents_and_self.map(&:name) return ([name_parts.first] + name_parts[1..-1].map { |n| "[#{n}]" }).join("") end |
#merge_with(other_param_desc) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/apipie/param_description.rb', line 112 def merge_with(other_param_desc) if self.validator && other_param_desc.validator self.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
78 79 80 81 82 83 84 85 |
# File 'lib/apipie/param_description.rb', line 78 def parents_and_self ret = [] if self.parent ret.concat(self.parent.parents_and_self) end ret << self ret end |
#to_json ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/apipie/param_description.rb', line 87 def to_json if validator.is_a? Apipie::Validator::HashValidator { :name => name.to_s, :full_name => full_name, :description => desc, :required => required, :allow_nil => allow_nil, :validator => validator.to_s, :expected_type => validator.expected_type, :params => validator.hash_params_ordered.map(&:to_json) } else { :name => name.to_s, :full_name => full_name, :description => desc, :required => required, :allow_nil => allow_nil, :validator => validator.to_s, :expected_type => validator.expected_type } end end |
#validate(value) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/apipie/param_description.rb', line 62 def validate(value) return true if @allow_nil && value.nil? unless @validator.valid?(value) error = @validator.error error = ParamError.new(error) unless error.is_a? Exception raise error end end |