smarter_json 0.9.2 → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmarterJSON
4
+ # All reader settings live in one options hash (smarter_csv style). This module
5
+ # holds the defaults, merges the caller's overrides onto them, and validates the
6
+ # result — mirroring SmarterCSV::Reader::Options.
7
+ module Options
8
+ DEFAULT_OPTIONS = {
9
+ acceleration: true, # use the C extension when available; false forces pure Ruby
10
+ encoding: nil, # label the input's encoding (no transcoding); nil keeps the input's own
11
+ symbolize_keys: false, # Symbol keys instead of String
12
+ duplicate_key: :last_wins, # :last_wins | :first_wins (repeats are also reported via on_warning)
13
+ decimal_precision: :auto, # :auto | :float | :bigdecimal (Oj-compatible decimal handling)
14
+ on_warning: nil, # a callable invoked once per non-fatal lenient fix (a SmarterJSON::Warning)
15
+ }.freeze
16
+
17
+ module_function
18
+
19
+ # Merge the caller's overrides onto the defaults, validate, and return the hash.
20
+ def process_options(given_options = {})
21
+ options = DEFAULT_OPTIONS.merge(given_options || {})
22
+ validate_options!(options)
23
+ options
24
+ end
25
+
26
+ # Raise ArgumentError (consistent with the generator's option checks) listing
27
+ # every invalid setting at once. Unknown keys are ignored, matching the lenient
28
+ # design — an option SmarterJSON doesn't recognize simply has no effect.
29
+ def validate_options!(options)
30
+ errors = []
31
+
32
+ unless %i[auto float bigdecimal].include?(options[:decimal_precision])
33
+ errors << "decimal_precision must be :auto, :float, or :bigdecimal (got #{options[:decimal_precision].inspect})"
34
+ end
35
+ unless %i[last_wins first_wins].include?(options[:duplicate_key])
36
+ errors << "duplicate_key must be :last_wins or :first_wins (got #{options[:duplicate_key].inspect})"
37
+ end
38
+ on_warning = options[:on_warning]
39
+ unless on_warning.nil? || on_warning.respond_to?(:call)
40
+ errors << "on_warning must be nil or a callable (got #{on_warning.class})"
41
+ end
42
+ encoding = options[:encoding]
43
+ unless encoding.nil? || encoding.is_a?(String)
44
+ errors << "encoding must be nil or a String (got #{encoding.class})"
45
+ end
46
+
47
+ raise ArgumentError, "SmarterJSON: invalid options — #{errors.join('; ')}" if errors.any?
48
+
49
+ options
50
+ end
51
+ end
52
+ end