Sha256: b5a72f8ad90ccb5d41af72a625ed312a7b496354f29e4b438582e64bbafafaff

Contents?: true

Size: 916 Bytes

Versions: 1

Compression:

Stored size: 916 Bytes

Contents

# encoding: utf-8

# HashContext
class HashContext
  attr_reader :receiver
  def initialize(receiver)
    @receiver = receiver
  end

  def method_missing(method_name, *args, &block)
    result = nil
    if args.size > 0
      receiver.reduce({}) do |ret, (key, value)|
        value = value.send method_name, *args
        ret[key] = value
        ret
      end
    else
      receiver.reduce({}) do |ret, (key, value)|
        value = value.send method_name
        ret[key] = value
        ret
      end
    end
  end

  def to_h
    @receiver
  end
end

# Hash
class Hash
  # return HashContext for each execute
  #
  # === Example
  #
  #   h = {key1: "value1", key2: "value2"}
  #   h.>>.upcase # => {key1: "VALUE1", key2: "VALUE2"}
  #   h.>>.+('_hoge') # => {key1: "value1_hoge", key2: "value2_hoge"}
  #
  def >>(dummy = nil)
    HashContext.new(self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/open_classes/hash/gte_gte.rb