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 (valid-UTF-8 ASCII-8BIT → UTF-8) 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 problem 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 problem at once. Configuration is strict — unlike the lenient data handling, an unknown option key or a bad value raises, so a caller’s typo or wrong type is caught immediately instead of silently having no effect.
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 |
# File 'lib/smarter_json/options.rb', line 30 def () errors = [] unknown = .keys - DEFAULT_OPTIONS.keys unless unknown.empty? errors << "unknown option#{unknown.size == 1 ? '' : 's'} #{unknown.map(&:inspect).join(', ')} " \ "— valid keys: #{DEFAULT_OPTIONS.keys.map(&:inspect).join(', ')}" end 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 unless [true, false].include?([:acceleration]) errors << "acceleration must be true or false (got #{[:acceleration].inspect})" end unless [true, false].include?([:symbolize_keys]) errors << "symbolize_keys must be true or false (got #{[:symbolize_keys].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 |