Class: Yema::Rule::StrongType

Inherits:
Yema::Rule show all
Defined in:
lib/yema/rule/strong_type.rb

Instance Attribute Summary collapse

Attributes inherited from Yema::Rule

#attribute_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Yema::Rule

#errors

Constructor Details

#initialize(attribute_name, options = {}) ⇒ StrongType

Returns a new instance of StrongType.



7
8
9
10
# File 'lib/yema/rule/strong_type.rb', line 7

def initialize(attribute_name, options={})
  super
  extract_options(options)
end

Instance Attribute Details

#member_typeObject (readonly)

Returns the value of attribute member_type.



5
6
7
# File 'lib/yema/rule/strong_type.rb', line 5

def member_type
  @member_type
end

#strictObject (readonly)

Returns the value of attribute strict.



5
6
7
# File 'lib/yema/rule/strong_type.rb', line 5

def strict
  @strict
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/yema/rule/strong_type.rb', line 5

def type
  @type
end

Class Method Details

.required_optionsObject



12
13
14
# File 'lib/yema/rule/strong_type.rb', line 12

def self.required_options
  [:type]
end

Instance Method Details

#matches?(value) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/yema/rule/strong_type.rb', line 16

def matches?(value)
  raise ArgumentError if member_type && !value.respond_to?(:all?)

  return true if (strict == :allow_nil && value.nil?)
  Utils.type_matches?(value, type) && member_type_matches?(value)
end