Class: Yema::Rule::Length

Inherits:
Yema::Rule show all
Defined in:
lib/yema/rule/length.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 = {}) ⇒ Length

Returns a new instance of Length.



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

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

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

Class Method Details

.required_optionsObject



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

def self.required_options
  [:length]
end

Instance Method Details

#matches?(value) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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

def matches?(value)
  raise ArgumentError unless value.respond_to?(:length)

  case length
  when Range;  length.include?(value.length)
  when Fixnum; length == value.length
  end
end