Module: Lazier
- Defined in:
- lib/lazier.rb,
lib/lazier/hash.rb,
lib/lazier/math.rb,
lib/lazier/i18n.rb,
lib/lazier/object.rb,
lib/lazier/string.rb,
lib/lazier/version.rb,
lib/lazier/boolean.rb,
lib/lazier/pathname.rb,
lib/lazier/settings.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
-
.benchmark(message: nil, precision: 0, &block) ⇒ Float|String
Measure the time in milliseconds required to execute the given block.
-
.clean_hash_compact ⇒ Object
:nodoc:.
-
.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.
-
.perform_load(mod, target = nil, extension = nil, &block) ⇒ Object
:nodoc:.
-
.platform(force = false) ⇒ Boolean, Symbol
Returns which platform are we running on.
-
.search_class(cls, scope = nil) ⇒ Object
:nodoc:.
-
.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.
161 162 163 164 |
# File 'lib/lazier.rb', line 161 def self.benchmark(message: nil, precision: 0, &block) rv = Benchmark.ms(&block) ? format("%s (%0.#{precision}f ms)", , rv) : rv end |
.clean_hash_compact ⇒ Object
: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.
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.
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_boolean ⇒ Object
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_datetime ⇒ Object
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_hash ⇒ Object
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_math ⇒ Object
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_object ⇒ Object
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_pathname ⇒ Object
Loads Pathname
extensions.
119 120 121 |
# File 'lib/lazier.rb', line 119 def self.load_pathname perform_load(:pathname, ::Pathname, ::Lazier::Pathname) end |
.load_string ⇒ Object
Loads String
extensions.
85 86 87 |
# File 'lib/lazier.rb', line 85 def self.load_string perform_load(:string, ::String, ::Lazier::String) end |
.loaded_modules ⇒ Array
Returns 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.
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
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 |
.settings ⇒ Settings
Returns the settings for the extensions.
41 42 43 |
# File 'lib/lazier.rb', line 41 def self.settings ::Lazier::Settings.instance end |