Class: Hash

Inherits:
Object show all
Defined in:
lib/simple_mapper/support/core_ext.rb,
lib/simple_mapper/support/bliss_serializer.rb,
lib/simple_mapper/default_plugins/options_to_query.rb

Instance Method Summary collapse

Instance Method Details

#crawl(&block) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple_mapper/support/core_ext.rb', line 44

def crawl(&block)
  raise ArgumentError, "no block given" unless block_given?
  self.each do |k,v|
    case block.arity
    when 1
      yield(v)
    when 2
      yield(k,v)
    when 3
      yield(self,k,v)
      v = self[k]
    end
    if v.is_a?(Array)
      v.crawl(&block)
    elsif v.is_a?(Hash)
      v.crawl(&block)
    end
  end
end

#except(*keys) ⇒ Object



40
41
42
# File 'lib/simple_mapper/support/core_ext.rb', line 40

def except(*keys)
  reject {|k,v| keys.flatten.include?(k) }
end

#stringify_keys(specials = {}) ⇒ Object



19
20
21
# File 'lib/simple_mapper/support/core_ext.rb', line 19

def stringify_keys(specials={})
  self.dup.stringify_keys!(specials)
end

#stringify_keys!(specials = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/simple_mapper/support/core_ext.rb', line 22

def stringify_keys!(specials={})
  self.each_key do |k|
    self[specials.has_key?(k) ? specials[k] : k.to_s] = self.delete(k)
  end
  self
end

#symbolize_keysObject



36
37
38
# File 'lib/simple_mapper/support/core_ext.rb', line 36

def symbolize_keys
  {}.merge(self).symbolize_keys!
end

#symbolize_keys!Object

Symbolize keys



30
31
32
33
34
35
# File 'lib/simple_mapper/support/core_ext.rb', line 30

def symbolize_keys!
  self.each_key do |k|
    self[k.to_sym] = self.delete(k)
  end
  self
end

#to_query(namespace = nil) ⇒ Object



19
20
21
22
23
# File 'lib/simple_mapper/default_plugins/options_to_query.rb', line 19

def to_query(namespace=nil)
  collect do |key, value|
    value.to_query(namespace ? "#{namespace}[#{key}]" : key)
  end * '&'
end

#to_xml(options = {}) ⇒ Object



84
85
86
87
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 84

def to_xml(options={})
  options[:root] = keys.length == 1 ? keys[0] : nil if !options.has_key?(:root)
  Serialize.to_xml(self.slashed, options.merge(:root => (options.has_key?(:root) ? options[:root] : 'xml')))
end