Class: Praxis::Types::MultipartArray

Inherits:
Attributor::Collection
  • Object
show all
Includes:
MediaTypeCommon
Defined in:
lib/praxis/types/multipart_array.rb,
lib/praxis/types/multipart_array/part_definition.rb

Defined Under Namespace

Classes: PartDefinition

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type: self.class.identifier) ⇒ MultipartArray

Returns a new instance of MultipartArray.



221
222
223
# File 'lib/praxis/types/multipart_array.rb', line 221

def initialize(content_type: self.class.identifier)
  self.content_type = content_type
end

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



34
35
36
# File 'lib/praxis/types/multipart_array.rb', line 34

def attributes
  @attributes
end

.identifierObject (readonly)

Returns the value of attribute identifier.



37
38
39
# File 'lib/praxis/types/multipart_array.rb', line 37

def identifier
  @identifier
end

.multipleObject (readonly)

Returns the value of attribute multiple.



35
36
37
# File 'lib/praxis/types/multipart_array.rb', line 35

def multiple
  @multiple
end

.optionsObject (readonly)

Returns the value of attribute options.



36
37
38
# File 'lib/praxis/types/multipart_array.rb', line 36

def options
  @options
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



219
220
221
# File 'lib/praxis/types/multipart_array.rb', line 219

def content_type
  @content_type
end

#preambleObject

Returns the value of attribute preamble.



218
219
220
# File 'lib/praxis/types/multipart_array.rb', line 218

def preamble
  @preamble
end

Class Method Details

.construct(constructor_block, _options = {}) ⇒ Object



44
45
46
# File 'lib/praxis/types/multipart_array.rb', line 44

def self.construct(constructor_block, _options={})
  Class.new(self, &constructor_block)
end

.constructable?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/praxis/types/multipart_array.rb', line 40

def self.constructable?
  true
end

.describe(shallow = true, example: nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/praxis/types/multipart_array.rb', line 157

def self.describe(shallow=true, example: nil)
  type_name = Attributor.type_name(self)
  hash = {
    name: type_name.gsub(Attributor::MODULE_PREFIX_REGEX, ''),
    family: self.family,
    id: self.id
  }
  hash[:example] = example if example

  hash[:part_name] = {type: name_type.describe(true)}

  unless shallow
    hash[:attributes] = {} if self.attributes.keys.any? { |name| name.kind_of? String}
    hash[:pattern_attributes] = {} if self.attributes.keys.any? { |name| name.kind_of? Regexp}

    if hash.key?(:attributes) || hash.key?(:pattern_attributes)
      self.describe_attributes(shallow, example: example).each do |name, sub_hash|
        case name
        when String
          hash[:attributes][name] = sub_hash
        when Regexp
          hash[:pattern_attributes][name.source] = sub_hash
        end
      end
    else
      hash[:part_payload] = {type: payload_type.describe(true)}
    end
  end
  hash
end

.describe_attributes(shallow = true, example: nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/praxis/types/multipart_array.rb', line 188

def self.describe_attributes(shallow=true, example: nil)
  self.attributes.each_with_object({}) do |(part_name, part_attribute), parts|
    sub_example = example.part(part_name) if example
    if sub_example && self.multiple.include?(part_name)
      sub_example = sub_example.first
    end

    sub_hash = part_attribute.describe(shallow, example: sub_example)


    if (options = sub_hash.delete(:options))
      sub_hash[:options] = {}
      if self.multiple.include?(part_name)
        sub_hash[:options][:multiple] = true
      end

      if (payload_attribute = options.delete :payload_attribute)
        if (required = payload_attribute.options[:required])
          sub_hash[:options][:required] = true
        end
      end
    end

    sub_hash[:type] = MultipartPart.describe(shallow, example: sub_example, options: part_attribute.options)


    parts[part_name] = sub_hash
  end
end

.dump(value, **opts) ⇒ Object



310
311
312
# File 'lib/praxis/types/multipart_array.rb', line 310

def self.dump(value, **opts)
  value.dump(**opts)
end

.example(context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/praxis/types/multipart_array.rb', line 135

def self.example(context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
  example = self.new

  self.attributes.each do |name, attribute|
    next if name.kind_of? Regexp
    sub_context = self.generate_subcontext(context, name)

    part = attribute.example(sub_context)
    part.name = name
    example.push part

    if self.multiple.include? name
      part = attribute.example(sub_context + ['2'])
      part.name = name
      example.push part
    end
  end

  example
end

.file(name, payload_type = nil, filename: nil, **opts, &block) ⇒ Object



112
113
114
# File 'lib/praxis/types/multipart_array.rb', line 112

def self.file(name, payload_type=nil, filename: nil, **opts, &block)
  self.part(name, payload_type, filename: true, **opts, &block)
end

.inherited(klass) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/praxis/types/multipart_array.rb', line 14

def self.inherited(klass)
  klass.instance_eval do
    @attributes = FuzzyHash.new
    @saved_blocks = []
    @multiple = []
    @options = {}

    @payload_type = Attributor::String
    @name_type = Attributor::String
    @member_type = Praxis::MultipartPart

    @payload_attribute = nil
    @part_attribute = nil

    @identifier = MediaTypeIdentifier.load('multipart/form-data').freeze

  end
end

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/praxis/types/multipart_array.rb', line 116

def self.load(value, context=Attributor::DEFAULT_ROOT_CONTEXT, content_type:nil)
  return value if value.kind_of?(self) || value.nil?

  if value.kind_of?(::String) && content_type.nil?
    raise ArgumentError, "content_type is required to load values of type String for #{Attributor.type_name(self)}"
  end

  parser = Praxis::MultipartParser.new({'Content-Type' => content_type}, value)
  preamble, parts = parser.parse

  instance = self.new
  instance.push(*parts)

  instance.preamble = preamble
  instance.content_type = content_type

  instance
end

.name_type(type = nil) ⇒ Object



48
49
50
51
52
# File 'lib/praxis/types/multipart_array.rb', line 48

def self.name_type(type=nil)
  return @name_type if type.nil?

  @name_type = Attributor.resolve_type type
end

.part(name, payload_type = nil, multiple: false, filename: false, **opts, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
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/praxis/types/multipart_array.rb', line 76

def self.part(name, payload_type=nil, multiple: false, filename: false, **opts, &block)
  @attributes.default_proc = nil

  if name.kind_of?(Regexp)
    raise 'part with regexp name may not take :multiple option' if multiple
    raise 'part with regexp name may not be required' if opts[:required] == true
  end

  self.multiple << name if multiple

  compiler = Attributor::DSLCompiler.new(self, opts)

  if filename
    filename_attribute = compiler.define('filename', String, required: true)
  end

  if block_given?
    definition = PartDefinition.new(&block)
    payload_attribute = definition.payload_attribute
    header_attribute = definition.headers_attribute
    filename_attribute = definition.filename_attribute || filename_attribute

    self.attributes[name] = compiler.define(name, Praxis::MultipartPart,
                                            payload_attribute: payload_attribute,
                                            headers_attribute: header_attribute,
                                            filename_attribute: filename_attribute
                                            )
  else
    payload_attribute = compiler.define(name, payload_type || self.payload_type, **opts)
    self.attributes[name] = compiler.define(name, Praxis::MultipartPart,
                                            payload_attribute: payload_attribute,
                                            filename_attribute: filename_attribute
                                            )
  end
end

.part_attributeObject



72
73
74
# File 'lib/praxis/types/multipart_array.rb', line 72

def self.part_attribute
  @part_attribute ||= Attributor::Attribute.new(Praxis::MultipartPart, payload_attribute: self.payload_attribute)
end

.payload_attributeObject



68
69
70
# File 'lib/praxis/types/multipart_array.rb', line 68

def self.payload_attribute
  @payload_attribute ||= Attributor::Attribute.new(@payload_type)
end

.payload_type(type = nil, **opts, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/praxis/types/multipart_array.rb', line 54

def self.payload_type(type=nil, **opts, &block)
  if type.nil?
    if block_given?
      type = Attributor::Struct
    else
      return @payload_type
    end
  end
  @payload_type = Attributor.resolve_type(type)
  @payload_attribute = Attributor::Attribute.new(@payload_type, **opts, &block)
  @part_attribute = nil
  @payload_type
end

Instance Method Details

#dump(**opts) ⇒ Object



314
315
316
317
318
319
320
321
322
323
# File 'lib/praxis/types/multipart_array.rb', line 314

def dump(**opts)
  boundary = content_type.parameters.get 'boundary'

  parts = self.collect do |part|
    part.dump(**opts)
  end

  all_entities = parts.join("\r\n--#{boundary}\r\n")
  "--#{boundary}\r\n#{all_entities}\r\n--#{boundary}--\r\n"
end

#part(name) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/praxis/types/multipart_array.rb', line 279

def part(name)
  if self.class.multiple.include?(name)
    self.select { |i| i.name == name }
  else
    self.find { |i| i.name == name }
  end
end

#payload_typeObject



233
234
235
# File 'lib/praxis/types/multipart_array.rb', line 233

def payload_type
  self.class.payload_type
end

#push(*parts, context: Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object

Raises:

  • (Attributor::AttributorException)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/praxis/types/multipart_array.rb', line 237

def push(*parts, context: Attributor::DEFAULT_ROOT_CONTEXT)
  part, *rest = parts
  if rest.any?
    return self.push(part, context: context).push(*rest, context:context)
  end

  original_context = context

  part.name = self.class.name_type.load(part.name, self.class.generate_subcontext(context, part.name))
  key = part.name

  context = self.class.generate_subcontext(context, key)

  # If no attributes are defined, we always use the default
  # payload_attribute, otherwise we constrain the parts
  # to the defined names.
  attribute = if self.class.attributes.empty?
    self.class.part_attribute
  elsif (default_thingy = self.class.attributes[key])
    default_thingy
  else
    nil
  end

  if attribute
    part.attribute = attribute
    part.load_payload(context + ['payload'])
    part.load_headers(context + ['headers'])
    return self << part
  elsif self.class.options[:case_insensitive_load]
    name = self.class.attributes.keys.find do |k|
      k.kind_of?(String) && key.downcase == k.downcase
    end
    if name
      part.name = name
      return self.push(part, context: original_context)
    end
  end

  raise Attributor::AttributorException, "Unknown part name received: #{key.inspect} while loading #{Attributor.humanize_context(context)}"
end

#validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/praxis/types/multipart_array.rb', line 287

def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)
  errors = self.each_with_index.each_with_object([]) do |(part, idx), errors|
    sub_context = if part.name
      self.class.generate_subcontext(context, part.name)
    else
      context + ["at(#{idx})"]
    end

    errors.push *part.validate(sub_context)
  end

  self.class.attributes.each do |name, attribute|
    payload_attribute = attribute.options[:payload_attribute]
    next unless payload_attribute.options[:required]
    next if self.part(name)

    sub_context = self.class.generate_subcontext(context, name)
    errors.push *payload_attribute.validate_missing_value(sub_context)
  end

  errors
end