Class: Render::Attribute
- Inherits:
-
Object
- Object
- Render::Attribute
- Defined in:
- lib/render/attribute.rb
Instance Attribute Summary collapse
-
#archetype ⇒ Object
Returns the value of attribute archetype.
-
#enums ⇒ Object
Returns the value of attribute enums.
-
#name ⇒ Object
Returns the value of attribute name.
-
#schema ⇒ Object
Returns the value of attribute schema.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #default_value ⇒ Object
-
#initialize(options = {}) ⇒ Attribute
constructor
Initialize take a few different Hashes { name: { type: UUID } } for standard Hashes to be aligned { type: UUID } for elements in an array to be parsed { name: { type: Object, attributes { … } } for nested schemas.
- #initialize_as_archetype(options) ⇒ Object
- #initialize_schema!(options) ⇒ Object
- #schema_value?(options = {}) ⇒ Boolean
- #serialize(explicit_value) ⇒ Object
- #to_hash(explicit_value = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Attribute
Initialize take a few different Hashes { name: { type: UUID } } for standard Hashes to be aligned { type: UUID } for elements in an array to be parsed { name: { type: Object, attributes { … } } for nested schemas
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/render/attribute.rb', line 14 def initialize( = {}) self.name = .keys.first if (.keys.first == :type && ![.keys.first].is_a?(Hash)) # todo there has to be a better way to do this initialize_as_archetype() else self.type = Render.parse_type([name][:type]) self.enums = [name][:enum] initialize_schema!() if schema_value?() end end |
Instance Attribute Details
#archetype ⇒ Object
Returns the value of attribute archetype.
8 9 10 |
# File 'lib/render/attribute.rb', line 8 def archetype @archetype end |
#enums ⇒ Object
Returns the value of attribute enums.
8 9 10 |
# File 'lib/render/attribute.rb', line 8 def enums @enums end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/render/attribute.rb', line 8 def name @name end |
#schema ⇒ Object
Returns the value of attribute schema.
8 9 10 |
# File 'lib/render/attribute.rb', line 8 def schema @schema end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/render/attribute.rb', line 8 def type @type end |
Instance Method Details
#default_value ⇒ Object
66 67 68 |
# File 'lib/render/attribute.rb', line 66 def default_value Render.live ? nil : faux_value end |
#initialize_as_archetype(options) ⇒ Object
25 26 27 28 29 |
# File 'lib/render/attribute.rb', line 25 def initialize_as_archetype() self.type = Render.parse_type([:type]) self.enums = [:enum] self.archetype = true end |
#initialize_schema!(options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/render/attribute.rb', line 31 def initialize_schema!() = { title: name, type: type } definition = [name] if definition.keys.include?(:attributes) .merge!({ attributes: definition[:attributes] }) else .merge!({ elements: definition[:elements] }) end self.schema = Schema.new() end |
#schema_value?(options = {}) ⇒ Boolean
70 71 72 73 |
# File 'lib/render/attribute.rb', line 70 def schema_value?( = {}) return true if schema [name].is_a?(Hash) && ([name][:attributes] || [name][:elements]) end |
#serialize(explicit_value) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/render/attribute.rb', line 47 def serialize(explicit_value) if archetype explicit_value || default_value else to_hash(explicit_value) end end |
#to_hash(explicit_value = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/render/attribute.rb', line 55 def to_hash(explicit_value = nil) value = if schema_value? schema.serialize(explicit_value) else # TODO guarantee type for explicit value (explicit_value || default_value) end { name.to_sym => value } end |