Sha256: ef4a03257f51eaa83945225c84a75543626b5ac17368110834b0aef442529223

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require "render/attribute"

module Render
  class ArrayAttribute < Attribute
    FAUX_DATA_UPPER_LIMIT = 5.freeze

    attr_accessor :archetype

    def initialize(options = {})
      super

      self.name = options.fetch(:title, :render_array_attribute_untitled).to_sym
      options = options.fetch(:items)
      self.type = Render.parse_type(options[:type])
      self.format = Render.parse_type(options[:format]) rescue nil
      self.enums = options[:enum]

      if options.keys.include?(:properties)
        self.schema = Schema.new(options)
      else
        self.archetype = true
      end
    end

    def serialize(explicit_values = nil)
      explicit_values = faux_array_data if (Render.live == false && explicit_values.nil?)
      if archetype
        explicit_values.collect do |value|
          value || default_value
        end
      else
        explicit_values.collect do |value|
          schema.serialize!(value)
        end
      end
    end

    private

    def faux_array_data
      rand(lower_limit..FAUX_DATA_UPPER_LIMIT).times.collect do
        archetype ? nil : {}
      end
    end

    def lower_limit
      required ? 1 : 0
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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