Method: DataModel::Struct#initialize

Defined in:
lib/data_model/struct.rb

#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