Sha256: 61cf2b4d7e5126e1755dcbd4f150be87e47b06492fa330e529899d286b15643b

Contents?: true

Size: 805 Bytes

Versions: 6

Compression:

Stored size: 805 Bytes

Contents

# frozen_string_literal: true
# Add two methods to Hash
class Hash
  # http://chrisholtz.com/blog/lets-make-a-ruby-hash-map-method-that-returns-a-hash-instead-of-an-array/
  def hmap
    inject({}) do |hash, (k, v)|
      hash.merge(yield(k, v))
    end
  end

  # Interpolate the data in the ctx hash into any String values
  def interpolate(ctx)
    hmap do |key, value|
      if value.is_a? String
        { key => value % ctx }
      else
        { key => value }
      end
    end
  end
end

# Add a method to Array
class Array
  # Interpolate the data in the ctx hash for each String & Hash item
  def interpolate(ctx)
    map do |entry|
      if entry.is_a? Hash
        entry.interpolate ctx
      elsif entry.is_a? String
        entry % @ctx
      else
        entry
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cyclid-0.2.5 app/cyclid/monkey_patches.rb
cyclid-0.2.4 app/cyclid/monkey_patches.rb
cyclid-0.2.3 app/cyclid/monkey_patches.rb
cyclid-0.2.2 app/cyclid/monkey_patches.rb
cyclid-0.2.1 app/cyclid/monkey_patches.rb
cyclid-0.2.0 app/cyclid/monkey_patches.rb