Class: Rbdantic::ModelConfig
- Inherits:
-
Object
- Object
- Rbdantic::ModelConfig
- Defined in:
- lib/rbdantic/config.rb
Overview
Model configuration options
Constant Summary collapse
- VALID_EXTRA_VALUES =
i[forbid ignore allow].freeze
- VALID_COERCE_MODES =
i[strict coerce].freeze
Instance Attribute Summary collapse
-
#coerce_mode ⇒ Object
readonly
Returns the value of attribute coerce_mode.
-
#extra ⇒ Object
readonly
Returns the value of attribute extra.
-
#frozen ⇒ Object
readonly
Returns the value of attribute frozen.
-
#strict ⇒ Object
readonly
Returns the value of attribute strict.
-
#validate_assignment ⇒ Object
readonly
Returns the value of attribute validate_assignment.
Instance Method Summary collapse
-
#initialize(strict: false, extra: :ignore, frozen: false, validate_assignment: true, coerce_mode: nil) ⇒ ModelConfig
constructor
A new instance of ModelConfig.
- #to_h ⇒ Object
- #with(**overrides) ⇒ Object (also: #merge)
Constructor Details
#initialize(strict: false, extra: :ignore, frozen: false, validate_assignment: true, coerce_mode: nil) ⇒ ModelConfig
Returns a new instance of ModelConfig.
11 12 13 14 15 16 17 18 |
# File 'lib/rbdantic/config.rb', line 11 def initialize(strict: false, extra: :ignore, frozen: false, validate_assignment: true, coerce_mode: nil) @coerce_mode = validate_coerce_mode!(coerce_mode || (strict ? :strict : :coerce)) strict = (@coerce_mode == :strict) @strict = strict @extra = validate_extra!(extra) @frozen = frozen @validate_assignment = validate_assignment end |
Instance Attribute Details
#coerce_mode ⇒ Object (readonly)
Returns the value of attribute coerce_mode.
9 10 11 |
# File 'lib/rbdantic/config.rb', line 9 def coerce_mode @coerce_mode end |
#extra ⇒ Object (readonly)
Returns the value of attribute extra.
9 10 11 |
# File 'lib/rbdantic/config.rb', line 9 def extra @extra end |
#frozen ⇒ Object (readonly)
Returns the value of attribute frozen.
9 10 11 |
# File 'lib/rbdantic/config.rb', line 9 def frozen @frozen end |
#strict ⇒ Object (readonly)
Returns the value of attribute strict.
9 10 11 |
# File 'lib/rbdantic/config.rb', line 9 def strict @strict end |
#validate_assignment ⇒ Object (readonly)
Returns the value of attribute validate_assignment.
9 10 11 |
# File 'lib/rbdantic/config.rb', line 9 def validate_assignment @validate_assignment end |
Instance Method Details
#to_h ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/rbdantic/config.rb', line 20 def to_h { strict: @strict, extra: @extra, frozen: @frozen, validate_assignment: @validate_assignment, coerce_mode: @coerce_mode } end |
#with(**overrides) ⇒ Object Also known as: merge
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rbdantic/config.rb', line 30 def with(**overrides) merged = to_h.merge(overrides) if overrides.key?(:strict) && !overrides.key?(:coerce_mode) merged[:coerce_mode] = overrides[:strict] ? :strict : :coerce elsif overrides.key?(:coerce_mode) && !overrides.key?(:strict) merged[:strict] = overrides[:coerce_mode] == :strict end self.class.new(**merged) end |