Class: Faraday::Options

Inherits:
Struct
  • Object
show all
Defined in:
lib/faraday/options.rb

Overview

Subclasses Struct with some special helpers for converting from a Hash to a Struct.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute_optionsObject

Internal



166
167
168
# File 'lib/faraday/options.rb', line 166

def self.attribute_options
  @attribute_options ||= {}
end

.fetch_error_classObject



205
206
207
208
209
210
211
# File 'lib/faraday/options.rb', line 205

def self.fetch_error_class
  @fetch_error_class ||= if Object.const_defined?(:KeyError)
                           ::KeyError
                         else
                           ::IndexError
                         end
end

.from(value) ⇒ Object

Public



8
9
10
# File 'lib/faraday/options.rb', line 8

def self.from(value)
  value ? new.update(value) : new
end

.memoized(key, &block) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/faraday/options.rb', line 170

def self.memoized(key, &block)
  unless block
    raise ArgumentError, '#memoized must be called with a block'
  end

  memoized_attributes[key.to_sym] = block
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    remove_method(key) if method_defined?(key, false)
    def #{key}() self[:#{key}]; end
  RUBY
end

.memoized_attributesObject



182
183
184
# File 'lib/faraday/options.rb', line 182

def self.memoized_attributes
  @memoized_attributes ||= {}
end

.options(mapping) ⇒ Object

Internal



156
157
158
# File 'lib/faraday/options.rb', line 156

def self.options(mapping)
  attribute_options.update(mapping)
end

.options_for(key) ⇒ Object

Internal



161
162
163
# File 'lib/faraday/options.rb', line 161

def self.options_for(key)
  attribute_options[key]
end

Instance Method Details

#[](key) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/faraday/options.rb', line 186

def [](key)
  key = key.to_sym
  if (method = self.class.memoized_attributes[key])
    super(key) || (self[key] = instance_eval(&method))
  else
    super
  end
end

#clearObject

Public



46
47
48
# File 'lib/faraday/options.rb', line 46

def clear
  members.each { |member| delete(member) }
end

#deep_dupObject

Public



71
72
73
# File 'lib/faraday/options.rb', line 71

def deep_dup
  self.class.from(self)
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

#eachObject

Public



13
14
15
16
17
18
19
# File 'lib/faraday/options.rb', line 13

def each
  return to_enum(:each) unless block_given?

  members.each do |key|
    yield(key.to_sym, send(key))
  end
end

#each_key(&block) ⇒ Object

Public



106
107
108
109
110
# File 'lib/faraday/options.rb', line 106

def each_key(&block)
  return to_enum(:each_key) unless block

  keys.each(&block)
end

#each_value(&block) ⇒ Object

Public



120
121
122
123
124
# File 'lib/faraday/options.rb', line 120

def each_value(&block)
  return to_enum(:each_value) unless block

  values.each(&block)
end

#empty?Boolean

Public

Returns:

  • (Boolean)


101
102
103
# File 'lib/faraday/options.rb', line 101

def empty?
  keys.empty?
end

#fetch(key, *args) ⇒ Object

Public



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/faraday/options.rb', line 76

def fetch(key, *args)
  unless symbolized_key_set.include?(key.to_sym)
    key_setter = "#{key}="
    if !args.empty?
      send(key_setter, args.first)
    elsif block_given?
      send(key_setter, yield(key))
    else
      raise self.class.fetch_error_class, "key not found: #{key.inspect}"
    end
  end
  send(key)
end

#inspectObject

Internal



144
145
146
147
148
149
150
151
152
153
# File 'lib/faraday/options.rb', line 144

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

Returns:

  • (Boolean)


113
114
115
# File 'lib/faraday/options.rb', line 113

def key?(key)
  keys.include?(key)
end

#keysObject

Public



96
97
98
# File 'lib/faraday/options.rb', line 96

def keys
  members.reject { |member| send(member).nil? }
end

#merge(other) ⇒ Object

Public



66
67
68
# File 'lib/faraday/options.rb', line 66

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ Object

Public



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/faraday/options.rb', line 51

def merge!(other)
  other.each do |key, other_value|
    self_value = send(key)
    sub_options = self.class.options_for(key)
    new_value = if self_value && sub_options && other_value
                  self_value.merge(other_value)
                else
                  other_value
                end
    send(:"#{key}=", new_value) unless new_value.nil?
  end
  self
end

#symbolized_key_setObject



195
196
197
# File 'lib/faraday/options.rb', line 195

def symbolized_key_set
  @symbolized_key_set ||= Set.new(keys.map(&:to_sym))
end

#to_hashObject

Public



134
135
136
137
138
139
140
141
# File 'lib/faraday/options.rb', line 134

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

Public



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/faraday/options.rb', line 22

def update(obj)
  obj.each do |key, value|
    sub_options = self.class.options_for(key)
    if sub_options
      new_value = sub_options.from(value) if value
    elsif value.is_a?(Hash)
      new_value = value.dup
    else
      new_value = value
    end

    send(:"#{key}=", new_value) unless new_value.nil?
  end
  self
end

#value?(value) ⇒ Boolean Also known as: has_value?

Public

Returns:

  • (Boolean)


127
128
129
# File 'lib/faraday/options.rb', line 127

def value?(value)
  values.include?(value)
end

#values_at(*keys) ⇒ Object

Public



91
92
93
# File 'lib/faraday/options.rb', line 91

def values_at(*keys)
  keys.map { |key| send(key) }
end