Module: Lazier

Defined in:
lib/lazier.rb,
lib/lazier/i18n.rb,
lib/lazier/hash.rb,
lib/lazier/math.rb,
lib/lazier/string.rb,
lib/lazier/object.rb,
lib/lazier/boolean.rb,
lib/lazier/version.rb,
lib/lazier/settings.rb,
lib/lazier/pathname.rb,
lib/lazier/datetime.rb,
lib/lazier/timezone.rb,
lib/lazier/exceptions.rb,
lib/lazier/configuration.rb

Overview

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 http://www.opensource.org/licenses/mit-license.php.

Defined Under Namespace

Modules: Boolean, DateTime, Exceptions, Hash, Math, Object, Pathname, String, TimeZone, Version Classes: Configuration, I18n, Settings

Constant Summary

ROOT =

The root directory of the library

File.absolute_path(__dir__ + "/../")

Class Method Summary (collapse)

Class Method Details

+ (Float|String) benchmark(message: nil, precision: 0, &block)

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

Parameters:

  • message (String|NilClass)

    An optional message (see return value).

  • precision (Fixnum)

    The precision for the message (see return value)..

  • block (Proc)

    The block to evaluate.

Returns:

  • (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.



144
145
146
147
# File 'lib/lazier.rb', line 144

def self.benchmark(message: nil, precision: 0, &block)
  rv = Benchmark.ms(&block)
  message ? format("%s (%0.#{precision}f ms)", message, rv) : rv
end

+ (Class) find_class(cls, scope = "::@", only_in_scope = false)

Finds a class to instantiate.

Parameters:

  • 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.

  • scope (String) (defaults to: "::@")

    The scope where to find the class. %CLASS%, %, $, ? and @ will be substituted with the class name.

  • only_in_scope (Boolean) (defaults to: false)

    If only search inside the scope.

Returns:

  • (Class)

    The found class.



127
128
129
130
131
132
133
134
135
# File 'lib/lazier.rb', line 127

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

+ (Settings) load!(*what)

Loads the extensions.

Parameters:

  • 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.

Returns:

  • (Settings)

    The settings for the extensions.



54
55
56
57
58
59
60
61
# File 'lib/lazier.rb', line 54

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

+ (Object) load_boolean

Loads Boolean extensions.



70
71
72
73
74
75
76
77
78
79
# File 'lib/lazier.rb', line 70

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

+ (Object) load_datetime

Loads DateTime extensions.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lazier.rb', line 97

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

+ (Object) load_hash

Loads Hash extensions.



87
88
89
90
91
92
93
94
# File 'lib/lazier.rb', line 87

def self.load_hash
  Lazier.load_object

  perform_load(:hash) do
    clean_hash_compact
    ::Hash.class_eval { include ::Lazier::Hash }
  end
end

+ (Object) load_math

Loads Math extensions.



110
111
112
113
# File 'lib/lazier.rb', line 110

def self.load_math
  Lazier.load_object
  perform_load(:math, ::Math, ::Lazier::Math)
end

+ (Object) load_object

Loads Object extensions.



64
65
66
67
# File 'lib/lazier.rb', line 64

def self.load_object
  Lazier.load_boolean
  perform_load(:object, ::Object, ::Lazier::Object)
end

+ (Object) load_pathname

Loads Pathname extensions.



116
117
118
# File 'lib/lazier.rb', line 116

def self.load_pathname
  perform_load(:pathname, ::Pathname, ::Lazier::Pathname)
end

+ (Object) load_string

Loads String extensions.



82
83
84
# File 'lib/lazier.rb', line 82

def self.load_string
  perform_load(:string, ::String, ::Lazier::String)
end

+ (Boolean, Symbol) platform(force = false)

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

Returns:

  • (Boolean)

    If force detection again.

  • (Symbol)

    The current platform.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/lazier.rb', line 153

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) settings

Returns the settings for the extensions.

Returns:

  • (Settings)

    The settings for the extensions.



37
38
39
# File 'lib/lazier.rb', line 37

def self.settings
  ::Lazier::Settings.instance
end