Class: Render::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/render/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Attribute

Initialize take a few different Hashes { name: { type: UUID } } for standard Hashes to be aligned { type: UUID } for elements in an array to be parsed { name: { type: Object, attributes { … } } for nested schemas



14
15
16
17
18
19
20
21
22
23
# File 'lib/render/attribute.rb', line 14

def initialize(options = {})
  self.name = options.keys.first
  if (options.keys.first == :type && !options[options.keys.first].is_a?(Hash)) # todo there has to be a better way to do this
    initialize_as_archetype(options)
  else
    self.type = Render.parse_type(options[name][:type])
    self.enums = options[name][:enum]
    initialize_schema!(options) if schema_value?(options)
  end
end

Instance Attribute Details

#archetypeObject

Returns the value of attribute archetype.



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

def archetype
  @archetype
end

#enumsObject

Returns the value of attribute enums.



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

def enums
  @enums
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#schemaObject

Returns the value of attribute schema.



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

def schema
  @schema
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#default_valueObject



66
67
68
# File 'lib/render/attribute.rb', line 66

def default_value
  Render.live ? nil : faux_value
end

#initialize_as_archetype(options) ⇒ Object



25
26
27
28
29
# File 'lib/render/attribute.rb', line 25

def initialize_as_archetype(options)
  self.type = Render.parse_type(options[:type])
  self.enums = options[:enum]
  self.archetype = true
end

#initialize_schema!(options) ⇒ Object



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

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

  definition = options[name]
  if definition.keys.include?(:attributes)
    schema_options.merge!({ attributes: definition[:attributes] })
  else
    schema_options.merge!({ elements: definition[:elements] })
  end

  self.schema = Schema.new(schema_options)
end

#schema_value?(options = {}) ⇒ Boolean



70
71
72
73
# File 'lib/render/attribute.rb', line 70

def schema_value?(options = {})
  return true if schema
  options[name].is_a?(Hash) && (options[name][:attributes] || options[name][:elements])
end

#serialize(explicit_value) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/render/attribute.rb', line 47

def serialize(explicit_value)
  if archetype
    explicit_value || default_value
  else
    to_hash(explicit_value)
  end
end

#to_hash(explicit_value = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/render/attribute.rb', line 55

def to_hash(explicit_value = nil)
  value = if schema_value?
    schema.serialize(explicit_value)
  else
    # TODO guarantee type for explicit value
    (explicit_value || default_value)
  end

  { name.to_sym => value }
end