Class: WscSdk::Schema

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

Overview

A class that manages the data schema for an SDK model.

Instance Method Summary collapse

Instance Method Details

#add_attribute(name, type, options = {}) ⇒ Object

Adds an attribute to the schema

Parameters:

  • name (Symbol)

    The name of the attribute.

  • type (Symbol)

    The type of the attribute. The type must be in the list of WscSdk::SchemaAttribute::TYPES

  • options (Hash) (defaults to: {})

    A hash of options

Options Hash (options):

  • :default (Any, Proc, Symbol)

    The default value of the attribute. This value must match the assigned type of the attribute. The default value can be determined by a direct value, a Proc or a Symbol that represents a method in the model.

    If a Proc is provided it will be passed an instance of the model, which can be used to determine the default value for the attribute. The Proc should return a value that is the same type as the attribute.

    If a Symbol is provided, the Symbol will be checked against the model to determine if there is a method available that has the same name. If one exists, that method will be passed an instance of the model, which can be used to determine the default value for the attribute. The method should return a value that is the same type as the attribute. If no method is found that matches the symbol, the symbol itself is used as the default.

  • :required (Array, Proc, Symbol)

    Determines if the attribute is required.

    If a Proc is provided it will be passed an instance of the model, which can be used to determine if the value is required. The Proc should return a boolean value.

    If a Symbol is provided, the Symbol will be checked against the model to determine if there is a method available that has the same name. If one exists, that method will be called. The method should return a boolean value.

  • :validate (Array, Proc, Symbol)

    Determines if the attribute is valid.

    If a Proc is provided it will be passed an instance of the model, which can be used to determine if the value is valid. The Proc should return a string describing why the attribute is not valid, otherwise it should return nil.

    If a Symbol is provided, the Symbol will be checked against the model to determine if there is a method available that has the same name. If one exists, that method will be passed an instance of the model, which can be used to determine if the value is valid. The Proc should return a string describing why the attribute is not valid, otherwise it should return nil.

Raises:

  • (ArgumentError)

    If the type is not in the WscSdk::SchemaAttribute::TYPES list.

  • (IndexError)

    If the attribute name is already defined in the schema.



73
74
75
76
77
# File 'lib/wsc_sdk/schema.rb', line 73

def add_attribute(name, type, options={})
  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, options)
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.

Parameters:

  • model (WscSdk::Model)

    The model to use to determine the default values in the event that they are defined as a Proc or a Symbol.



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

Parameters:

  • attribute (Symbol)

    The name of the attribute

Returns:

  • (Boolean)


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.

Parameters:

  • attribute (Symbol)

    The attribute to validate the value for.

  • model (WscSdk::Model)

    The model to transform the value for.

  • value (Any)

    The value to validate

Returns:

  • (Any)

    The transformed value.



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.

Parameters:

  • attribute (Symbol)

    The attribute to validate the value for.

  • value (Any)

    The value to validate

Returns:

  • (Boolean)

    An indication if the value is valid.



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