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

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 process_options(given_options = {})
  options = DEFAULT_OPTIONS.merge(given_options || {})
  validate_options!(options)
  options
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.

Raises:

  • (ArgumentError)


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 validate_options!(options)
  errors = []

  unless %i[auto float bigdecimal].include?(options[:decimal_precision])
    errors << "decimal_precision must be :auto, :float, or :bigdecimal (got #{options[:decimal_precision].inspect})"
  end
  unless %i[last_wins first_wins].include?(options[:duplicate_key])
    errors << "duplicate_key must be :last_wins or :first_wins (got #{options[:duplicate_key].inspect})"
  end
  on_warning = options[: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 = options[: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?

  options
end