Class: Render::HashAttribute

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

Constant Summary

Constants inherited from Attribute

Attribute::SCHEMA_IDENTIFIERS

Instance Attribute Summary

Attributes inherited from Attribute

#enums, #format, #name, #required, #schema, #type

Instance Method Summary collapse

Methods inherited from Attribute

#bias_type, #default_value, #nested_schema?

Constructor Details

#initialize(options = {}) ⇒ HashAttribute

Returns a new instance of HashAttribute.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/render/hash_attribute.rb', line 6

def initialize(options = {})
  super

  self.name = options.keys.first
  options = options[name]
  self.type = Render.parse_type(options[:type])
  self.format = Render.parse_type(options[:format]) rescue nil
  self.enums = options[:enum]

  initialize_schema!(options) if nested_schema?(options)
end

Instance Method Details

#initialize_schema!(options) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/render/hash_attribute.rb', line 18

def initialize_schema!(options)
  schema_options = {
    title: name,
    type: bias_type
  }

  self.schema = Schema.new(schema_options.merge(options))
end

#serialize(explicit_value) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/render/hash_attribute.rb', line 27

def serialize(explicit_value)
  if !!schema
    value = schema.serialize!(explicit_value)
    { name.to_sym => value }
  else
    value = (explicit_value || default_value)
    { name.to_sym => value }
  end
end