Class: Render::Schema
- Inherits:
-
Object
- Object
- Render::Schema
- Defined in:
- lib/render/schema.rb
Constant Summary collapse
- DEFAULT_TITLE =
"untitled".freeze
Instance Attribute Summary collapse
-
#array_attribute ⇒ Object
Returns the value of attribute array_attribute.
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#hash_attributes ⇒ Object
Returns the value of attribute hash_attributes.
-
#raw_data ⇒ Object
Returns the value of attribute raw_data.
-
#rendered_data ⇒ Object
Returns the value of attribute rendered_data.
-
#serialized_data ⇒ Object
Returns the value of attribute serialized_data.
-
#title ⇒ Object
Returns the value of attribute title.
-
#type ⇒ Object
Returns the value of attribute type.
-
#universal_title ⇒ Object
Returns the value of attribute universal_title.
Instance Method Summary collapse
-
#initialize(definition_or_title) ⇒ Schema
constructor
TODO When given { ids: [1,2] }, parental_mapping { ids: id } means to make 2 calls.
- #render!(options_and_explicit_data = nil) {|serialized_data| ... } ⇒ Object
- #serialize!(explicit_data = nil) ⇒ Object
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_attribute ⇒ Object
Returns the value of attribute array_attribute.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def array_attribute @array_attribute end |
#definition ⇒ Object
Returns the value of attribute definition.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def definition @definition end |
#hash_attributes ⇒ Object
Returns the value of attribute hash_attributes.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def hash_attributes @hash_attributes end |
#raw_data ⇒ Object
Returns the value of attribute raw_data.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def raw_data @raw_data end |
#rendered_data ⇒ Object
Returns the value of attribute rendered_data.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def rendered_data @rendered_data end |
#serialized_data ⇒ Object
Returns the value of attribute serialized_data.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def serialized_data @serialized_data end |
#title ⇒ Object
Returns the value of attribute title.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def title @title end |
#type ⇒ Object
Returns the value of attribute type.
16 17 18 |
# File 'lib/render/schema.rb', line 16 def type @type end |
#universal_title ⇒ Object
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
58 59 60 61 62 63 64 |
# File 'lib/render/schema.rb', line 58 def render!( = nil) endpoint = .delete(:endpoint) if .is_a?(Hash) self.raw_data = Render.live ? request(endpoint) : 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 |