Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/netzke/core/ruby_ext/hash.rb

Direct Known Subclasses

Netzke::Core::EndpointResponse

Instance Method Summary collapse

Instance Method Details

#netzke_deep_map(&block) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/netzke/core/ruby_ext/hash.rb', line 2

def netzke_deep_map(&block)
  self.dup.tap do |h|
    h.each_pair do |k,v|
      h[k] = v.netzke_deep_map(&block) if v.respond_to?('netzke_deep_map')
    end
  end
end

#netzke_deep_replace(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/netzke/core/ruby_ext/hash.rb', line 10

def netzke_deep_replace(&block)
  self.dup.tap do |h|
    h.each_pair do |k,v|
      if v.is_a?(Hash)
        res = yield(v)
        if res == v # no changes, need to go further down
          h[k] = v.netzke_deep_replace(&block) if v.respond_to?('netzke_deep_replace')
        else
          h[k] = res
        end
      else
        if v.is_a?(Array)
          h[k] = v.netzke_deep_replace(&block)
        end
      end
    end
  end
end

#netzke_jsonifyObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/netzke/core/ruby_ext/hash.rb', line 29

def netzke_jsonify
  self.inject({}) do |h,(k,v)|
    new_key = if k.is_a?(Netzke::Core::JsonLiteral)
                k
              elsif k.is_a?(String)
                k.camelize(:lower)
              elsif k.is_a?(Symbol)
                k.to_s.camelize(:lower).to_sym
              else
                k
              end

    new_value = case v
                when Array, Hash
                  v.netzke_jsonify
                when Class, Proc
                  v.to_s
                else
                  v
                end

    h.merge(new_key => new_value)
  end
end

#netzke_literalize_keysObject



64
65
66
67
# File 'lib/netzke/core/ruby_ext/hash.rb', line 64

def netzke_literalize_keys
  netzke_update_keys{ |k| Netzke::Core::JsonLiteral.new(k.to_s) }
  self
end

#netzke_update_keysObject



55
56
57
58
59
60
61
62
# File 'lib/netzke/core/ruby_ext/hash.rb', line 55

def netzke_update_keys #:yield:
  if block_given?
    keys.each { |old_key| store(yield(old_key), delete(old_key)) }
  else
    to_enum(:netzke_update_keys)
  end
  self
end