-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap
Api#attributes
with indifferent key access
A number of bugs have been introduced or missed due to the way attributes are exposed by fog. Fog transforms the top level of attributes from the JSON sources (String) to Symbols, but does not act recursively. This results in accessing nested structures with a mix of Symbol and String keys which inevitably is missed or incorrectly specced out. This adds a simple wrapper around `Api#attributes` that takes the fog attributes and allows indifferent access with Symbols or Strings.
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# A simpler wrapper to allows either String or Symbol keys to be used | ||
# when accessing attributes since fog applies a change on the top | ||
# level resulting in a mix of both which has introduced issues. | ||
class IndifferentAccessHash | ||
def initialize(hash) | ||
@hash = hash | ||
end | ||
|
||
# @param key [String, Symbol] the key to look up | ||
# @return [Object] the value of the key | ||
def [](key) | ||
value = @hash[key.to_s] || @hash[key.to_sym] | ||
wrap(value) | ||
end | ||
|
||
# @param key [String, Symbol] the key to set | ||
# @param value [Object] the value to set | ||
# @return [Object] the value of the key | ||
def []=(key, value) | ||
@hash[key.to_s] = value | ||
end | ||
|
||
# @param other [Object] the object to compare | ||
# @return [Object] the result of the comparison | ||
def ==(other) | ||
@hash == (other.is_a?(IndifferentAccessHash) ? other.to_h : other) | ||
end | ||
|
||
def method_missing(method, *args, &block) | ||
@hash.send(method, *args, &block) | ||
end | ||
|
||
def to_h | ||
@hash | ||
end | ||
|
||
private | ||
|
||
# This is to handle nested hashes to avoid the original issue again | ||
def wrap(value) | ||
case value | ||
when Hash | ||
IndifferentAccessHash.new(value) | ||
when Array | ||
value.map { |v| wrap(v) } | ||
else | ||
value | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters