Sha256: 674a48e883041531b3f8bd0dc6472e4ffaf8983f70cfa4e61d84a98062e21b5b

Contents?: true

Size: 896 Bytes

Versions: 1

Compression:

Stored size: 896 Bytes

Contents

module DataModel
	module Type
		extend self

		def string
			{
				read: lambda do |val|
					err = {}

					unless val.is_a? String
						err[:root] = "#{val} is not a string, it is a #{val.class.name}"
					end

					[val, err]
				end
			}
		end

		def hash
			{
				read: lambda do |val, ctx|
					err = {}
					ctx => {config:, children:}

					unless val.is_a? Hash
						err[:root] = "#{val} is not a hash, it is a #{val.class.name}"
						return [val, err]
					end

					# TODO: this needs to be wayyyy better
					if !config[:open?] && (val.length > children.length)
						err[:root] = "more elements found in closed hash then specified children"
					end

					children.each do |child|
						child => {property:}
						v, e = Model.read(child, val.fetch(property))

						val = val.merge({ property => v })
						err[property] = e
					end

					[val, err]
				end
			}
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_model-0.0.1 lib/data_model/type.rb