Sha256: 18f8b0e92f75cdf08de0655587db08c3bf84ad21777c40f29edacb1d76f5b6b3

Contents?: true

Size: 1.57 KB

Versions: 12

Compression:

Stored size: 1.57 KB

Contents

require 'time'

def silence_warnings
  old_verbose, $VERBOSE = $VERBOSE, nil
  yield
ensure
  $VERBOSE = old_verbose
end

class Object
  def blank?
    respond_to?(:empty?) ? empty? : !self
  end

  def present?
    !blank?
  end

  def try(m, *args)
    send(m, *args) if respond_to?(m)
  end
end

class NilClass
  def try(*args)
  end
end

class String
  def underscore
    word = self.dup
    word.gsub!('::', '/')
    word.gsub!(/(?:([A-Za-z\d])|^)((?=a)b)(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
    word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    word.tr!('-', '_')
    word.downcase!
    word
  end
end

class Array
  def extract_options!
    self[-1].is_a?(Hash) ? self.pop : {}
  end
end

class Numeric
  def percents
    self
  end
  alias_method :percent, :percents

  def seconds
    self
  end
  alias_method :second, :seconds

  def minutes
    self * 60
  end
  alias_method :minute, :minutes

  def hours
    self * 3600
  end
  alias_method :hour, :hours

  def days
    self * 86_400
  end
  alias_method :day, :days

  def weeks
    self * 86_400 * 7
  end
  alias_method :week, :weeks

  def ago
    ::Time.now - self
  end

  def bytes
    self
  end
  alias_method :byte, :bytes

  def kilobytes
    self * 1024
  end
  alias_method :kilobyte, :kilobytes

  def megabytes
    self * 1024 * 1024
  end
  alias_method :megabyte, :megabytes

  def gigabytes
    self * 1024 * 1024 * 1024
  end
  alias_method :gigabyte, :gigabytes

  def terabytes
    self * 1024 * 1024 * 1024 * 1024
  end
  alias_method :terabyte, :terabytes
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
eye-0.10.1.pre lib/eye/utils/mini_active_support.rb
eye-0.10.0 lib/eye/utils/mini_active_support.rb
eye-0.10.0.alpha2 lib/eye/utils/mini_active_support.rb
eye-0.10.0.alpha lib/eye/utils/mini_active_support.rb
eye-0.9.4.nosigar lib/eye/utils/mini_active_support.rb
eye-0.9.4 lib/eye/utils/mini_active_support.rb
eye-0.9.3 lib/eye/utils/mini_active_support.rb
eye-0.9.2.nosigar lib/eye/utils/mini_active_support.rb
eye-0.9.2 lib/eye/utils/mini_active_support.rb
eye-0.9.1 lib/eye/utils/mini_active_support.rb
eye-0.9.pre lib/eye/utils/mini_active_support.rb
eye-0.8.1 lib/eye/utils/mini_active_support.rb