module Yema class Rule class StrongType < self attr_reader :type, :strict, :member_type def initialize(attribute_name, options={}) super extract_options(options) end def self.required_options [:type] end def matches?(value) return allow_nil_rule(value) if strict == :allow_nil if type == TrueClass || type == FalseClass boolean_matches?(value) elsif value.kind_of?(Array) array_matches?(value) else value.kind_of?(type) end end private def allow_nil_rule(value) return true if value.nil? value.kind_of?(type) end def extract_options(options) @type = options.fetch(:type) @member_type = options.fetch(:member_type, nil) @strict = options.fetch(:strict, :high) assert_options_value end def assert_options_value raise InvalidOptionError, "Strict option only accept [:high, :allow_nil]" unless [:high, :allow_nil].include?(strict) end def boolean_matches?(value) value.equal?(true) || value.equal?(false) end def array_matches?(value) if member_type value.kind_of?(Array) && value.all? { |v| v.kind_of?(member_type) } else value.kind_of?(Array) end end end end end