Sha256: c0479841f78d88cfb71b9b128a10e9d2f303db08fd7082f98fbbc14a2ea05a5b

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

require 'bigdecimal'
require 'guerrilla_patch/allocate'

module Kernel
  def auto_assign(caller_binding)
    method(caller[0][/`.*'/][1..-2]).parameters.each { |arg| instance_eval("@#{arg[1]} = caller_binding.eval(arg[1].to_s)") }
  end

  def let(name, &block)
    define_method(name, &block)
  end

  def let_self(name, &block)
    define_singleton_method(name, &block)
  end

  def when_present(item, &block)
    if block_given?
      item.nil? ? '' : block.call(item)
    else
      item
    end
  end

  def consists_of
    PoorsManStringBuilder.new.tap { |builder| yield(builder) }.result
  end

  class PoorsManStringBuilder
    attr_reader :result

    def initialize
      @result = ''
    end

    def add(item)
      @result << item.to_s
    end
    alias :always :add

    def when_present(item, &block)
      @result << Kernel.when_present(item, &block).to_s
    end

    def when(item, &block)
      return unless item
      @result << Kernel.when_present(item, &block).to_s
    end
  end
end

#add sum_me to array, with or without name
class Array
  def sum_me(name = nil)
    self.map(&name).reduce(0, :+)
  end
end

class Hash
  def negative
    Hash[self.map { |key,value| [key, -value]}]
  end
end

class Float
  def divide(ratios)
    DivideByType.divide(ratios, self)
  end

  def negative
    -self
  end
end

class BigDecimal
  def divide(ratios)
    DivideByType.divide(ratios, self)
  end
end

class Fixnum
  def negative
    -self
  end

  def divide(ratios)
    DivideByType.divide(ratios, self)
  end

end

class BigDecimal
  def negative
    -self
  end
end

class Hash
  def sum_me
    self.each_value.reduce(0,:+)
  end
end


module Kernel
  def to_d
    BigDecimal.new(self.to_s)
  end

  def allocate_evenly(number_of_slices)
    Allocate.evenly(self.to_d, number_of_slices)
  end

  def allocate(ratios)
    Allocate.new(self.to_d, ratios).divided
  end
end



Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guerrilla_patch-3.0.3 lib/guerrilla_patch/kernel.rb
guerrilla_patch-3.0.2 lib/guerrilla_patch/kernel.rb
guerrilla_patch-2.8.7 lib/guerrilla_patch/kernel.rb
guerrilla_patch-2.8.6 lib/guerrilla_patch/kernel.rb