Module: SmarterJSON::Options
- Defined in:
- lib/smarter_json/options.rb
Overview
All reader settings live in one options hash (smarter_csv style). This module holds the defaults, merges the caller’s overrides onto them, and validates the result — mirroring SmarterCSV::Reader::Options.
Constant Summary collapse
- DEFAULT_OPTIONS =
{ acceleration: true, # use the C extension when available; false forces pure Ruby encoding: nil, # label the input's encoding (no transcoding); nil keeps the input's own symbolize_keys: false, # Symbol keys instead of String duplicate_key: :last_wins, # :last_wins | :first_wins (repeats are also reported via on_warning) decimal_precision: :auto, # :auto | :float | :bigdecimal (Oj-compatible decimal handling) on_warning: nil, # a callable invoked once per non-fatal lenient fix (a SmarterJSON::Warning) }.freeze
Class Method Summary collapse
-
.process_options(given_options = {}) ⇒ Object
Merge the caller’s overrides onto the defaults, validate, and return the hash.
-
.validate_options!(options) ⇒ Object
Raise ArgumentError (consistent with the generator’s option checks) listing every invalid setting at once.
Class Method Details
.process_options(given_options = {}) ⇒ Object
Merge the caller’s overrides onto the defaults, validate, and return the hash.
20 21 22 23 24 |
# File 'lib/smarter_json/options.rb', line 20 def ( = {}) = DEFAULT_OPTIONS.merge( || {}) () end |
.validate_options!(options) ⇒ Object
Raise ArgumentError (consistent with the generator’s option checks) listing every invalid setting at once. Unknown keys are ignored, matching the lenient design — an option SmarterJSON doesn’t recognize simply has no effect.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/smarter_json/options.rb', line 29 def () errors = [] unless %i[auto float bigdecimal].include?([:decimal_precision]) errors << "decimal_precision must be :auto, :float, or :bigdecimal (got #{[:decimal_precision].inspect})" end unless %i[last_wins first_wins].include?([:duplicate_key]) errors << "duplicate_key must be :last_wins or :first_wins (got #{[:duplicate_key].inspect})" end on_warning = [:on_warning] unless on_warning.nil? || on_warning.respond_to?(:call) errors << "on_warning must be nil or a callable (got #{on_warning.class})" end encoding = [:encoding] unless encoding.nil? || encoding.is_a?(String) errors << "encoding must be nil or a String (got #{encoding.class})" end raise ArgumentError, "SmarterJSON: invalid options — #{errors.join('; ')}" if errors.any? end |