Sha256: 957af8b9b9b26b00f988a699fd25122dd5fe703d340fa8863d7bf04094ab1a16

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

class Hash
  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

  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

  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

  # From http://rubyworks.github.com/facets
  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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
netzke-core-6.5.0.0 lib/netzke/core/ruby_ext/hash.rb
netzke-core-6.5.0.0.rc2 lib/netzke/core/ruby_ext/hash.rb
netzke-core-6.5.0.0.rc1 lib/netzke/core/ruby_ext/hash.rb
netzke-core-1.0.1.0 lib/netzke/core/ruby_ext/hash.rb
netzke-core-1.0.0.0 lib/netzke/core/ruby_ext/hash.rb