class MovieMasher::Hashable
Base class for mocking a Hash.
Attributes
hash[RW]
identifier[RW]
Public Class Methods
new(hash = nil)
click to toggle source
Set the actual Hash when creating.
# File lib/util/hashable.rb, line 112 def initialize(hash = nil) unless hash.is_a?(Hash) # puts "Hashable#initialize NOT HASH #{hash}" hash = {} end @hash = hash @identifier = SecureRandom.uuid end
resolved_hash(hash_or_path)
click to toggle source
# File lib/util/hashable.rb, line 7 def resolved_hash(hash_or_path) data = {} case hash_or_path when String hash_or_path = __resolved_string(hash_or_path) if hash_or_path begin case __string_type(hash_or_path) when 'yaml' data = YAML.load(hash_or_path) when 'json' data = JSON.parse(hash_or_path) else data[:error] = "unsupported configuration type #{hash_or_path}" end rescue StandardError => e data[:error] = "job file could not be parsed: #{e.message}" end else data[:error] = "job file could not be found: #{hash_or_path}" end when Hash data = Marshal.load(Marshal.dump(hash_or_path)) end symbolize(data) end
symbolize(hash_or_array, key = nil)
click to toggle source
# File lib/util/hashable.rb, line 34 def symbolize(hash_or_array, key = nil) result = hash_or_array case hash_or_array when Hash result = {} hash_or_array.each do |k, v| k = k.downcase if k.is_a?(String) && key != :parameters k = k.to_sym result[k] = symbolize(v, k) end when Array result = hash_or_array.map { |v| symbolize(v) } end result end
Public Instance Methods
[](symbol)
click to toggle source
[]=(symbol, value)
click to toggle source
_get(symbol)
click to toggle source
# File lib/util/hashable.rb, line 171 def _get(symbol) @hash[symbol] end
_set(symbol, value)
click to toggle source
# File lib/util/hashable.rb, line 166 def _set(symbol, value) symbol = symbol.to_s[0..-2].to_sym @hash[symbol] = value end
keys()
click to toggle source
# File lib/util/hashable.rb, line 121 def keys @hash.keys end
slice(*keys)
click to toggle source
# File lib/util/hashable.rb, line 125 def slice(*keys) @hash.slice(*keys) end
to_hash()
click to toggle source
Return deep copy of underlying Hash.
# File lib/util/hashable.rb, line 130 def to_hash Marshal.load(Marshal.dump(@hash)) end
to_json(state = nil)
click to toggle source
Return underlying Hash in JSON format.
# File lib/util/hashable.rb, line 135 def to_json(state = nil) @hash.to_json state end
to_s()
click to toggle source
# File lib/util/hashable.rb, line 139 def to_s @hash.to_s end
values()
click to toggle source
# File lib/util/hashable.rb, line 143 def values @hash.values end