Class: Render::Schema

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

Constant Summary collapse

DEFAULT_TITLE =
"untitled".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
# File 'lib/render/schema.rb', line 22

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

  self.definition = determine_definition(definition_or_title)
  title_or_default = definition.fetch(:title, DEFAULT_TITLE)
  self.title = title_or_default.to_sym
  self.type = Type.parse(definition[:type]) || Object
  self.universal_title = definition.fetch(:universal_title, nil)

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

Instance Attribute Details

#array_attributeObject

Returns the value of attribute array_attribute.



12
13
14
# File 'lib/render/schema.rb', line 12

def array_attribute
  @array_attribute
end

#definitionObject

Returns the value of attribute definition.



12
13
14
# File 'lib/render/schema.rb', line 12

def definition
  @definition
end

#hash_attributesObject

Returns the value of attribute hash_attributes.



12
13
14
# File 'lib/render/schema.rb', line 12

def hash_attributes
  @hash_attributes
end

#raw_dataObject

Returns the value of attribute raw_data.



12
13
14
# File 'lib/render/schema.rb', line 12

def raw_data
  @raw_data
end

#rendered_dataObject

Returns the value of attribute rendered_data.



12
13
14
# File 'lib/render/schema.rb', line 12

def rendered_data
  @rendered_data
end

#serialized_dataObject

Returns the value of attribute serialized_data.



12
13
14
# File 'lib/render/schema.rb', line 12

def serialized_data
  @serialized_data
end

#titleObject

Returns the value of attribute title.



12
13
14
# File 'lib/render/schema.rb', line 12

def title
  @title
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/render/schema.rb', line 12

def type
  @type
end

#universal_titleObject

Returns the value of attribute universal_title.



12
13
14
# File 'lib/render/schema.rb', line 12

def universal_title
  @universal_title
end

Instance Method Details

#render!(explicit_data = nil, endpoint = nil) {|serialized_data| ... } ⇒ Object

Yields:



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

def render!(explicit_data = nil, endpoint = nil)
  self.raw_data = Render.live ? request(endpoint) : explicit_data
  serialize!(raw_data)
  yield serialized_data if block_given?
  self.rendered_data = Extensions::DottableHash.new(hash_with_title_prefixes(serialized_data))
end

#serialize!(explicit_data = nil) ⇒ Object



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

def serialize!(explicit_data = nil)
  if (type == Array)
    self.serialized_data = array_attribute.serialize(explicit_data)
  else
    self.serialized_data = hash_attributes.inject({}) do |processed_explicit_data, attribute|
      explicit_data ||= {}
      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