Class: Mongory::Converters::AbstractConverter
- Inherits:
-
Utils::SingletonBuilder
- Object
- Utils::SingletonBuilder
- Mongory::Converters::AbstractConverter
- Includes:
- Singleton
- Defined in:
- lib/mongory/converters/abstract_converter.rb
Overview
AbstractConverter provides a flexible DSL-style mechanism for dynamically converting objects based on their class.
It allows you to register conversion rules for specific classes, with optional fallback behavior.
Direct Known Subclasses
ConditionConverter, DataConverter, KeyConverter, ValueConverter
Defined Under Namespace
Classes: Registry
Constant Summary collapse
- NOTHING =
A sentinel value used to indicate absence of a secondary argument.
Utils::SingletonBuilder.new('NOTHING')
Instance Method Summary collapse
-
#configure { ... } ⇒ void
Opens a configuration block to register more converters.
-
#convert(target, other = NOTHING) ⇒ Object
Applies the registered conversion to the given target object.
- #fallback(target, _) ⇒ Object
-
#find_strategy(target) ⇒ Proc
Finds the appropriate conversion strategy for the target object.
-
#freeze ⇒ void
Freezes all internal registries.
-
#initialize ⇒ AbstractConverter
constructor
Initializes the builder with a label and optional configuration block.
-
#register(klass, converter = nil) {|*args| ... } ⇒ void
Registers a conversion rule for a given class.
Methods inherited from Utils::SingletonBuilder
Constructor Details
#initialize ⇒ AbstractConverter
Initializes the builder with a label and optional configuration block.
33 34 35 36 37 |
# File 'lib/mongory/converters/abstract_converter.rb', line 33 def initialize super(self.class.to_s) @registries = [] @convert_strategy_map = {}.compare_by_identity end |
Instance Method Details
#configure { ... } ⇒ void
This method returns an undefined value.
Opens a configuration block to register more converters.
77 78 79 80 |
# File 'lib/mongory/converters/abstract_converter.rb', line 77 def configure yield self freeze end |
#convert(target, other = NOTHING) ⇒ Object
Applies the registered conversion to the given target object.
44 45 46 47 48 49 50 51 |
# File 'lib/mongory/converters/abstract_converter.rb', line 44 def convert(target, other = NOTHING) convert_strategy = @convert_strategy_map[target.class] ||= find_strategy(target) return fallback(target, other) if convert_strategy == NOTHING return target.instance_exec(&convert_strategy) if other == NOTHING target.instance_exec(other, &convert_strategy) end |
#fallback(target, _) ⇒ Object
53 54 55 |
# File 'lib/mongory/converters/abstract_converter.rb', line 53 def fallback(target, _) target end |
#find_strategy(target) ⇒ Proc
Finds the appropriate conversion strategy for the target object. Searches through registered rules and returns the first matching one, or the fallback strategy if no match is found.
63 64 65 66 67 68 69 70 71 |
# File 'lib/mongory/converters/abstract_converter.rb', line 63 def find_strategy(target) @registries.each do |registry| next unless target.is_a?(registry.klass) return registry.exec end NOTHING end |
#freeze ⇒ void
This method returns an undefined value.
Freezes all internal registries.
85 86 87 |
# File 'lib/mongory/converters/abstract_converter.rb', line 85 def freeze @registries.freeze end |
#register(klass, converter = nil) {|*args| ... } ⇒ void
This method returns an undefined value.
Registers a conversion rule for a given class.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mongory/converters/abstract_converter.rb', line 96 def register(klass, converter = nil, &block) raise 'converter or block is required.' if [converter, block].compact.empty? raise 'A class or module is reuqired.' unless klass.is_a?(Module) if converter.is_a?(Symbol) register(klass) { |*args, &bl| send(converter, *args, &bl) } elsif block.is_a?(Proc) @registries.unshift(Registry.new(klass, block)) @convert_strategy_map[klass] = block else raise 'Support Symbol and block only.' end end |