Skip to content

Commit

Permalink
allow custom env keys in an embedded hash
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Oct 13, 2013
1 parent 7dff048 commit c9c1c4c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
41 changes: 41 additions & 0 deletions lib/faraday/options.rb
Expand Up @@ -205,6 +205,22 @@ class Env < Options.new(:method, :body, :url, :request, :request_headers,

def_delegators :request, :params_encoder

def [](key)
if in_member_set?(key)
super(key)
else
custom_members[key]
end
end

def []=(key, value)
if in_member_set?(key)
super(key, value)
else
custom_members[key] = value
end
end

def success?
SuccessfulStatuses.include?(status)
end
Expand All @@ -225,5 +241,30 @@ def parse_body?
def parallel?
!!parallel_manager
end

def custom_members
@custom_members ||= {}
end

def in_member_set?(key)
member_set.include?(key.to_sym)
end

def member_set
@member_set ||= Set.new(members)
end

def inspect
attrs = [nil]
members.each do |mem|
if value = send(mem)
attrs << "@#{mem}=#{value.inspect}"
end
end
if !custom_members.empty?
attrs << "@custom=#{custom_members.inspect}"
end
%(#<#{self.class}#{attrs.join(" ")}>)
end
end
end
22 changes: 21 additions & 1 deletion test/options_test.rb
Expand Up @@ -143,5 +143,25 @@ def test_merge
assert_equal 1, options.a
assert_nil options.b
end
end

def test_env_access_member
e = Faraday::Env.new
assert_nil e.method
e.method = :get
assert_equal :get, e.method
end

def test_env_access_symbol_non_member
e = Faraday::Env.new
assert_nil e[:custom]
e[:custom] = :boom
assert_equal :boom, e[:custom]
end

def test_env_access_string_non_member
e = Faraday::Env.new
assert_nil e["custom"]
e["custom"] = :boom
assert_equal :boom, e["custom"]
end
end

0 comments on commit c9c1c4c

Please sign in to comment.