Sha256: d2f60b57acbc18a6bf21cf303347123970f3b0d1efef868755e683f5ac4a585b

Contents?: true

Size: 1.71 KB

Versions: 26

Compression:

Stored size: 1.71 KB

Contents

require 'json'
class Hash
  XML_TYPE_NAMES =
      {
          "Symbol"     => "symbol",
          "Fixnum"     => "integer",
          "Bignum"     => "integer",
          "BigDecimal" => "decimal",
          "Float"      => "float",
          "TrueClass"  => "boolean",
          "FalseClass" => "boolean",
          "Date"       => "date",
          "DateTime"   => "datetime",
          "Time"       => "datetime",
          "ActiveSupport::TimeWithZone" => "datetime"
      }

  XML_FORMATTING =
      {
          "symbol"   => Proc.new { |symbol| symbol.to_s },
          "date"     => Proc.new { |date| date.strftime('%d/%m/%Y') },
          "datetime" => Proc.new { |time| time.xmlschema },
          "yaml"     => Proc.new { |yaml| yaml.to_yaml }
      }

  def limit_to_keys(limit_keys)
    dup.limit_to_keys!(limit_keys)
  end

  def limit_to_keys!(limit_keys)
    keys.each { |key| delete(key) unless limit_keys.include? key }
    self
  end

  def soft_delete(key)
    cloned_hash = deep_clone
    cloned_hash.delete(key)
    cloned_hash
  end

  def deep_clone
    Marshal::load(Marshal.dump(self))
  end

  # Return a new hash with all keys converted to strings.
  def stringify_keys
    dup.stringify_keys!
  end

  # Destructively convert all keys to strings.
  def stringify_keys!
    keys.each do |key|
      self[key.to_s] = delete(key)
    end
    self
  end

  # Return a new hash with all keys converted to symbols, as long as
  # they respond to +to_sym+.
  def symbolize_keys
    dup.symbolize_keys!
  end

  # Destructively convert all keys to symbols, as long as they respond
  # to +to_sym+.
  def symbolize_keys!
    keys.each do |key|
      self[(key.to_sym rescue key) || key] = delete(key)
    end
    self
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
fossil-0.5.26 lib/support/hash_extentions.rb
fossil-0.5.25 lib/support/hash_extentions.rb
fossil-0.5.24 lib/support/hash_extentions.rb
fossil-0.5.23 lib/support/hash_extentions.rb
fossil-0.5.22 lib/support/hash_extentions.rb
fossil-0.5.21 lib/support/hash_extentions.rb
fossil-0.5.20 lib/support/hash_extentions.rb
fossil-0.5.19 lib/support/hash_extentions.rb
fossil-0.5.18 lib/support/hash_extentions.rb
fossil-0.5.17 lib/support/hash_extentions.rb
fossil-0.5.16 lib/support/hash_extentions.rb
fossil-0.5.15 lib/support/hash_extentions.rb
fossil-0.5.14 lib/support/hash_extentions.rb
fossil-0.5.13 lib/support/hash_extentions.rb
fossil-0.5.12 lib/support/hash_extentions.rb
fossil-0.5.11 lib/support/hash_extentions.rb
fossil-0.5.10 lib/support/hash_extentions.rb
fossil-0.5.9 lib/support/hash_extentions.rb
fossil-0.5.8 lib/support/hash_extentions.rb
fossil-0.5.7 lib/hash_extentions.rb