Class: Render::Schema

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_or_title) ⇒ Schema

The schema need to know where its getting a value from an Attribute, e.g. { foo: “bar” } => { foo: { type: String } } an Archetype, e.g. [1,2,3] => { type: Integer } # could this be a pass-through? an Attribute-Schema, e.g. { foo: { bar: “baz” } } => { foo: { type: Object, attributes: { bar: { type: String } } } an Attribute-Array, e.g. [{ foo: “bar” }] => { type: Array, elements: { type: Object, attributes: { foo: { type: String } } } } and we need to identify when given { ids: [1,2] }, parental_mapping { ids: id } means to make 2 calls



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

def initialize(schema_or_title)
  self.schema = schema_or_title.is_a?(Hash) ? schema_or_title : find_schema(schema_or_title)
  self.title = schema[:title]
  self.type = Render.parse_type(schema[:type])

  if array_of_schemas?(schema[:elements])
    self.attributes = [Attribute.new({ elements: schema[:elements] })]
  else
    definitions = schema[:attributes] || schema[:elements]
    self.attributes = definitions.collect do |key, value|
      Attribute.new({ key => value })
    end
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#schemaObject

Returns the value of attribute schema.



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

def schema
  @schema
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#array_of_schemas?(definition = {}) ⇒ Boolean

Returns:



37
38
39
40
# File 'lib/render/schema.rb', line 37

def array_of_schemas?(definition = {})
  return false unless definition
  definition.keys.include?(:attributes)
end

#render(options = {}) ⇒ Object



42
43
44
45
46
# File 'lib/render/schema.rb', line 42

def render(options = {})
  endpoint = options.delete(:endpoint)
  data = Render.live ? request(endpoint) : options
  { title.to_sym => serialize(data) }
end

#serialize(data) ⇒ Object



48
49
50
51
# File 'lib/render/schema.rb', line 48

def serialize(data)
  # data.is_a?(Array) ? to_array(data) : to_hash(data)
  (type == Array) ? to_array(data) : to_hash(data)
end