Class: Render::Schema

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

Constant Summary collapse

DEFAULT_TITLE =
"untitled".freeze
CONTAINER_KEYWORDS =
%w(items properties).freeze
ROOT_POINTER =
"#".freeze
POINTER_SEPARATOR =
%r{\/}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_or_title) ⇒ Schema

Returns a new instance of Schema.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/render/schema.rb', line 22

def initialize(definition_or_title)
  Render.logger.debug("Loading #{definition_or_title}")

  process_definition!(definition_or_title)
  interpolate_refs!(definition)

  self.title = definition.fetch(:title, DEFAULT_TITLE)
  self.id = Definition.parse_id(definition)
  self.type = Type.parse(definition[:type]) || Object

  if array_schema?
    self.array_attribute = ArrayAttribute.new(definition)
  else
    self.hash_attributes = definition.fetch(:properties).collect do |name, attribute_definition|
      HashAttribute.new({ name => attribute_definition })
    end
    require_attributes!
  end
end

Instance Attribute Details

#array_attributeObject

Returns the value of attribute array_attribute.



15
16
17
# File 'lib/render/schema.rb', line 15

def array_attribute
  @array_attribute
end

#definitionObject

Returns the value of attribute definition.



15
16
17
# File 'lib/render/schema.rb', line 15

def definition
  @definition
end

#hash_attributesObject

Returns the value of attribute hash_attributes.



15
16
17
# File 'lib/render/schema.rb', line 15

def hash_attributes
  @hash_attributes
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/render/schema.rb', line 15

def id
  @id
end

#titleObject

Returns the value of attribute title.



15
16
17
# File 'lib/render/schema.rb', line 15

def title
  @title
end

#typeObject

Returns the value of attribute type.



15
16
17
# File 'lib/render/schema.rb', line 15

def type
  @type
end

Instance Method Details

#attributesObject



62
63
64
# File 'lib/render/schema.rb', line 62

def attributes
  array_schema? ? array_attribute : hash_attributes
end

#render!(explicit_data = nil, endpoint = nil) ⇒ Object



56
57
58
59
60
# File 'lib/render/schema.rb', line 56

def render!(explicit_data = nil, endpoint = nil)
  raw_data = Render.live ? request(endpoint) : explicit_data
  data = serialize!(raw_data)
  data.is_a?(Array) ? data : Extensions::DottableHash.new(data)
end

#serialize!(explicit_data = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/render/schema.rb', line 42

def serialize!(explicit_data = nil)
  if (type == Array)
    array_attribute.serialize(explicit_data)
  else
    explicit_data ||= {}
    hash_attributes.inject({}) do |processed_explicit_data, attribute|
      value = explicit_data.fetch(attribute.name, nil)
      maintain_nil = explicit_data.has_key?(attribute.name)
      serialized_attribute = attribute.serialize(value, maintain_nil)
      processed_explicit_data.merge!(serialized_attribute)
    end
  end
end