Sha256: 116cd3265156ab6ba6e945f70eb0ea77f84b825c734b297828be29943e9eda34
Contents?: true
Size: 1.37 KB
Versions: 2
Compression:
Stored size: 1.37 KB
Contents
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
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
yema-0.0.2 | lib/yema/rule/strong_type.rb |
yema-0.0.1 | lib/yema/rule/strong_type.rb |