File: matches.rb

package info (click to toggle)
ruby-valid 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: ruby: 309; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 807 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Validation
  module Rule
    # Matches rule
    class Matches
      attr_writer :obj

      # This class should take the field to match with in the constructor:
      #
      # rule = Validation::Rule::Matches(:password)
      # rule.obj = OpenStruct.new(:password => 'foo')
      # rule.valid_value?('foo')
      def initialize(matcher_field)
        @matcher_field = matcher_field
      end

      # The error key for this rule
      def error_key
        :matches
      end

      # Params is the matcher_field given in the constructor
      def params
        @matcher_field
      end

      # Determines if value matches the field given in the constructor
      def valid_value?(value)
        matcher_value = @obj.send(@matcher_field)
        matcher_value == value
      end
    end
  end
end