module Lazier

Several Ruby object enhancements.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>. Licensed under the MIT license, which can be found at choosealicense.com/licenses/mit.

Constants

ROOT

The root directory of the library

Public Class Methods

benchmark(message: nil, precision: 0, &block) click to toggle source

Measure the time in milliseconds required to execute the given block.

@param message [String|NilClass] An optional message (see return value). @param precision [Fixnum] The precision for the message (see return value). @param block [Proc] The block to evaluate. @return [Float|String] If a `message` is provided, then the message itself plus the duration under parenthesis will be returned,

otherwise the duration alone as a number.
# File lib/lazier.rb, line 161
def self.benchmark(message: nil, precision: 0, &block)
  rv = Benchmark.ms(&block)
  message ? format("%s (%0.#{precision}f ms)", message, rv) : rv
end
find_class(cls, scope = "::@", only_in_scope = false) click to toggle source

Finds a class to instantiate.

@param cls [Symbol|String|Object] If a `String` or a `Symbol` or a `Class`, then it will be the class to instantiate.

Otherwise the class of the object will returned.

@param scope [String] The scope where to find the class. `%CLASS%`, `%`, `$`, `?` and `@` will be substituted with the class name. @param only_in_scope [Boolean] If only search inside the scope. @return [Class] The found class.

# File lib/lazier.rb, line 144
def self.find_class(cls, scope = "::@", only_in_scope = false)
  if [::String, ::Symbol].include?(cls.class)
    cls = cls.to_s.camelize
    cls.gsub!(/^::/, "") if scope && only_in_scope
    search_class(cls, scope) || raise(NameError, ["", cls])
  else
    cls.is_a?(::Class) ? cls : cls.class
  end
end
load!(*what) { || ... } click to toggle source

Loads the extensions.

@param what [Array] The modules to load. Valid values are: @option object Extensions for all objects. @option boolean Extensions for boolean values. @option string Extensions for strings. @option hash Extensions for hashs. @option hash_method_access Extensions for hash to allow method access. Not included by default. @option datetime Extensions date and time objects. @option math Extensions for Math module. @option pathname Extensions for path objects. @return [Settings] The settings for the extensions.

# File lib/lazier.rb, line 57
def self.load!(*what)
  valid_modules = [:object, :boolean, :string, :hash, :datetime, :math, :pathname]
  modules = what.present? ? what.flatten.uniq.compact.map(&:to_sym) : valid_modules
  (modules & valid_modules).each { |w| ::Lazier.send("load_#{w}") }

  yield if block_given?
  ::Lazier::Settings.instance
end
load_boolean() click to toggle source

Loads `Boolean` extensions.

# File lib/lazier.rb, line 73
def self.load_boolean
  perform_load(:boolean) do
    [::TrueClass, ::FalseClass].each do |klass|
      klass.class_eval do
        include ::Lazier::Object
        include ::Lazier::Boolean
      end
    end
  end
end
load_datetime() click to toggle source

Loads `DateTime` extensions.

# File lib/lazier.rb, line 100
def self.load_datetime
  Lazier.load_object

  perform_load(:datetime) do
    [::Time, ::Date, ::DateTime].each do |c|
      c.class_eval { include ::Lazier::DateTime }
    end

    ::ActiveSupport::TimeZone.class_eval { include ::Lazier::TimeZone }
  end
end
load_hash() click to toggle source

Loads `Hash` extensions.

# File lib/lazier.rb, line 90
def self.load_hash
  Lazier.load_object

  perform_load(:hash) do
    clean_hash_compact
    ::Hash.class_eval { include ::Lazier::Hash }
  end
end
load_math() click to toggle source

Loads `Math` extensions.

# File lib/lazier.rb, line 113
def self.load_math
  Lazier.load_object
  perform_load(:math, ::Math, ::Lazier::Math)
end
load_object() click to toggle source

Loads `Object` extensions.

# File lib/lazier.rb, line 67
def self.load_object
  Lazier.load_boolean
  perform_load(:object, ::Object, ::Lazier::Object)
end
load_pathname() click to toggle source

Loads `Pathname` extensions.

# File lib/lazier.rb, line 119
def self.load_pathname
  perform_load(:pathname, ::Pathname, ::Lazier::Pathname)
end
load_string() click to toggle source

Loads `String` extensions.

# File lib/lazier.rb, line 85
def self.load_string
  perform_load(:string, ::String, ::Lazier::String)
end
loaded_modules() click to toggle source

Returns the list of loaded Lazier modules.

@return [Array] The list of loaded Lazier modules.

# File lib/lazier.rb, line 126
def self.loaded_modules
  @loaded || []
end
modules_loaded?(*modules) click to toggle source

Checks if all of the specified modules have been loaded.

@return [Boolean] `true` if the all of the specified modules has already been loaded, `false` otherwise.

# File lib/lazier.rb, line 133
def self.modules_loaded?(*modules)
  (modules.flatten.uniq.compact.map(&:to_sym) - @loaded).blank?
end
platform(force = false) click to toggle source

Returns which platform are we running on. Can be `:java`, `:osx`, `:posix` or `:win32`

@return [Boolean] If force detection again. @return [Symbol] The current platform.

# File lib/lazier.rb, line 170
def self.platform(force = false)
  @platform = nil if force

  @platform ||=
    case RUBY_PLATFORM
    when /cygwin|mingw|win32/ then :win32
    when /java/ then :java
    when /darwin/ then :osx
    else :posix
    end
end
settings() click to toggle source

Returns the settings for the extensions.

@return [Settings] The settings for the extensions.

# File lib/lazier.rb, line 41
def self.settings
  ::Lazier::Settings.instance
end