Module: Lazier

Defined in:
lib/lazier.rb,
lib/lazier/hash.rb,
lib/lazier/i18n.rb,
lib/lazier/math.rb,
lib/lazier/object.rb,
lib/lazier/string.rb,
lib/lazier/boolean.rb,
lib/lazier/version.rb,
lib/lazier/datetime.rb,
lib/lazier/pathname.rb,
lib/lazier/settings.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 [email protected]. Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/mit.

Defined Under Namespace

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

Constant Summary collapse

ROOT =

The root directory of the library

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

Class Method Summary collapse

Class Method Details

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

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

Parameters:

  • message (String|NilClass) (defaults to: nil)

    An optional message (see return value).

  • precision (Fixnum) (defaults to: 0)

    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.



161
162
163
164
# 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

.clean_hash_compactObject

:nodoc:



183
184
185
186
187
188
# File 'lib/lazier.rb', line 183

def self.clean_hash_compact
  ::Hash.class_eval do
    remove_method(:compact) if {}.respond_to?(:compact)
    remove_method(:compact!) if {}.respond_to?(:compact!)
  end
end

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

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.



144
145
146
147
148
149
150
151
152
# 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) ⇒ Settings

Loads the extensions.

Parameters:

  • what (Array)

    The modules to load. Valid values are:

  • object (Hash)

    a customizable set of options

  • boolean (Hash)

    a customizable set of options

  • string (Hash)

    a customizable set of options

  • hash (Hash)

    a customizable set of options

  • hash_method_access (Hash)

    a customizable set of options

  • datetime (Hash)

    a customizable set of options

  • math (Hash)

    a customizable set of options

  • pathname (Hash)

    a customizable set of options

Returns:

  • (Settings)

    The settings for the extensions.



57
58
59
60
61
62
63
64
# 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_booleanObject

Loads Boolean extensions.



73
74
75
76
77
78
79
80
81
82
# 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_datetimeObject

Loads DateTime extensions.



100
101
102
103
104
105
106
107
108
109
110
# 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_hashObject

Loads Hash extensions.



90
91
92
93
94
95
96
97
# 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_mathObject

Loads Math extensions.



113
114
115
116
# File 'lib/lazier.rb', line 113

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

.load_objectObject

Loads Object extensions.



67
68
69
70
# File 'lib/lazier.rb', line 67

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

.load_pathnameObject

Loads Pathname extensions.



119
120
121
# File 'lib/lazier.rb', line 119

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

.load_stringObject

Loads String extensions.



85
86
87
# File 'lib/lazier.rb', line 85

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

.loaded_modulesArray

Returns the list of loaded Lazier modules.

Returns:

  • (Array)

    The list of loaded Lazier modules.



126
127
128
# File 'lib/lazier.rb', line 126

def self.loaded_modules
  @loaded || []
end

.modules_loaded?(*modules) ⇒ Boolean

Checks if all of the specified modules have been loaded.

Returns:

  • (Boolean)

    true if the all of the specified modules has already been loaded, false otherwise.



133
134
135
# File 'lib/lazier.rb', line 133

def self.modules_loaded?(*modules)
  (modules.flatten.uniq.compact.map(&:to_sym) - @loaded).blank?
end

.perform_load(mod, target = nil, extension = nil, &block) ⇒ Object

:nodoc:



199
200
201
202
203
204
205
206
# File 'lib/lazier.rb', line 199

def self.perform_load(mod, target = nil, extension = nil, &block)
  @loaded ||= []

  unless @loaded.include?(mod)
    block_given? ? block.call : target.class_eval { include extension }
    @loaded << mod
  end
end

.platform(force = false) ⇒ Boolean, Symbol

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

Returns:

  • (Boolean)

    If force detection again.

  • (Symbol)

    The current platform.



170
171
172
173
174
175
176
177
178
179
180
# 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

.search_class(cls, scope = nil) ⇒ Object

:nodoc:



191
192
193
194
195
196
# File 'lib/lazier.rb', line 191

def self.search_class(cls, scope = nil)
  cls = scope.gsub(/%CLASS%|[@%$?]/, cls)
  cls.constantize
rescue
  nil
end

.settingsSettings

Returns the settings for the extensions.

Returns:

  • (Settings)

    The settings for the extensions.



41
42
43
# File 'lib/lazier.rb', line 41

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