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 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 collapse
- ROOT =
The root directory of the library
File.absolute_path(__dir__ + "/../")
Class Method Summary collapse
-
.benchmark(message: nil, precision: 0, &block) ⇒ Float|String
Measure the time in milliseconds required to execute the given block.
-
.find_class(cls, scope = "::@", only_in_scope = false) ⇒ Class
Finds a class to instantiate.
-
.load!(*what) ⇒ Settings
Loads the extensions.
-
.load_boolean ⇒ Object
Loads Boolean extensions.
-
.load_datetime ⇒ Object
Loads DateTime extensions.
-
.load_hash ⇒ Object
Loads Hash extensions.
-
.load_math ⇒ Object
Loads Math extensions.
-
.load_object ⇒ Object
Loads Object extensions.
-
.load_pathname ⇒ Object
Loads Pathname extensions.
-
.load_string ⇒ Object
Loads String extensions.
-
.loaded_modules ⇒ Array
Returns the list of loaded Lazier modules.
-
.modules_loaded?(*modules) ⇒ Boolean
Checks if all of the specified modules have been loaded.
-
.platform(force = false) ⇒ Boolean, Symbol
Returns which platform are we running on.
-
.settings ⇒ Settings
Returns the settings for the extensions.
Class Method Details
.benchmark(message: nil, precision: 0, &block) ⇒ Float|String
Measure the time in milliseconds required to execute the given block.
160 161 162 163 |
# File 'lib/lazier.rb', line 160 def self.benchmark(message: nil, precision: 0, &block) rv = Benchmark.ms(&block) ? format("%s (%0.#{precision}f ms)", , rv) : rv end |
.find_class(cls, scope = "::@", only_in_scope = false) ⇒ Class
Finds a class to instantiate.
143 144 145 146 147 148 149 150 151 |
# File 'lib/lazier.rb', line 143 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.
56 57 58 59 60 61 62 63 |
# File 'lib/lazier.rb', line 56 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 ⇒ Object
Loads Boolean extensions.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lazier.rb', line 72 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 ⇒ Object
Loads DateTime extensions.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/lazier.rb', line 99 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 ⇒ Object
Loads Hash extensions.
89 90 91 92 93 94 95 96 |
# File 'lib/lazier.rb', line 89 def self.load_hash Lazier.load_object perform_load(:hash) do clean_hash_compact ::Hash.class_eval { include ::Lazier::Hash } end end |
.load_math ⇒ Object
Loads Math extensions.
112 113 114 115 |
# File 'lib/lazier.rb', line 112 def self.load_math Lazier.load_object perform_load(:math, ::Math, ::Lazier::Math) end |
.load_object ⇒ Object
Loads Object extensions.
66 67 68 69 |
# File 'lib/lazier.rb', line 66 def self.load_object Lazier.load_boolean perform_load(:object, ::Object, ::Lazier::Object) end |
.load_pathname ⇒ Object
Loads Pathname extensions.
118 119 120 |
# File 'lib/lazier.rb', line 118 def self.load_pathname perform_load(:pathname, ::Pathname, ::Lazier::Pathname) end |
.load_string ⇒ Object
Loads String extensions.
84 85 86 |
# File 'lib/lazier.rb', line 84 def self.load_string perform_load(:string, ::String, ::Lazier::String) end |
.loaded_modules ⇒ Array
Returns the list of loaded Lazier modules.
125 126 127 |
# File 'lib/lazier.rb', line 125 def self.loaded_modules @loaded || [] end |
.modules_loaded?(*modules) ⇒ Boolean
Checks if all of the specified modules have been loaded.
132 133 134 |
# File 'lib/lazier.rb', line 132 def self.modules_loaded?(*modules) (modules.flatten.uniq.compact.map(&:to_sym) - @loaded).blank? end |
.platform(force = false) ⇒ Boolean, Symbol
Returns which platform are we running on. Can be :java
, :osx
, :posix
or :win32
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/lazier.rb', line 169 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 |