Class: Rulix::Validators::FormatValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rulix/validators/format_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ FormatValidator

Returns a new instance of FormatValidator.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rulix/validators/format_validator.rb', line 6

def initialize options = nil
  case options
  when Regexp
    self.pattern = options
  when Hash
    self.pattern = options[:pattern]
    self.message = options[:message]
  else
    options ||= {}
  end

  self.message ||= 'does not match format'
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/rulix/validators/format_validator.rb', line 4

def message
  @message
end

#patternObject

Returns the value of attribute pattern.



4
5
6
# File 'lib/rulix/validators/format_validator.rb', line 4

def pattern
  @pattern
end

Instance Method Details

#call(string) ⇒ Object



20
21
22
# File 'lib/rulix/validators/format_validator.rb', line 20

def call string
  (string && pattern === string) || [false, message]
end

#to_procObject



24
25
26
# File 'lib/rulix/validators/format_validator.rb', line 24

def to_proc
  method(:call)
end