Class: DataModel::Struct
- Inherits:
-
Object
- Object
- DataModel::Struct
- Defined in:
- lib/data_model/struct.rb
Overview
A struct that has typed properties, which will raise when the wrong type is assigned
Direct Known Subclasses
Builtin::Array::Arguments, Builtin::BigDecimal::Arguments, Builtin::Boolean::Arguments, Builtin::Date::Arguments, Builtin::Float::Arguments, Builtin::Hash::Arguments, Builtin::Integer::Arguments, Builtin::Numeric::Arguments, Builtin::Object::Arguments, Builtin::Or::Arguments, Builtin::String::Arguments, Builtin::Symbol::Arguments, Builtin::Time::Arguments
Class Attribute Summary collapse
-
.required_types ⇒ Array<Symbol>
readonly
required types configured by the prop macro.
-
.types ⇒ Hash
readonly
types configured by the prop macro.
Instance Attribute Summary collapse
-
#errors ⇒ Error
readonly
The errors for this struct.
Class Method Summary collapse
-
.prop(name, schema, opts = {}) ⇒ void
Define a property on the struct.
Instance Method Summary collapse
- #initialize(data = {}, coerce: false, strict: true) ⇒ void constructor
-
#to_hash ⇒ Hash
convert struct to a hash.
Constructor Details
#initialize(data = {}, coerce: false, strict: true) ⇒ void
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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 required_types = self.class.required_types found = [] for req in required_types for key in data.keys if key.to_s == req.to_s found << req break end end end found = required_types - found if !found.empty? raise "Missing required fields: #{found.join(', ')}" end end |
Class Attribute Details
.required_types ⇒ Array<Symbol> (readonly)
required types configured by the prop macro
11 12 13 |
# File 'lib/data_model/struct.rb', line 11 def required_types @required_types end |
.types ⇒ Hash (readonly)
types configured by the prop macro
7 8 9 |
# File 'lib/data_model/struct.rb', line 7 def types @types end |
Instance Attribute Details
#errors ⇒ Error (readonly)
Returns 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.
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..inspect}" end @values[name] = val end end |
Instance Method Details
#to_hash ⇒ Hash
convert struct to a hash
119 120 121 |
# File 'lib/data_model/struct.rb', line 119 def to_hash @values end |