Class: DataModel::Model
- Inherits:
-
Object
- Object
- DataModel::Model
- 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
-
#schema ⇒ Array
readonly
The schema configured.
Instance Method Summary collapse
-
#coerce(data) ⇒ Array
Read data with the model.
-
#initialize(schema, type) ⇒ void
constructor
Create a new model.
-
#validate(data) ⇒ Boolean
Validate data against the model.
Constructor Details
#initialize(schema, type) ⇒ void
Create a new model.
9 10 11 12 |
# File 'lib/data_model/model.rb', line 9 def initialize(schema, type) @schema = schema @type = type end |
Instance Attribute Details
#schema ⇒ Array (readonly)
Returns 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]
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.
21 22 23 24 |
# File 'lib/data_model/model.rb', line 21 def validate(data) _, err = @type.read(data) return err end |