Sha256: e9db7414eff294504cfd71cb97b63955af02911d36eaafb4a7dbf783564b19f4

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 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[: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
      # lower_limit = (required ? 1 : 0)
      1
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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