class Hash

Hook helper into toplevel `Hash`

Public Instance Methods

hulk_smash(*args)
Alias for: to_smash
to_smash(*args) click to toggle source

Convert to Smash

@return [Smash]

# File lib/bogo/smash.rb, line 145
def to_smash(*args)
  self.to_type_converter(::Smash, :to_smash, *args)
end
Also aliased as: hulk_smash

Protected Instance Methods

smash_conversion(obj, convert_call, *args) click to toggle source

Convert object to smash if applicable

@param obj [Object] @param convert_call [Symbol] builtin hash convert @return [Smash, Object]

# File lib/bogo/smash.rb, line 188
def smash_conversion(obj, convert_call, *args)
  case obj
  when Hash
    obj.send(convert_call, *args)
  when Array
    obj.map do |i|
      result = smash_conversion(i, convert_call, *args)
      args.include?(:freeze) ? result.freeze : result
    end
  else
    args.include?(:freeze) ? obj.freeze : obj
  end
end
to_type_converter(type, convert_call, *args) click to toggle source

Convert to type

@param type [Class] hash type @param convert_call [Symbol] builtin hash convert @return [Smash]

# File lib/bogo/smash.rb, line 157
def to_type_converter(type, convert_call, *args)
  result = type.new.tap do |smash|
    if(args.include?(:sorted))
      process = self.sort_by do |entry|
        entry.first.to_s
      end
    else
      process = self
    end
    process.each do |k,v|
      if(args.include?(:snake))
        k = Bogo::Utility.snake(k.to_s)
      elsif(args.include?(:camel))
        k = Bogo::Utility.camel(k.to_s)
      end
      smash[k.is_a?(Symbol) ? k.to_s : k] = smash_conversion(v, convert_call, *args)
    end
  end
  if(args.include?(:freeze))
    result.values.map(&:freeze)
    result.freeze
  else
    result
  end
end