Class: Render::ArrayAttribute

Inherits:
Attribute show all
Defined in:
lib/render/attributes/array_attribute.rb

Constant Summary collapse

FAUX_DATA_UPPER_LIMIT =
5.freeze
DEFAULT_NAME =
:render_array_attribute_untitled

Constants inherited from Attribute

Render::Attribute::SCHEMA_IDENTIFIERS

Instance Attribute Summary collapse

Attributes inherited from Attribute

#default, #enums, #exclusive_maximum, #exclusive_minimum, #format, #max_length, #maximum, #min_length, #minimum, #multiple_of, #name, #schema, #types

Instance Method Summary collapse

Methods inherited from Attribute

#bias_type, #default_value, #nested_schema?

Constructor Details

#initialize(options = {}) ⇒ ArrayAttribute

Returns a new instance of ArrayAttribute.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/render/attributes/array_attribute.rb', line 13

def initialize(options = {})
  super

  self.name = options.fetch(:title, DEFAULT_NAME).to_sym
  self.min_items = options[:minItems] || 0
  self.max_items = options[:maxItems]
  self.unique = !!options[:uniqueItems]

  options = options.fetch(:items)
  process_options!(options)

  if options.keys.include?(:properties)
    self.schema = Schema.new(options)
  else
    self.simple = true
  end
end

Instance Attribute Details

#max_itemsObject

Returns the value of attribute max_items.



8
9
10
# File 'lib/render/attributes/array_attribute.rb', line 8

def max_items
  @max_items
end

#min_itemsObject

Returns the value of attribute min_items.



8
9
10
# File 'lib/render/attributes/array_attribute.rb', line 8

def min_items
  @min_items
end

#simpleObject

Returns the value of attribute simple.



8
9
10
# File 'lib/render/attributes/array_attribute.rb', line 8

def simple
  @simple
end

#uniqueObject

Returns the value of attribute unique.



8
9
10
# File 'lib/render/attributes/array_attribute.rb', line 8

def unique
  @unique
end

Instance Method Details

#serialize(explicit_values = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/render/attributes/array_attribute.rb', line 31

def serialize(explicit_values = nil)
  explicit_values = faux_array_data if (Render.live == false && explicit_values.nil?)
  values = if simple
    explicit_values.collect do |value|
      value = (value || default_value)
      Type.to(types, value)
    end
  else
    explicit_values.collect do |value|
      schema.serialize!(value)
    end
  end

  unique ? values.uniq : values
end