Module: Lazier::Object
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/object.rb
Overview
Extensions for all objects.
Constant Summary
- BOOLEAN_MATCHER =
Expression to match a boolean value.
/^(\s*(1|0|true|false|yes|no|t|f|y|n)\s*)$/i
- BOOLEAN_TRUE_MATCHER =
Expression to match a true value.
/^(\s*(1|true|yes|t|y)\s*)$/i
- INTEGER_MATCHER =
Expression to match a integer value.
/^([+-]?)(\d+)$/
- FLOAT_MATCHER =
Expression to match a float value.
/^([+-]?)(\d+)([.,]\d+)?$/
Instance Method Summary (collapse)
-
- (Boolean) boolean?
(also: #is_boolean?)
Checks if the object is a valid boolean value.
-
- (String) ensure(default_value, verifier = :blank?)
Makes sure that the object is set to something meaningful.
-
- (Array) ensure_array(default_value = nil, uniq = false, compact = false, flatten = false, sanitizer = nil, &block)
Makes sure that the object is an array.
-
- (Hash) ensure_hash(access = nil, default_value = nil, sanitizer = nil, &block)
Makes sure that the object is an hash.
-
- (String) ensure_string(default_value = "", stringifier = :to_s)
Makes sure that the object is a string.
-
- (Boolean) float?
(also: #number?, #is_float?, #is_number?)
Checks if the object is a valid float.
-
- (String) for_debug(format = :yaml, as_exception = true)
Inspects an object.
-
- (String) format_boolean(true_name = nil, false_name = nil)
Formats a boolean.
-
- (String) format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
Formats a number.
-
- (String) indexize(length = 2, filler = "0", formatter = :rjust)
Prepares an object to be printed in list summaries, like
[01/04] Opening this...
. -
- (Boolean) integer?
(also: #is_integer?)
Checks if the object is a valid integer.
-
- (String) normalize_number
Normalizes a number for conversion.
-
- (Boolean) numeric?(klass = Integer, matcher = ::Lazier::Object::INTEGER_MATCHER)
(also: #is_numeric?)
Checks if the object is of a numeric class of matches a numeric string expression.
-
- (Float) round_to_precision(precision = 2)
Returns the rounded float representaton of the object.
-
- (Object|nil) safe_send(method, *args, &block)
Sends a method to the object.
-
- (Boolean) to_boolean
Converts the object to a boolean.
-
- (Float) to_float(default_value = 0.0)
Converts the object to a float.
-
- (Fixnum) to_integer(default_value = 0)
Converts the object to a integer.
Instance Method Details
- (Boolean) boolean? Also known as: is_boolean?
Checks if the object is a valid boolean value.
60 61 62 |
# File 'lib/lazier/object.rb', line 60 def boolean? is_a?(::TrueClass) || !self || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER end |
- (String) ensure(default_value, verifier = :blank?)
Makes sure that the object is set to something meaningful.
80 81 82 83 |
# File 'lib/lazier/object.rb', line 80 def ensure(default_value, verifier = :blank?) valid = block_given? ? yield(self) : send(verifier) !valid ? self : default_value end |
- (Array) ensure_array(default_value = nil, uniq = false, compact = false, flatten = false, sanitizer = nil, &block)
Makes sure that the object is an array. For non array objects, return a single element array containing the object.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lazier/object.rb', line 107 def ensure_array(default_value = nil, uniq = false, compact = false, flatten = false, sanitizer = nil, &block) rv = if is_a?(::Array) dup else default_value || (self.is_a?(NilClass) ? [] : [self]) end rv = manipulate_array(rv, uniq, compact, flatten).map(&(block || sanitizer)) if block_given? || sanitizer manipulate_array(rv, uniq, compact, flatten) end |
- (Hash) ensure_hash(access = nil, default_value = nil, sanitizer = nil, &block)
Makes sure that the object is an hash. For non hash objects, return an hash basing on the default_value
parameter.
129 130 131 132 133 134 135 136 |
# File 'lib/lazier/object.rb', line 129 def ensure_hash(access = nil, default_value = nil, sanitizer = nil, &block) default_value = {} if default_value.is_a?(NilClass) rv = convert_to_hash(default_value) rv = sanitize_hash(rv, sanitizer, block) if block || sanitizer rv.respond_to?(:ensure_access) ? rv.ensure_access(access) : rv end |
- (String) ensure_string(default_value = "", stringifier = :to_s)
Makes sure that the object is a string.
90 91 92 93 94 95 96 |
# File 'lib/lazier/object.rb', line 90 def ensure_string(default_value = "", stringifier = :to_s) if is_a?(NilClass) default_value else block_given? ? yield(self, default_value) : send(stringifier) end end |
- (Boolean) float? Also known as: number?, is_float?, is_number?
Checks if the object is a valid float.
42 43 44 |
# File 'lib/lazier/object.rb', line 42 def float? numeric?(Numeric, ::Lazier::Object::FLOAT_MATCHER) end |
- (String) for_debug(format = :yaml, as_exception = true)
Inspects an object.
221 222 223 224 225 226 227 228 229 |
# File 'lib/lazier/object.rb', line 221 def for_debug(format = :yaml, as_exception = true) rv = case format when :pretty_json then ::JSON.pretty_generate(self) else send("to_#{format}") end as_exception ? raise(::Lazier::Exceptions::Debug, rv) : rv end |
- (String) format_boolean(true_name = nil, false_name = nil)
Formats a boolean.
201 202 203 204 |
# File 'lib/lazier/object.rb', line 201 def format_boolean(true_name = nil, false_name = nil) settings = ::Lazier.settings.boolean_names to_boolean ? (true_name || settings[true]) : (false_name || settings[false]) end |
- (String) format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
Formats a number.
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/lazier/object.rb', line 181 def format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil) if is_number? settings = ::Lazier.settings.format_number add_string ||= settings[:add_string] rv = format("%0.#{[precision || settings[:precision], 0].max}f", to_float).split(".") rv[0].gsub!(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{k_separator || settings[:k_separator]}") rv = rv.join(decimal_separator || settings[:decimal_separator]) add_string ? rv + " #{add_string}" : rv else nil end end |
- (String) indexize(length = 2, filler = "0", formatter = :rjust)
Prepares an object to be printed in list summaries, like [01/04] Opening this...
.
212 213 214 |
# File 'lib/lazier/object.rb', line 212 def indexize(length = 2, filler = "0", formatter = :rjust) ensure_string.send(formatter, length, filler) end |
- (Boolean) integer? Also known as: is_integer?
Checks if the object is a valid integer.
34 35 36 |
# File 'lib/lazier/object.rb', line 34 def integer? numeric?(Integer, ::Lazier::Object::INTEGER_MATCHER) end |
- (String) normalize_number
Normalizes a number for conversion. Basically this methods removes all separator and ensures that .
is used for decimal separator.
27 28 29 |
# File 'lib/lazier/object.rb', line 27 def normalize_number is_boolean? ? to_i.to_s : ensure_string.strip.gsub(/[\.,](?=(.*[\.,]))/, "").gsub(",", ".") end |
- (Boolean) numeric?(klass = Integer, matcher = ::Lazier::Object::INTEGER_MATCHER) Also known as: is_numeric?
Checks if the object is of a numeric class of matches a numeric string expression.
52 53 54 |
# File 'lib/lazier/object.rb', line 52 def numeric?(klass = Integer, matcher = ::Lazier::Object::INTEGER_MATCHER) is_a?(klass) || is_a?(::TrueClass) || !self || normalize_number =~ matcher end |
- (Float) round_to_precision(precision = 2)
Returns the rounded float representaton of the object.
169 170 171 |
# File 'lib/lazier/object.rb', line 169 def round_to_precision(precision = 2) is_number? ? to_float.round([precision, 0].max) : nil end |
- (Object|nil) safe_send(method, *args, &block)
Sends a method to the object. If the objects doesn’t not respond to the method, it returns nil
instead of raising an exception.
71 72 73 |
# File 'lib/lazier/object.rb', line 71 def safe_send(method, *args, &block) respond_to?(method) ? send(method, *args, &block) : nil end |
- (Boolean) to_boolean
Converts the object to a boolean.
141 142 143 |
# File 'lib/lazier/object.rb', line 141 def to_boolean is_a?(TrueClass) || to_integer == 1 || ::Lazier::Object::BOOLEAN_TRUE_MATCHER.match(ensure_string).is_a?(MatchData) end |
- (Float) to_float(default_value = 0.0)
Converts the object to a float.
157 158 159 160 161 162 163 |
# File 'lib/lazier/object.rb', line 157 def to_float(default_value = 0.0) if is_float? ::Kernel.Float(is_a?(::Numeric) ? self : normalize_number) else default_value end end |
- (Fixnum) to_integer(default_value = 0)
Converts the object to a integer.
149 150 151 |
# File 'lib/lazier/object.rb', line 149 def to_integer(default_value = 0) to_float(default_value).to_i end |