Sha256: 36e2c0ce20e91f5956930f26291abf6b4ad26908f5342e6f4a74ebaa8c7158f8

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# An Attribute represents a specific key and value as part of a Schema.
# It is responsible for casting its value and generating sample data.

require "uuid"

module Render
  class Attribute
    SCHEMA_IDENTIFIERS = [:properties, :items].freeze

    attr_accessor :name,
      :type,
      :schema,
      :enums,
      :format,
      :required

    def initialize(options = {})
      Render.logger.debug("Initializing attribute #{options}")
      self.required = false
    end

    def generator
      @generator ||= Render.generators.detect do |generator|
        generator.type == type && name.match(generator.matcher)
      end
    end

    def bias_type
      format || type
    end

    def default_value
      Render.live ? nil : faux_value
    end

    def nested_schema?(options = {})
      options.any? { |name, value| SCHEMA_IDENTIFIERS.include?(name) }
    end

    private

    def faux_value
      return enums.sample if enums
      return generator.algorithm.call if generator

      case(bias_type.name)
      when("String") then "#{name} (generated)"
      when("Integer") then rand(1000)
      when("UUID") then UUID.generate
      when("Boolean") then [true, false].sample
      when("Float") then rand(0.1..99).round(2)
      when("Time")
        time = Time.now
        (type == String) ? time.to_s : time
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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