Sha256: 8dfe0a9ef825fcd0c465f0798a43685b3498572d32a274592a807f36b8de0141
Contents?: true
Size: 1.8 KB
Versions: 3
Compression:
Stored size: 1.8 KB
Contents
class SchemaSerializer class Schema attr_reader :key, :type, :nullable, :items, :required, :properties def initialize(key, hash = {}) @key = key @type = hash["type"] @nullable = !hash["nullable"].nil? case type when "array" @items = self.class.new(@key, hash.fetch("items")) when "object", nil @required = hash["required"] || [] @properties = hash.fetch("properties").each_with_object({}) { |(column, property), obj| obj[column] = self.class.new(column, property) } end end def serialize(object) if object.nil? return nil if nullable raise NullValue, "#{key} is not allowed to be null" if SchemaSerializer.config.raise_on_null end case type when "integer" object.to_i when "number" object.to_f when "string" object.to_s when "boolean" !!object when "array" serialize_array(object) else serialize_object(object) end end private def serialize_array(object) object.map { |item| items.serialize(item) } end def serialize_object(object) not_enough_columns = required - properties.keys raise RequiredNotDefined, not_enough_columns.join(", ") unless not_enough_columns.empty? properties.each_with_object({}) { |(column, schema), obj| obj[column] = schema.serialize(get_value(object, column)) } end def get_value(object, column) return object[column] || object[column.to_sym] if object.is_a?(Hash) value = object.public_send(column) return value if !object.respond_to?(:defined_enums) || !object.defined_enums.has_key?(column) object.defined_enums[column][value] end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
schema_serializer-0.2.7 | lib/schema_serializer/schema.rb |
schema_serializer-0.2.6 | lib/schema_serializer/schema.rb |
schema_serializer-0.2.4 | lib/schema_serializer/schema.rb |