Class: Hash

Inherits:
Object show all
Defined in:
lib/sequel/serializer/json_serializer.rb,
lib/support/core_extensions.rb,
lib/sequel/serializer/xml_serializer.rb

Overview

:nodoc:

Constant Summary collapse

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 }
}

Instance Method Summary collapse

Instance Method Details

#deep_cloneObject



102
103
104
# File 'lib/support/core_extensions.rb', line 102

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

#diff(other) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/support/core_extensions.rb', line 134

def diff(other)
  self.keys.inject({}) do |memo, key|
    unless self[key] == other[key]
      memo[key] = other[key]
    end
    memo
  end
end

#limit_to_keys(limit_keys) ⇒ Object



87
88
89
# File 'lib/support/core_extensions.rb', line 87

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

#limit_to_keys!(limit_keys) ⇒ Object



91
92
93
94
# File 'lib/support/core_extensions.rb', line 91

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

#merge_add_values(other_hash) ⇒ Object



143
144
145
146
147
148
# File 'lib/support/core_extensions.rb', line 143

def merge_add_values(other_hash)
  self.each do |k, v|
    self[k] += other_hash[k] if self[k] and other_hash[k]
  end
  self
end

#soft_delete(key) ⇒ Object



96
97
98
99
100
# File 'lib/support/core_extensions.rb', line 96

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

#stringify_keysObject

Return a new hash with all keys converted to strings.



107
108
109
# File 'lib/support/core_extensions.rb', line 107

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object

Destructively convert all keys to strings.



112
113
114
115
116
117
# File 'lib/support/core_extensions.rb', line 112

def stringify_keys!
  keys.each do |key|
    self[key.to_s] = delete(key)
  end
  self
end

#symbolize_keysObject

Return a new hash with all keys converted to symbols, as long as they respond to to_sym.



121
122
123
# File 'lib/support/core_extensions.rb', line 121

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object

Destructively convert all keys to symbols, as long as they respond to to_sym.



127
128
129
130
131
132
# File 'lib/support/core_extensions.rb', line 127

def symbolize_keys!
  keys.each do |key|
    self[(key.to_sym rescue key) || key] = delete(key)
  end
  self
end

#to_fos_json(options = {}) ⇒ Object



121
122
123
# File 'lib/sequel/serializer/json_serializer.rb', line 121

def to_fos_json(options = {})
  to_json({:only=>[]}.merge(options))
end

#to_fos_xml(options = {}) ⇒ Object



480
481
482
# File 'lib/sequel/serializer/xml_serializer.rb', line 480

def to_fos_xml(options = {})
  to_xml({:skip_instruct=>true, :skip_types=>true, :dasherize=>false, :only=>[]}.merge(options))
end