Class: CPS::Validator

Inherits:
Struct
  • Object
show all
Defined in:
lib/cps-client/validator.rb

Overview

CPS::Validator.load d = CPS::Validator.find(:domain) d.name => :domain d.valid?(“qutic.com”) => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#maxObject

Returns the value of attribute max

Returns:

  • (Object)

    the current value of max



8
9
10
# File 'lib/cps-client/validator.rb', line 8

def max
  @max
end

#minObject

Returns the value of attribute min

Returns:

  • (Object)

    the current value of min



8
9
10
# File 'lib/cps-client/validator.rb', line 8

def min
  @min
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



8
9
10
# File 'lib/cps-client/validator.rb', line 8

def name
  @name
end

#regexpObject

Returns the value of attribute regexp

Returns:

  • (Object)

    the current value of regexp



8
9
10
# File 'lib/cps-client/validator.rb', line 8

def regexp
  @regexp
end

Class Method Details

.find(key) ⇒ Object



23
24
25
# File 'lib/cps-client/validator.rb', line 23

def self.find(key)
  @@all[key]
end

.loadObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cps-client/validator.rb', line 11

def self.load
  return @@all if @@all.present?

  data_file = File.join(File.dirname(__FILE__), '..', '..', 'data', 'validation.yml')

  @@all = {}
  YAML.load(File.read(data_file)).each_pair do |key, c|
    @@all[key.to_sym] = Validator.new(key.to_sym, c[:regexp], c[:min], c[:max])
  end
  @@all
end

Instance Method Details

#to_sObject



27
28
29
# File 'lib/cps-client/validator.rb', line 27

def to_s
  name.to_s
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/cps-client/validator.rb', line 31

def valid?(value)
  !value.nil? &&
  value.length >= self.min &&
  value.length <= self.max &&
  !regexp.match(value).nil?
end