Sha256: 3217159feda30dd6b41b56d94340553d1ba158a811cf46a7d6eb37023f8b4322

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

require "render/attributes/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
      self.required = options.fetch(:required, nil)
      options = options.fetch(:items)
      process_type!(options)

      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

4 entries across 4 versions & 1 rubygems

Version Path
render-0.0.8 lib/render/attributes/array_attribute.rb
render-0.0.7 lib/render/attributes/array_attribute.rb
render-0.0.6 lib/render/attributes/array_attribute.rb
render-0.0.5 lib/render/attributes/array_attribute.rb