Sha256: 0e9d064973f0ca74e77dc80ded62462fe21b272b00f7d499d0af2805f821fdfd

Contents?: true

Size: 1002 Bytes

Versions: 1

Compression:

Stored size: 1002 Bytes

Contents

require "render"
require "render/attribute"

module Render
  class HashAttribute < Attribute
    def initialize(options = {})
      super

      self.name = options.keys.first
      options = options[name]
      self.type = Render.parse_type(options[:type])
      self.format = Render.parse_type(options[:format]) rescue nil
      self.enums = options[:enum]

      initialize_schema!(options) if nested_schema?(options)
    end

    def initialize_schema!(options)
      schema_options = {
        title: name,
        type: bias_type
      }

      self.schema = Schema.new(schema_options.merge(options))
    end

    def serialize(explicit_value, maintain_nil = false)
      if !!schema
        value = schema.serialize!(explicit_value)
        { name.to_sym => value }
      else
        if (maintain_nil && !explicit_value)
          value = explicit_value
        else
          value = (explicit_value || default_value)
        end

        { name.to_sym => value }
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
render-0.0.4 lib/render/hash_attribute.rb