Class: Apipie::ParamDescription

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block) ⇒ ParamDescription

Returns a new instance of ParamDescription.



24
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
61
62
63
64
65
66
67
68
69
70
# File 'lib/apipie/param_description.rb', line 24

def initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block)

  if desc_or_options.is_a?(Hash)
    options = options.merge(desc_or_options)
  elsif desc_or_options.is_a?(String)
    options[:desc] = desc_or_options
  elsif !desc_or_options.nil?
    raise ArgumentError.new("param description: expected description or options as 3rd parameter")
  end

  options.symbolize_keys!

  # we save options to know what was passed in DSL
  @options = options
  if @options[:param_group]
    @from_concern = @options[:param_group][:from_concern]
  end

  @method_description = method_description
  @name = concern_subst(name)
  @as = options[:as] || @name
  @desc = preformat_text(@options[:desc])

  @parent = @options[:parent]
  @metadata = @options[:meta]

  @required = if @options.has_key? :required
    @options[:required]
  else
    Apipie.configuration.required_by_default?
  end

  @show = if @options.has_key? :show
    @options[:show]
  else
    true
  end

  @allow_nil = @options[:allow_nil] || false

  action_awareness

  if validator
    @validator = Validator::BaseValidator.find(self, validator, @options, block)
    raise "Validator for #{validator} not found." unless @validator
  end
end

Instance Attribute Details

#allow_nilObject (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

#asObject (readonly)

Returns the value of attribute as.



11
12
13
# File 'lib/apipie/param_description.rb', line 11

def as
  @as
end

#descObject (readonly)

Returns the value of attribute desc.



11
12
13
# File 'lib/apipie/param_description.rb', line 11

def desc
  @desc
end

#metadataObject (readonly)

Returns the value of attribute metadata.



11
12
13
# File 'lib/apipie/param_description.rb', line 11

def 
  @metadata
end

#method_descriptionObject (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

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/apipie/param_description.rb', line 11

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/apipie/param_description.rb', line 11

def options
  @options
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/apipie/param_description.rb', line 12

def parent
  @parent
end

#requiredObject

Returns the value of attribute required.



12
13
14
# File 'lib/apipie/param_description.rb', line 12

def required
  @required
end

#showObject (readonly)

Returns the value of attribute show.



11
12
13
# File 'lib/apipie/param_description.rb', line 11

def show
  @show
end

#validatorObject (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



14
15
16
17
18
19
20
21
22
# File 'lib/apipie/param_description.rb', line 14

def self.from_dsl_data(method_description, args)
  param_name, validator, desc_or_options, options, block = args
  Apipie::ParamDescription.new(method_description,
                               param_name,
                               validator,
                               desc_or_options,
                               options,
                               &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


148
149
150
151
152
153
# File 'lib/apipie/param_description.rb', line 148

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)

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
# File 'lib/apipie/param_description.rb', line 157

def action_aware?
  if @options.has_key?(:action_aware)
    return @options[:action_aware]
  elsif @parent
    @parent.action_aware?
  else
    false
  end
end

#action_awarenessObject

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/apipie/param_description.rb', line 181

def action_awareness
  if action_aware?
    if !@options.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_actionObject



167
168
169
170
171
172
173
174
175
176
# File 'lib/apipie/param_description.rb', line 167

def as_action
  if @options[:param_group] && @options[:param_group][:options] &&
      @options[:param_group][:options][:as]
    @options[:param_group][:options][:as].to_s
  elsif @parent
    @parent.as_action
  else
    @method_description.method
  end
end

#concern_subst(string) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/apipie/param_description.rb', line 196

def concern_subst(string)
  return string if string.nil? or !from_concern?

  original = string
  string = ":#{original}" if original.is_a? Symbol

  replaced = method_description.resource.controller._apipie_perform_concern_subst(string)

  return original if replaced == string
  return replaced.to_sym if original.is_a? Symbol
  return replaced
end

#from_concern?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/apipie/param_description.rb', line 72

def from_concern?
  method_description.from_concern? || @from_concern
end

#full_nameObject



93
94
95
96
97
# File 'lib/apipie/param_description.rb', line 93

def full_name
  name_parts = parents_and_self.map{|p| p.name if p.show}.compact
  return name.to_s if name_parts.blank?
  return ([name_parts.first] + name_parts[1..-1].map { |n| "[#{n}]" }).join("")
end

#merge_with(other_param_desc) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/apipie/param_description.rb', line 126

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_selfObject

returns an array of all the parents: starting with the root parent ending with itself



101
102
103
104
105
106
107
108
# File 'lib/apipie/param_description.rb', line 101

def parents_and_self
  ret = []
  if self.parent
    ret.concat(self.parent.parents_and_self)
  end
  ret << self
  ret
end

#preformat_text(text) ⇒ Object



209
210
211
# File 'lib/apipie/param_description.rb', line 209

def preformat_text(text)
  concern_subst(Apipie.markup_to_html(text || ''))
end

#process_value(value) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/apipie/param_description.rb', line 85

def process_value(value)
  if @validator.respond_to?(:process_value)
    @validator.process_value(value)
  else
    value
  end
end

#to_json(lang = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/apipie/param_description.rb', line 110

def to_json(lang = nil)
  hash = { :name => name.to_s,
           :full_name => full_name,
           :description => preformat_text(Apipie.app.translate(@options[:desc], lang)),
           :required => required,
           :allow_nil => allow_nil,
           :validator => validator.to_s,
           :expected_type => validator.expected_type,
           :metadata => ,
           :show => show }
  if sub_params = validator.params_ordered
    hash[:params] = sub_params.map { |p| p.to_json(lang)}
  end
  hash
end

#validate(value) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/apipie/param_description.rb', line 76

def validate(value)
  return true if @allow_nil && value.nil?
  if (!@allow_nil && value.nil?) || !@validator.valid?(value)
    error = @validator.error
    error = ParamError.new(error) unless error.is_a? StandardError
    raise error
  end
end