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
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
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
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
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
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
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
Loads `Math` extensions.
# File lib/lazier.rb, line 113 def self.load_math Lazier.load_object perform_load(:math, ::Math, ::Lazier::Math) end
Loads `Object` extensions.
# File lib/lazier.rb, line 67 def self.load_object Lazier.load_boolean perform_load(:object, ::Object, ::Lazier::Object) end
Loads `Pathname` extensions.
# File lib/lazier.rb, line 119 def self.load_pathname perform_load(:pathname, ::Pathname, ::Lazier::Pathname) end
Loads `String` extensions.
# File lib/lazier.rb, line 85 def self.load_string perform_load(:string, ::String, ::Lazier::String) end
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
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
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