Custom Options Classes¶ ↑
Faraday’s own options classes (ConnectionOptions, RequestOptions, SSLOptions, ProxyOptions and Env) inherit from Faraday::BaseOptions. The legacy Faraday::Options, a Struct, still exists for code that subclasses it, but new options classes should not use it. Two building blocks are available for code that defines its own options classes, such as adapters and middleware:
-
Faraday::OptionsLikeis a marker module. It is included by the legacyFaraday::Optionsand byFaraday::BaseOptions, soFaraday::Utils.deep_merge!,Faraday::Utils.deep_mergeandFaraday::Utils.deep_dupcan treat both the same way. Classes that include it should respond toto_hashanddeep_dup. -
Faraday::BaseOptionsis an abstract class that implementsfrom,update,merge,merge!,deep_dup,to_hashandinspectfor you, including nested coercion.
Defining a class¶ ↑
A subclass lists its attributes in MEMBERS and defines a public reader and writer for each of them. COERCIONS is optional and maps an attribute to the coercion applied to every non-nil value assigned to it:
-
a
Class, coerced withClass.from(value)(for example anotherBaseOptionssubclass); -
a
Proc, coerced withproc.call(value).
class CacheOptions < Faraday::BaseOptions MEMBERS = %i[ttl store].freeze attr_accessor :ttl, :store end class MyOptions < Faraday::BaseOptions MEMBERS = %i[timeout cache headers].freeze COERCIONS = { cache: CacheOptions }.freeze attr_accessor :timeout, :cache, :headers end options = MyOptions.new(timeout: 5, cache: { ttl: 60 }) options.cache.class # => CacheOptions
Behavior¶ ↑
-
MyOptions.from(value)returns a deep copy ofvalueif it is already aMyOptions(the caller’s object is never shared), a new empty instance fornil, and a new instance built from aHash, anotherOptionsLikeor anything that responds toto_hash. Other values raiseArgumentError. -
newaccepts a single optional hash, soMyOptions.new(timeout: 5)works too. -
Attributes are only read and written through their public readers and writers, so a subclass can override them.
-
Keys can be symbols or strings, and are normalized to symbols. A key that is not in
MEMBERSraisesNoMethodError, like it does forFaraday::Options. -
nilvalues are skipped bynew,updateandmerge!, so they never overwrite an existing value.falsedoes. -
merge!deep merges a hash into the existing hash or options value of the same attribute, recursively. Other values are replaced. Nested options objects are replaced by a merged copy rather than mutated. -
mergeanddeep_dupcopy hashes, arrays and nested options recursively, so the result shares none of them with the original. Any other object (strings, procs, IO objects and so on) is kept as is. -
to_hashskipsnilattributes and converts nested options to hashes. Becausedeep_dupandmergego throughto_hash, an attribute holding an options object needs a matching entry inCOERCIONSto stay an options object.
options.merge(cache: { store: :memory }).to_hash # => { timeout: 5, cache: { ttl: 60, store: :memory } } options.merge!(timeout: nil, 'cache' => { 'ttl' => 30 }) options.to_hash # => { timeout: 5, cache: { ttl: 30 } }
Faraday::Utils.deep_merge!(target, hash) accepts a BaseOptions as target and calls target.merge!(hash).
Struct-style access¶ ↑
BaseOptions has no Struct API: no [], []=, each, keys, dig or value-based ==. Use the readers and writers instead. RequestOptions, SSLOptions and ProxyOptions still answer [], []=, fetch and key? through Faraday::StructCompat, because adapters and other gems call them (for example env[:ssl].fetch(:verify, true) or conn.options[:timeout] = 5). StructCompat is deprecated and will be removed in Faraday 3.0, so new classes must not include it.
For the same reason, every BaseOptions class keeps to_h, empty? and the class method members. They are deprecated too: use to_hash, to_hash.empty? and MEMBERS in new code.
Faraday::Env keeps its own hash-like API, because middleware relies on it. See The Env Object.