Sha256: a72b4e6e8cf2142242de125294f7b09178844507d6c043534369bc5b609ed319

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# stabby lambdas "= ->"" is same as lambda { |params ... }
params_to_hash = lambda { |params|
  "".tap do |s|
    params.each_with_index do |(k, v), i|
      if v.is_a? Hash
        s << ":#{k} => {\n"
        s << "#{params_to_hash.call(v)}".indent(2)
        s << "\n}"
      else
        if v.is_a? String
          s << ":#{k} => '#{v}'"
        else
          s << ":#{k} => #{v}"
        end
      end
      if i != params.length - 1
        s << ",\n"
      end
    end
  end
}

params_to_hash_for_args = lambda { |payload|
  "".tap do |s|
    payload.each_with_index do |(k, v), i|
      s << "'#{k.to_sym}' => '#{v}'".indent(2)
      if i != payload.length - 1
        s << ",\n"
      end
    end
  end
}

@helpers = {:params_to_hash => params_to_hash,
            :params_to_hash_for_args => params_to_hash_for_args}

String.class_eval do
  def indent(count, char = ' ')
    gsub(/([^\n]*)(\n|$)/) do |match|
      last_iteration = ($1 == "" && $2 == "")
      line = ""
      line << (char * count) unless last_iteration
      line << $1
      line << $2
      line
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
balanced-0.7.5 scenarios/helpers.rb