File: coercer.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 805 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
34
35
36
37
38
39
40
41
module Virtus

  # Abstract coercer class
  #
  class Coercer
    include Equalizer.new(inspect) << :primitive << :type

    # @api private
    attr_reader :primitive, :type

    # @api private
    def initialize(type)
      @type      = type
      @primitive = type.primitive
    end

    # Coerce input value into expected primitive type
    #
    # @param [Object] input
    #
    # @return [Object] coerced input
    #
    # @api public
    def call(input)
      NotImplementedError.new("#{self.class}#call must be implemented")
    end

    # Return if the input value was successfuly coerced
    #
    # @param [Object] input
    #
    # @return [Object] coerced input
    #
    # @api public
    def success?(primitive, input)
      input.kind_of?(primitive)
    end

  end # Coercer

end # Virtus