Class: Faraday::Options
Overview
Subclasses Struct with some special helpers for converting from a Hash to a Struct.
Class Method Summary collapse
-
.attribute_options ⇒ Object
Internal.
- .fetch_error_class ⇒ Object
-
.from(value) ⇒ Object
Public.
- .inherited(subclass) ⇒ Object
- .memoized(key) ⇒ Object
- .memoized_attributes ⇒ Object
-
.options(mapping) ⇒ Object
Internal.
-
.options_for(key) ⇒ Object
Internal.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#clear ⇒ Object
Public.
-
#delete(key) ⇒ Object
Public.
-
#each ⇒ Object
Public.
-
#each_key ⇒ Object
Public.
-
#each_value ⇒ Object
Public.
-
#empty? ⇒ Boolean
Public.
-
#fetch(key, *args) ⇒ Object
Public.
-
#inspect ⇒ Object
Internal.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Public.
-
#keys ⇒ Object
Public.
-
#merge(value) ⇒ Object
Public.
- #symbolized_key_set ⇒ Object
-
#to_hash ⇒ Object
Public.
-
#update(obj) ⇒ Object
(also: #merge!)
Public.
-
#value?(value) ⇒ Boolean
(also: #has_value?)
Public.
-
#values_at(*keys) ⇒ Object
Public.
Class Method Details
.attribute_options ⇒ Object
Internal
148 149 150 |
# File 'lib/faraday/options.rb', line 148 def self. @attribute_options ||= {} end |
.fetch_error_class ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/faraday/options.rb', line 182 def self.fetch_error_class @fetch_error_class ||= if Object.const_defined?(:KeyError) ::KeyError else ::IndexError end end |
.from(value) ⇒ Object
Public
6 7 8 |
# File 'lib/faraday/options.rb', line 6 def self.from(value) value ? new.update(value) : new end |
.inherited(subclass) ⇒ Object
176 177 178 179 180 |
# File 'lib/faraday/options.rb', line 176 def self.inherited(subclass) super subclass..update() subclass.memoized_attributes.update(memoized_attributes) end |
.memoized(key) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/faraday/options.rb', line 152 def self.memoized(key) memoized_attributes[key.to_sym] = Proc.new class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{key}() self[:#{key}]; end RUBY end |
.memoized_attributes ⇒ Object
159 160 161 |
# File 'lib/faraday/options.rb', line 159 def self.memoized_attributes @memoized_attributes ||= {} end |
.options(mapping) ⇒ Object
Internal
138 139 140 |
# File 'lib/faraday/options.rb', line 138 def self.(mapping) .update(mapping) end |
.options_for(key) ⇒ Object
Internal
143 144 145 |
# File 'lib/faraday/options.rb', line 143 def self.(key) [key] end |
Instance Method Details
#[](key) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/faraday/options.rb', line 163 def [](key) key = key.to_sym if method = self.class.memoized_attributes[key] super(key) || (self[key] = instance_eval(&method)) else super end end |
#clear ⇒ Object
Public
46 47 48 |
# File 'lib/faraday/options.rb', line 46 def clear members.each { |member| delete(member) } end |
#delete(key) ⇒ Object
Public
39 40 41 42 43 |
# File 'lib/faraday/options.rb', line 39 def delete(key) value = send(key) send("#{key}=", nil) value end |
#each ⇒ Object
Public
11 12 13 14 15 16 |
# File 'lib/faraday/options.rb', line 11 def each return to_enum(:each) unless block_given? members.each do |key| yield(key.to_sym, send(key)) end end |
#each_key ⇒ Object
Public
86 87 88 89 90 91 |
# File 'lib/faraday/options.rb', line 86 def each_key return to_enum(:each_key) unless block_given? keys.each do |key| yield(key) end end |
#each_value ⇒ Object
Public
101 102 103 104 105 106 |
# File 'lib/faraday/options.rb', line 101 def each_value return to_enum(:each_value) unless block_given? values.each do |value| yield(value) end end |
#empty? ⇒ Boolean
Public
81 82 83 |
# File 'lib/faraday/options.rb', line 81 def empty? keys.empty? end |
#fetch(key, *args) ⇒ Object
Public
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/faraday/options.rb', line 56 def fetch(key, *args) unless symbolized_key_set.include?(key.to_sym) key_setter = "#{key}=" if args.size > 0 send(key_setter, args.first) elsif block_given? send(key_setter, Proc.new.call(key)) else raise self.class.fetch_error_class, "key not found: #{key.inspect}" end end send(key) end |
#inspect ⇒ Object
Internal
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/faraday/options.rb', line 126 def inspect values = [] members.each do |member| value = send(member) values << "#{member}=#{value.inspect}" if value end values = values.empty? ? ' (empty)' : (' ' << values.join(", ")) %(#<#{self.class}#{values}>) end |
#key?(key) ⇒ Boolean Also known as: has_key?
Public
94 95 96 |
# File 'lib/faraday/options.rb', line 94 def key?(key) keys.include?(key) end |
#keys ⇒ Object
Public
76 77 78 |
# File 'lib/faraday/options.rb', line 76 def keys members.reject { |member| send(member).nil? } end |
#merge(value) ⇒ Object
Public
51 52 53 |
# File 'lib/faraday/options.rb', line 51 def merge(value) dup.update(value) end |
#symbolized_key_set ⇒ Object
172 173 174 |
# File 'lib/faraday/options.rb', line 172 def symbolized_key_set @symbolized_key_set ||= Set.new(keys.map { |k| k.to_sym }) end |
#to_hash ⇒ Object
Public
116 117 118 119 120 121 122 123 |
# File 'lib/faraday/options.rb', line 116 def to_hash hash = {} members.each do |key| value = send(key) hash[key.to_sym] = value unless value.nil? end hash end |
#update(obj) ⇒ Object Also known as: merge!
Public
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/faraday/options.rb', line 19 def update(obj) obj.each do |key, value| if = self.class.(key) value = .from(value) if value elsif Hash === value hash = {} value.each do |hash_key, hash_value| hash[hash_key] = hash_value end value = hash end self.send("#{key}=", value) unless value.nil? end self end |
#value?(value) ⇒ Boolean Also known as: has_value?
Public
109 110 111 |
# File 'lib/faraday/options.rb', line 109 def value?(value) values.include?(value) end |
#values_at(*keys) ⇒ Object
Public
71 72 73 |
# File 'lib/faraday/options.rb', line 71 def values_at(*keys) keys.map { |key| send(key) } end |