Class: Philiprehberger::TomlKit::TypeCoercion
- Inherits:
-
Object
- Object
- Philiprehberger::TomlKit::TypeCoercion
- Defined in:
- lib/philiprehberger/toml_kit/type_coercion.rb
Overview
Registry for custom type serialization and deserialization hooks.
Allows registering custom handlers for Ruby types that are not natively supported by TOML. Coercions are applied during dump (serialize) and can be applied after parse (deserialize).
Defined Under Namespace
Classes: Rule
Instance Method Summary collapse
-
#coerce_for_deserialize(value) ⇒ Object
Apply deserialization coercions to a value (after parse).
-
#coerce_for_serialize(value) ⇒ Object
Apply serialization coercions to a value (for dump).
-
#handles?(value) ⇒ Boolean
Check if there is a registered rule for the given value.
-
#initialize ⇒ TypeCoercion
constructor
A new instance of TypeCoercion.
-
#register(type, serializer:, tag: nil, deserializer: nil) ⇒ self
Register a custom type coercion.
-
#rules ⇒ Array<Rule>
Return all registered rules.
Constructor Details
#initialize ⇒ TypeCoercion
Returns a new instance of TypeCoercion.
14 15 16 |
# File 'lib/philiprehberger/toml_kit/type_coercion.rb', line 14 def initialize @rules = [] end |
Instance Method Details
#coerce_for_deserialize(value) ⇒ Object
Apply deserialization coercions to a value (after parse). Walks hashes and arrays recursively looking for tagged strings.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/philiprehberger/toml_kit/type_coercion.rb', line 58 def coerce_for_deserialize(value) case value when String if value.start_with?('__coerced:') parts = value.split(':', 3) tag = parts[1] raw = parts[2] rule = @rules.find { |r| r.tag == tag } if rule&.deserializer rule.deserializer.call(raw) else value end else value end when Hash value.transform_values { |v| coerce_for_deserialize(v) } when Array value.map { |v| coerce_for_deserialize(v) } else value end end |
#coerce_for_serialize(value) ⇒ Object
Apply serialization coercions to a value (for dump). Walks hashes and arrays recursively.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/philiprehberger/toml_kit/type_coercion.rb', line 35 def coerce_for_serialize(value) rule = find_rule(value) if rule result = rule.serializer.call(value) if rule.tag "__coerced:#{rule.tag}:#{result}" else result end elsif value.is_a?(Hash) value.transform_values { |v| coerce_for_serialize(v) } elsif value.is_a?(Array) value.map { |v| coerce_for_serialize(v) } else value end end |
#handles?(value) ⇒ Boolean
Check if there is a registered rule for the given value.
87 88 89 |
# File 'lib/philiprehberger/toml_kit/type_coercion.rb', line 87 def handles?(value) !find_rule(value).nil? end |
#register(type, serializer:, tag: nil, deserializer: nil) ⇒ self
Register a custom type coercion.
25 26 27 28 |
# File 'lib/philiprehberger/toml_kit/type_coercion.rb', line 25 def register(type, serializer:, tag: nil, deserializer: nil) @rules << Rule.new(type: type, serializer: serializer, deserializer: deserializer, tag: tag) self end |
#rules ⇒ Array<Rule>
Return all registered rules.
94 95 96 |
# File 'lib/philiprehberger/toml_kit/type_coercion.rb', line 94 def rules @rules.dup end |