Class: DataModel::Struct

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

Overview

A struct that has typed properties, which will raise when the wrong type is assigned

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, coerce: false, strict: true) ⇒ void

Parameters:

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

    the data to initialize the struct with

  • coerce (Boolean) (defaults to: false)

    whether to coerce the data if it is not correctly typed

  • strict (Boolean) (defaults to: true)

    whether to raise if a required field is missing



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/data_model/struct.rb', line 85

def initialize(data = {}, coerce: false, strict: true)
  @coerce = coerce
  @strict = strict
  @values = {}
  @errors = Error.new

  if data.nil?
    return
  end

  for k, v in data
    send("#{k}=", v)
  end

  for req in self.class.required_types
    for key in data.keys
      if key.to_s == req.to_s
        next
      end
    end
    
    raise "Missing required field #{req}"
  end
end

Class Attribute Details

.required_typesArray<Symbol> (readonly)

required types configured by the prop macro

Returns:

  • (Array<Symbol>)

    the required types configured



11
12
13
# File 'lib/data_model/struct.rb', line 11

def required_types
  @required_types
end

.typesHash (readonly)

types configured by the prop macro

Returns:

  • (Hash)

    the types configured



7
8
9
# File 'lib/data_model/struct.rb', line 7

def types
  @types
end

Instance Attribute Details

#errorsError (readonly)

Returns the errors for this struct.

Returns:

  • (Error)

    the errors for this struct



79
80
81
# File 'lib/data_model/struct.rb', line 79

def errors
  @errors
end

Class Method Details

.prop(name, schema, opts = {}) ⇒ void

This method returns an undefined value.

Define a property on the struct.

Parameters:

  • name (Symbol)

    the name of the property

  • schema (Array)

    the schema to define

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

    a customizable set of options

Options Hash (opts):

  • :default (Boolean)

    the default value for the property



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/data_model/struct.rb', line 19

def self.prop(name, schema, opts = {})
  has_default = opts.key?(:default)
  default = opts[:default]

  # ensure values are initialized
  @types ||= {}
  @required_types ||= []

  # if a prop does not have a default, it is required
  if !has_default
    @required_types << name
  end

  # store the data model for the prop
  schema = Array(schema)
  @types[name] = DataModel.define(schema)

  # field getter
  define_method(name) do
    @values ||= {}
    types = self.class.types

    if !types.key?(name)
      raise "No prop configured for #{name}"
    end

    if !@values.key?(name) && has_default
      return default
    end

    return @values[name]
  end

  # field setter
  define_method("#{name}=") do |val|
    @values ||= {}
    types = self.class.types

    if !types.key?(name)
      raise "No prop configured for #{name}"
    end

    model = types.fetch(name)

    if @coerce
      val, err = model.coerce(val)
    else
      err = model.validate(val)
    end

    if @strict && err.any?
      errors.merge_child(prop, err)
      raise "Invalid value for #{name}: #{err.to_messages.inspect}"
    end

    @values[name] = val
  end
end

Instance Method Details

#to_hashHash

convert struct to a hash

Returns:

  • (Hash)

    the struct as a hash



112
113
114
# File 'lib/data_model/struct.rb', line 112

def to_hash
  @values
end