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

TODO When given { ids: [1,2] }, parental_mapping { ids: id } means to make 2 calls



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/render/schema.rb', line 27

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 = Render.parse_type(definition[:type])
  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.



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

def array_attribute
  @array_attribute
end

#definitionObject

Returns the value of attribute definition.



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

def definition
  @definition
end

#hash_attributesObject

Returns the value of attribute hash_attributes.



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

def hash_attributes
  @hash_attributes
end

#raw_dataObject

Returns the value of attribute raw_data.



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

def raw_data
  @raw_data
end

#rendered_dataObject

Returns the value of attribute rendered_data.



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

def rendered_data
  @rendered_data
end

#serialized_dataObject

Returns the value of attribute serialized_data.



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

def serialized_data
  @serialized_data
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#universal_titleObject

Returns the value of attribute universal_title.



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

def universal_title
  @universal_title
end

Instance Method Details

#render!(options_and_explicit_data = nil) {|serialized_data| ... } ⇒ Object

Yields:



58
59
60
61
62
63
64
# File 'lib/render/schema.rb', line 58

def render!(options_and_explicit_data = nil)
  endpoint = options_and_explicit_data.delete(:endpoint) if options_and_explicit_data.is_a?(Hash)
  self.raw_data = Render.live ? request(endpoint) : options_and_explicit_data
  serialize!(raw_data)
  yield serialized_data if block_given?
  self.rendered_data = DottableHash.new(hash_with_title_prefixes(serialized_data))
end

#serialize!(explicit_data = nil) ⇒ Object



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

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)
      serialized_attribute = attribute.serialize(value)
      processed_explicit_data.merge!(serialized_attribute)
    end
  end
end