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/version.rb,
lib/lazier/boolean.rb,
lib/lazier/timezone.rb,
lib/lazier/datetime.rb,
lib/lazier/settings.rb,
lib/lazier/pathname.rb,
lib/lazier/localizer.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, I18n, Math, Object, Pathname, String, TimeZone, Version Classes: Configuration, Localizer, Settings

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



140
141
142
143
# File 'lib/lazier.rb', line 140

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 = "::%CLASS%", 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: "::%CLASS%")

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

  • only_in_scope (Boolean) (defaults to: false)

    If only try to instantiate the class in the scope.

Returns:

  • (Class)

    The found class.



123
124
125
126
127
128
129
130
131
# File 'lib/lazier.rb', line 123

def self.find_class(cls, scope = "::%CLASS%", only_in_scope = false)
  if cls.is_a?(::String) || cls.is_a?(::Symbol)
    rv, cls = perform_initial_class_search(cls, only_in_scope)
    rv = search_class_inside_scope(rv, cls, scope) # Search inside scope
    rv || raise(NameError.new("", 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.



51
52
53
54
55
56
57
# File 'lib/lazier.rb', line 51

def self.load!(*what)
  modules = what.present? ? what.flatten.uniq.compact.map(&:to_s) : %w(object boolean string hash datetime math pathname)
  modules.each { |w| ::Lazier.send("load_#{w}") }

  yield if block_given?
  ::Lazier::Settings.instance
end

+ (Object) load_boolean

Loads Boolean extensions.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lazier.rb', line 65

def self.load_boolean
  ::TrueClass.class_eval do
    include ::Lazier::Object
    include ::Lazier::Boolean
  end

  ::FalseClass.class_eval do
    include ::Lazier::Object
    include ::Lazier::Boolean
  end
end

+ (Object) load_datetime

Loads DateTime extensions.



94
95
96
97
98
99
100
101
102
# File 'lib/lazier.rb', line 94

def self.load_datetime
  Lazier.load_object

  [::Time, ::Date, ::DateTime].each do |c|
    c.class_eval { include ::Lazier::DateTime }
  end

  ::ActiveSupport::TimeZone.class_eval { include ::Lazier::TimeZone }
end

+ (Object) load_hash

Loads Hash extensions.



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

def self.load_hash
  clean_hash_compact
  ::Hash.class_eval { include ::Lazier::Hash }
end

+ (Object) load_hash_method_access

Loads Hash method access extensions.



89
90
91
# File 'lib/lazier.rb', line 89

def self.load_hash_method_access
  ::Hash.class_eval { include Hashie::Extensions::MethodAccess }
end

+ (Object) load_math

Loads Math extensions.



105
106
107
108
# File 'lib/lazier.rb', line 105

def self.load_math
  Lazier.load_object
  ::Math.class_eval { include ::Lazier::Math }
end

+ (Object) load_object

Loads Object extensions.



60
61
62
# File 'lib/lazier.rb', line 60

def self.load_object
  ::Object.class_eval { include ::Lazier::Object }
end

+ (Object) load_pathname

Loads Pathname extensions.



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

def self.load_pathname
  require "pathname"
  ::Pathname.class_eval { include ::Lazier::Pathname }
end

+ (Object) load_string

Loads String extensions.



78
79
80
# File 'lib/lazier.rb', line 78

def self.load_string
  ::String.class_eval { include ::Lazier::String }
end

+ (Settings) settings

Returns the settings for the extensions.

Returns:

  • (Settings)

    The settings for the extensions.



34
35
36
# File 'lib/lazier.rb', line 34

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