Skip to content

Commit

Permalink
Option method access
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Dec 30, 2012
1 parent 03504db commit ac1d9e4
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/faraday/options.rb
Expand Up @@ -2,28 +2,19 @@ module Faraday
# Subclasses Struct with some special helpers for converting from a Hash to
# a Struct.
class Options < Struct
# Public
def self.from(value)
value ? new.update(value) : new
end

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

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

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

# Public
def each(&block)
members.each do |key|
block.call key.to_sym, send(key)
end
end

# Public
def update(obj)
obj.each do |key, value|
next unless value
Expand All @@ -43,29 +34,35 @@ def update(obj)
self
end

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

# Public
def merge(value)
dup.update(value)
end

# Public
def fetch(key, default = nil)
send(key) || send("#{key}=", default ||
(block_given? ? Proc.new.call : nil))
end

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

# Public
def keys
members.reject { |m| send(m).nil? }
end

# Public
def to_hash
hash = {}
members.each do |key|
Expand All @@ -75,6 +72,7 @@ def to_hash
hash
end

# Internal
def inspect
values = []
members.each do |m|
Expand All @@ -85,6 +83,21 @@ def inspect

%(#<#{self.class}#{values}>)
end

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

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

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

class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
Expand Down

0 comments on commit ac1d9e4

Please sign in to comment.