Class: Bogo::Smash
- Includes:
- Hashie::Extensions::Coercion, Hashie::Extensions::DeepMerge, Hashie::Extensions::IndifferentAccess, Hashie::Extensions::MergeInitializer
- Defined in:
- lib/bogo/smash.rb
Overview
Customized Hash
Defined Under Namespace
Classes: NoValue
Constant Summary collapse
Instance Method Summary collapse
-
#checksum ⇒ String
Calculate checksum of hash (sha256).
-
#fetch(*args) ⇒ Object
Fetch value at given path or return a default value.
-
#get(*args) ⇒ Object, NilClass
Get value at given path.
-
#initialize(*args) ⇒ Smash
constructor
Create new instance.
- #merge(hash) ⇒ Object
- #merge!(hash) ⇒ Object
-
#retrieve(*args) ⇒ Object, NoValue
Get value at given path.
-
#set(*args) ⇒ Object
Set value at given path.
-
#to_hash(*args) ⇒ Hash
Convert to Hash.
Methods inherited from Hash
Constructor Details
Instance Method Details
#checksum ⇒ String
Calculate checksum of hash (sha256)
107 108 109 |
# File 'lib/bogo/smash.rb', line 107 def checksum Digest::SHA256.hexdigest(self.to_smash(:sorted).to_s) end |
#fetch(*args) ⇒ Object
Fetch value at given path or return a default value
71 72 73 74 75 |
# File 'lib/bogo/smash.rb', line 71 def fetch(*args) default_value = args.pop val = retrieve(*args) val == NO_VALUE ? default_value : val end |
#get(*args) ⇒ Object, NilClass
Get value at given path
62 63 64 65 |
# File 'lib/bogo/smash.rb', line 62 def get(*args) val = retrieve(*args) val == NO_VALUE ? nil : val end |
#merge(hash) ⇒ Object
39 40 41 42 |
# File 'lib/bogo/smash.rb', line 39 def merge(hash) hash = hash.to_smash super(hash) end |
#merge!(hash) ⇒ Object
34 35 36 37 |
# File 'lib/bogo/smash.rb', line 34 def merge!(hash) hash = hash.to_smash super(hash) end |
#retrieve(*args) ⇒ Object, NoValue
Get value at given path
48 49 50 51 52 53 54 55 56 |
# File 'lib/bogo/smash.rb', line 48 def retrieve(*args) args.inject(self) do |memo, key| if(memo.is_a?(Hash) && memo.to_smash.has_key?(key.to_s)) memo.to_smash[key] else NO_VALUE end end end |
#set(*args) ⇒ Object
Set value at given path
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bogo/smash.rb', line 81 def set(*args) unless(args.size > 1) raise ArgumentError.new 'Set requires at least one key and a value' end value = args.pop set_key = args.pop leaf = args.inject(self) do |memo, key| unless(memo[key].is_a?(Hash)) memo[key] = Smash.new end memo[key] end leaf[set_key] = value value end |