class Faraday::Options

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

Public Class Methods

Internal

# File lib/faraday/options.rb, line 166
def self.attribute_options
  @attribute_options ||= {}
end
# 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

Public

# File lib/faraday/options.rb, line 8
def self.from(value)
  value ? new.update(value) : new
end
Calls superclass method
# File lib/faraday/options.rb, line 199
def self.inherited(subclass)
  super
  subclass.attribute_options.update(attribute_options)
  subclass.memoized_attributes.update(memoized_attributes)
end
# 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
# File lib/faraday/options.rb, line 182
def self.memoized_attributes
  @memoized_attributes ||= {}
end

Internal

# File lib/faraday/options.rb, line 156
def self.options(mapping)
  attribute_options.update(mapping)
end

Internal

# File lib/faraday/options.rb, line 161
def self.options_for(key)
  attribute_options[key]
end

Public Instance Methods

Calls superclass method
# 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

Public

# File lib/faraday/options.rb, line 46
def clear
  members.each { |member| delete(member) }
end

Public

# File lib/faraday/options.rb, line 71
def deep_dup
  self.class.from(self)
end

Public

# File lib/faraday/options.rb, line 39
def delete(key)
  value = send(key)
  send(:"#{key}=", nil)
  value
end

Public

# 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

Public

# File lib/faraday/options.rb, line 106
def each_key(&block)
  return to_enum(:each_key) unless block

  keys.each(&block)
end

Public

# File lib/faraday/options.rb, line 120
def each_value(&block)
  return to_enum(:each_value) unless block

  values.each(&block)
end

Public

# File lib/faraday/options.rb, line 101
def empty?
  keys.empty?
end

Public

# 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

Internal

# 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

Public

# File lib/faraday/options.rb, line 113
def key?(key)
  keys.include?(key)
end
Also aliased as: has_key?

Public

# File lib/faraday/options.rb, line 96
def keys
  members.reject { |member| send(member).nil? }
end

Public

# File lib/faraday/options.rb, line 66
def merge(other)
  dup.merge!(other)
end

Public

# 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
# File lib/faraday/options.rb, line 195
def symbolized_key_set
  @symbolized_key_set ||= Set.new(keys.map(&:to_sym))
end

Public

# 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

Public

# 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

Public

# File lib/faraday/options.rb, line 127
def value?(value)
  values.include?(value)
end
Also aliased as: has_value?

Public

# File lib/faraday/options.rb, line 91
def values_at(*keys)
  keys.map { |key| send(key) }
end