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


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

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

.fetch_error_classObject


195
196
197
198
199
200
201
# File 'lib/faraday/options.rb', line 195

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


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

def self.inherited(subclass)
  super
  subclass.attribute_options.update(attribute_options)
  subclass.memoized_attributes.update(memoized_attributes)
end

.memoized(key) ⇒ Object


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

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_attributesObject


172
173
174
# File 'lib/faraday/options.rb', line 172

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

.options(mapping) ⇒ Object

Internal


151
152
153
# File 'lib/faraday/options.rb', line 151

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

.options_for(key) ⇒ Object

Internal


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

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

Instance Method Details

#[](key) ⇒ Object


176
177
178
179
180
181
182
183
# File 'lib/faraday/options.rb', line 176

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


43
44
45
# File 'lib/faraday/options.rb', line 43

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

#deep_dupObject

Public


64
65
66
# File 'lib/faraday/options.rb', line 64

def deep_dup
  self.class.from(self)
end

#delete(key) ⇒ Object

Public


36
37
38
39
40
# File 'lib/faraday/options.rb', line 36

def delete(key)
  value = send(key)
  send("#{key}=", nil)
  value
end

#eachObject

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_keyObject

Public


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

def each_key
  return to_enum(:each_key) unless block_given?
  keys.each do |key|
    yield(key)
  end
end

#each_valueObject

Public


114
115
116
117
118
119
# File 'lib/faraday/options.rb', line 114

def each_value
  return to_enum(:each_value) unless block_given?
  values.each do |value|
    yield(value)
  end
end

#empty?Boolean

Public

Returns:

  • (Boolean)

94
95
96
# File 'lib/faraday/options.rb', line 94

def empty?
  keys.empty?
end

#fetch(key, *args) ⇒ Object

Public


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/faraday/options.rb', line 69

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

#inspectObject

Internal


139
140
141
142
143
144
145
146
147
148
# File 'lib/faraday/options.rb', line 139

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)

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

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

#keysObject

Public


89
90
91
# File 'lib/faraday/options.rb', line 89

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

#merge(other) ⇒ Object

Public


59
60
61
# File 'lib/faraday/options.rb', line 59

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

#merge!(other) ⇒ Object

Public


48
49
50
51
52
53
54
55
56
# File 'lib/faraday/options.rb', line 48

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

#symbolized_key_setObject


185
186
187
# File 'lib/faraday/options.rb', line 185

def symbolized_key_set
  @symbolized_key_set ||= Set.new(keys.map { |k| k.to_sym })
end

#to_hashObject

Public


129
130
131
132
133
134
135
136
# File 'lib/faraday/options.rb', line 129

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


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/faraday/options.rb', line 19

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

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

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

Public

Returns:

  • (Boolean)

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

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

#values_at(*keys) ⇒ Object

Public


84
85
86
# File 'lib/faraday/options.rb', line 84

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