Sha256: 5f2b5d74b35220961e5a936fdf517a5940fd9bb08b9ab2ed8923f94b8d211deb

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true
# author: Vadim Shaveiko <@vshaveyko>
class RailsApiDoc::Controller::Parameter::Repository::Param

  NESTED_TYPES = [:ary_object, :object, :model, Object].freeze

  STRAIGHT_TYPES = [:bool, :string, :integer, :array, :datetime, :enum, String, Object, Integer, Array, DateTime].freeze

  ACCEPTED_TYPES = (NESTED_TYPES + STRAIGHT_TYPES).freeze

  # @type - type to check
  def self.accepted_nested_type?(type)
    type.in?(NESTED_TYPES)
  end

  def self.valid_type?(type)
    return if type.nil? || type.in?(ACCEPTED_TYPES)
    raise ArgumentError, "Wrong type: #{type}. " \
                         "Correct types are: #{ACCEPTED_TYPES}."
  end

  def self.valid_enum?(type, enum)
    return false unless type == :enum
    return if enum.is_a?(Array)
    raise ArgumentError, 'Enum must be an array.'
  end

  def self.valid_nested?(type, block_given)
    return false unless accepted_nested_type?(type)
    return true if block_given
    raise ArgumentError, 'Empty object passed.'
  end

  def initialize(name, store)
    @name = name
    @store = store
  end

  def enum?
    @store[:type] == :enum && @store[:enum].present?
  end

  def ary_object?
    @store[:type] == :ary_object
  end

  def nested?
    self.class.accepted_nested_type?(@store[:type])
  end

  def nesting
    @store[:nested]
  end

  def required?
    @store[:required]
  end

  def method_missing(name, *args)
    return @store.public_send(name, *args) if respond_to_missing?(name)
    super
  end

  def respond_to_missing?(name)
    @store.respond_to?(name)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_api_documentation-0.2.0 lib/rails_api_doc/controller/parameter/repository/param.rb