File: configuration.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 (72 lines) | stat: -rw-r--r-- 2,044 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Virtus

  # A Configuration instance
  class Configuration

    # Access the finalize setting for this instance
    attr_accessor :finalize

    # Access the coerce setting for this instance
    attr_accessor :coerce

    # Access the strict setting for this instance
    attr_accessor :strict

    # Access the nullify_blank setting for this instance
    attr_accessor :nullify_blank

    # Access the required setting for this instance
    attr_accessor :required

    # Access the constructor setting for this instance
    attr_accessor :constructor

    # Access the mass-assignment setting for this instance
    attr_accessor :mass_assignment

    # Initialized a configuration instance
    #
    # @return [undefined]
    #
    # @api private
    def initialize(options={})
      @finalize        = options.fetch(:finalize, true)
      @coerce          = options.fetch(:coerce, true)
      @strict          = options.fetch(:strict, false)
      @nullify_blank   = options.fetch(:nullify_blank, false)
      @required        = options.fetch(:required, true)
      @constructor     = options.fetch(:constructor, true)
      @mass_assignment = options.fetch(:mass_assignment, true)
      @coercer         = Coercible::Coercer.new

      yield self if block_given?
    end

    # Access the coercer for this instance and optional configure a
    # new coercer with the passed block
    #
    # @example
    #   configuration.coercer do |config|
    #     config.string.boolean_map = { true => '1', false => '0' }
    #   end
    #
    # @return [Coercer]
    #
    # @api private
    def coercer(&block)
      @coercer = Coercible::Coercer.new(&block) if block_given?
      @coercer
    end

    # @api private
    def to_h
      { :coerce             => coerce,
        :finalize           => finalize,
        :strict             => strict,
        :nullify_blank      => nullify_blank,
        :required           => required,
        :configured_coercer => coercer }.freeze
    end

  end # class Configuration
end # module Virtus