Class: DataModel::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/data_model/model.rb

Overview

A model is a schema and a type. It is the primary interface for interacting with the data_model gem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, type) ⇒ void

Create a new model.

Parameters:

  • schema (Array)

    the schema to define

  • type (Type)

    the type to use



9
10
11
12
# File 'lib/data_model/model.rb', line 9

def initialize(schema, type)
	@schema = schema
	@type = type
end

Instance Attribute Details

#schemaArray (readonly)

Returns the schema configured.

Returns:

  • (Array)

    the schema configured



15
16
17
# File 'lib/data_model/model.rb', line 15

def schema
  @schema
end

Instance Method Details

#coerce(data) ⇒ Array

Read data with the model. This will return a tuple of [data, error]

Parameters:

  • data (Hash)

    the data to read

Returns:

  • (Array)

    a tuple of [data, error]



29
30
31
32
# File 'lib/data_model/model.rb', line 29

def coerce(data)
	result = @type.read(data, coerce: true)
	return result
end

#validate(data) ⇒ Boolean

Validate data against the model. This will return true if the data is valid, or false if it is not. If it is not valid, it will raise an exception.

Parameters:

  • data (Hash)

    the data to validate

Returns:

  • (Boolean)

    true if the data is valid, false if it is not



21
22
23
24
# File 'lib/data_model/model.rb', line 21

def validate(data)
	_, err = @type.read(data)
	return err
end