|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Faraday |
| 4 | + # Abstract base class for Options-like classes. |
| 5 | + # |
| 6 | + # Provides common functionality for nested coercion, deep merging, and duplication. |
| 7 | + # Subclasses must define: |
| 8 | + # - +MEMBERS+: Array of attribute names (symbols) |
| 9 | + # - +COERCIONS+: Hash mapping attribute names to coercion classes |
| 10 | + # |
| 11 | + # @example Creating a subclass |
| 12 | + # class MyOptions < Faraday::BaseOptions |
| 13 | + # MEMBERS = [:timeout, :open_timeout].freeze |
| 14 | + # COERCIONS = {}.freeze |
| 15 | + # |
| 16 | + # attr_accessor :timeout, :open_timeout |
| 17 | + # end |
| 18 | + # |
| 19 | + # options = MyOptions.new(timeout: 10) |
| 20 | + # options.timeout # => 10 |
| 21 | + # |
| 22 | + # @example With nested coercion |
| 23 | + # class ProxyOptions < Faraday::BaseOptions |
| 24 | + # MEMBERS = [:uri].freeze |
| 25 | + # COERCIONS = { uri: URI }.freeze |
| 26 | + # |
| 27 | + # attr_accessor :uri |
| 28 | + # end |
| 29 | + # |
| 30 | + # @see OptionsLike |
| 31 | + class BaseOptions |
| 32 | + include OptionsLike |
| 33 | + |
| 34 | + # Subclasses must define: |
| 35 | + # - MEMBERS: Array of attribute names (symbols) |
| 36 | + # - COERCIONS: Hash mapping attribute names to coercion classes |
| 37 | + |
| 38 | + class << self |
| 39 | + # Create new instance from hash or existing instance. |
| 40 | + # |
| 41 | + # @param value [nil, Hash, BaseOptions] the value to convert |
| 42 | + # @return [BaseOptions] a new instance or the value itself if already correct type |
| 43 | + # |
| 44 | + # @example |
| 45 | + # MyOptions.from(nil) # => empty MyOptions instance |
| 46 | + # MyOptions.from(timeout: 10) # => MyOptions with timeout=10 |
| 47 | + # existing = MyOptions.new(timeout: 10) |
| 48 | + # MyOptions.from(existing) # => returns existing (same instance) |
| 49 | + def from(value) |
| 50 | + return value if value.is_a?(self) |
| 51 | + return new if value.nil? |
| 52 | + |
| 53 | + new(value) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + # Initialize a new instance with the given options. |
| 58 | + # |
| 59 | + # @param options_hash [Hash, #to_hash, nil] options to initialize with as positional arg |
| 60 | + # @param options [Hash] options to initialize with as keyword args |
| 61 | + # @return [BaseOptions] self |
| 62 | + # |
| 63 | + # @example |
| 64 | + # options = MyOptions.new(timeout: 10, open_timeout: 5) |
| 65 | + # options = MyOptions.new({ timeout: 10 }) |
| 66 | + def initialize(options_hash = nil, **options) |
| 67 | + # Merge positional and keyword arguments |
| 68 | + if options_hash |
| 69 | + options_hash = options_hash.to_hash if options_hash.respond_to?(:to_hash) |
| 70 | + options = options_hash.merge(options) |
| 71 | + end |
| 72 | + |
| 73 | + self.class::MEMBERS.each do |key| |
| 74 | + value = options[key] || options[key.to_s] |
| 75 | + value = coerce(key, value) |
| 76 | + instance_variable_set(:"@#{key}", value) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # Update this instance with values from another hash/instance. |
| 81 | + # |
| 82 | + # @param obj [Hash, #to_hash] the values to update with |
| 83 | + # @return [BaseOptions] self |
| 84 | + # |
| 85 | + # @example |
| 86 | + # options = MyOptions.new(timeout: 10) |
| 87 | + # options.update(timeout: 20, open_timeout: 5) |
| 88 | + # options.timeout # => 20 |
| 89 | + def update(obj) |
| 90 | + obj = obj.to_hash if obj.respond_to?(:to_hash) |
| 91 | + obj.each do |key, value| |
| 92 | + key = key.to_sym |
| 93 | + if self.class::MEMBERS.include?(key) |
| 94 | + value = coerce(key, value) |
| 95 | + instance_variable_set(:"@#{key}", value) |
| 96 | + end |
| 97 | + end |
| 98 | + self |
| 99 | + end |
| 100 | + |
| 101 | + # Non-destructive merge. |
| 102 | + # |
| 103 | + # Creates a deep copy and merges the given hash/instance into it. |
| 104 | + # |
| 105 | + # @param obj [Hash, #to_hash] the values to merge |
| 106 | + # @return [BaseOptions] a new instance with merged values |
| 107 | + # |
| 108 | + # @example |
| 109 | + # options = MyOptions.new(timeout: 10) |
| 110 | + # new_options = options.merge(timeout: 20) |
| 111 | + # options.timeout # => 10 (unchanged) |
| 112 | + # new_options.timeout # => 20 |
| 113 | + def merge(obj) |
| 114 | + deep_dup.merge!(obj) |
| 115 | + end |
| 116 | + |
| 117 | + # Destructive merge using {Utils.deep_merge!}. |
| 118 | + # |
| 119 | + # @param obj [Hash, #to_hash] the values to merge |
| 120 | + # @return [BaseOptions] self |
| 121 | + # |
| 122 | + # @example |
| 123 | + # options = MyOptions.new(timeout: 10) |
| 124 | + # options.merge!(timeout: 20) |
| 125 | + # options.timeout # => 20 |
| 126 | + def merge!(obj) |
| 127 | + obj = obj.to_hash if obj.respond_to?(:to_hash) |
| 128 | + hash = to_hash |
| 129 | + Utils.deep_merge!(hash, obj) |
| 130 | + update(hash) |
| 131 | + end |
| 132 | + |
| 133 | + # Create a deep duplicate of this instance. |
| 134 | + # |
| 135 | + # @return [BaseOptions] a new instance with deeply duplicated values |
| 136 | + # |
| 137 | + # @example |
| 138 | + # original = MyOptions.new(timeout: 10) |
| 139 | + # copy = original.deep_dup |
| 140 | + # copy.timeout = 20 |
| 141 | + # original.timeout # => 10 (unchanged) |
| 142 | + def deep_dup |
| 143 | + self.class.new( |
| 144 | + self.class::MEMBERS.each_with_object({}) do |key, hash| |
| 145 | + value = instance_variable_get(:"@#{key}") |
| 146 | + hash[key] = Utils.deep_dup(value) |
| 147 | + end |
| 148 | + ) |
| 149 | + end |
| 150 | + |
| 151 | + # Convert to a hash. |
| 152 | + # |
| 153 | + # @return [Hash] hash representation with symbol keys |
| 154 | + # |
| 155 | + # @example |
| 156 | + # options = MyOptions.new(timeout: 10) |
| 157 | + # options.to_hash # => { timeout: 10 } |
| 158 | + def to_hash |
| 159 | + self.class::MEMBERS.each_with_object({}) do |key, hash| |
| 160 | + hash[key] = instance_variable_get(:"@#{key}") |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + # Inspect the instance. |
| 165 | + # |
| 166 | + # @return [String] human-readable representation |
| 167 | + # |
| 168 | + # @example |
| 169 | + # options = MyOptions.new(timeout: 10) |
| 170 | + # options.inspect # => "#<MyOptions {:timeout=>10}>" |
| 171 | + def inspect |
| 172 | + "#<#{self.class} #{to_hash.inspect}>" |
| 173 | + end |
| 174 | + |
| 175 | + private |
| 176 | + |
| 177 | + # Coerce a value based on the COERCIONS configuration. |
| 178 | + # |
| 179 | + # @param key [Symbol] the attribute name |
| 180 | + # @param value [Object] the value to coerce |
| 181 | + # @return [Object] the coerced value or original if no coercion defined |
| 182 | + def coerce(key, value) |
| 183 | + coercion = self.class::COERCIONS[key] |
| 184 | + return value unless coercion |
| 185 | + return value if value.nil? |
| 186 | + return value if value.is_a?(coercion) |
| 187 | + |
| 188 | + coercion.from(value) |
| 189 | + end |
| 190 | + end |
| 191 | +end |
0 commit comments