Class: WscSdk::Schema
- Inherits:
-
Hash
- Object
- Hash
- WscSdk::Schema
- Defined in:
- lib/wsc_sdk/schema.rb
Overview
A class that manages the data schema for an SDK model.
Instance Method Summary collapse
-
#add_attribute(name, type, options = {}) ⇒ Object
Adds an attribute to the schema.
-
#defaults(model) ⇒ Object
Generates a hash of default values for the given model.
-
#has_attribute?(attribute) ⇒ Boolean
Determine if the schema has an attribute.
-
#transform_value(attribute, model, value) ⇒ Any
Transforms a value into the appropriate type.
-
#valid_type?(attribute, value) ⇒ Boolean
Determines if the value is a valid type for the attribute.
Instance Method Details
#add_attribute(name, type, options = {}) ⇒ Object
Adds an attribute to the schema
73 74 75 76 77 |
# File 'lib/wsc_sdk/schema.rb', line 73 def add_attribute(name, type, ={}) name = name.to_sym raise IndexError.new("The attribute `#{name}` is already defined in the schema") if has_key?(name) self[name] = SchemaAttribute.new(name, type, ) end |
#defaults(model) ⇒ Object
Generates a hash of default values for the given model.
This is a non-destructive process, so if the attributes are already established and have values, then nothing will happen to them.
132 133 134 135 136 137 138 |
# File 'lib/wsc_sdk/schema.rb', line 132 def defaults(model) hsh = {} each do |name, attribute| hsh[name] = attribute.default_value(model) end hsh end |
#has_attribute?(attribute) ⇒ Boolean
Determine if the schema has an attribute
119 120 121 |
# File 'lib/wsc_sdk/schema.rb', line 119 def has_attribute?(attribute) has_key?(attribute) end |
#transform_value(attribute, model, value) ⇒ Any
Transforms a value into the appropriate type.
109 110 111 112 |
# File 'lib/wsc_sdk/schema.rb', line 109 def transform_value(attribute, model, value) return nil unless has_attribute?(attribute) self[attribute].transform_value(model, value) end |
#valid_type?(attribute, value) ⇒ Boolean
Determines if the value is a valid type for the attribute.
90 91 92 93 |
# File 'lib/wsc_sdk/schema.rb', line 90 def valid_type?(attribute, value) return false unless has_attribute?(attribute) self[attribute].valid_type?(value) end |