class Eapi::TypeChecker

Attributes

allow_raw[R]
given_type[R]

Public Class Methods

constant_for_type(type) click to toggle source
# File lib/eapi/type_checker.rb, line 4
def self.constant_for_type(type)
  if type.kind_of? Module
    type
  else
    begin
      type.to_s.constantize
    rescue NameError
      nil
    end
  end
end
new(given_type, allow_raw = false) click to toggle source
# File lib/eapi/type_checker.rb, line 18
def initialize(given_type, allow_raw = false)
  @given_type = given_type
  @allow_raw  = allow_raw
end

Public Instance Methods

is_valid_type?(value) click to toggle source
# File lib/eapi/type_checker.rb, line 23
def is_valid_type?(value)
  value.nil? || valid_raw?(value) || is_same_type?(value) || poses_as_type?(value)
end

Private Instance Methods

allow_raw?() click to toggle source
# File lib/eapi/type_checker.rb, line 54
def allow_raw?
  allow_raw
end
is_same_type?(value) click to toggle source
# File lib/eapi/type_checker.rb, line 34
def is_same_type?(value)
  value.kind_of?(type_class) if type_class.present?
end
load_type_class() click to toggle source
# File lib/eapi/type_checker.rb, line 50
def load_type_class
  Eapi::TypeChecker.constant_for_type given_type
end
poses_as_type?(value) click to toggle source
# File lib/eapi/type_checker.rb, line 38
def poses_as_type?(value)
  value.respond_to?(:is?) && value.is?(type)
end
type() click to toggle source
# File lib/eapi/type_checker.rb, line 42
def type
  given_type
end
type_class() click to toggle source
# File lib/eapi/type_checker.rb, line 46
def type_class
  @type_class ||= load_type_class
end
valid_raw?(value) click to toggle source
# File lib/eapi/type_checker.rb, line 28
def valid_raw?(value)
  return false unless allow_raw?

  value.kind_of?(Array) || value.kind_of?(Hash)
end