Module: Lazier::Object
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActionView::Helpers::NumberHelper
- 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)
-
- (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 = nil, sanitizer = nil, &block)
Makes sure that the object is an array.
-
- (Hash) ensure_hash(access = nil, default_value = nil, sanitizer = nil)
Makes sure that the object is an hash.
-
- (String) ensure_string(default_value = "", stringifier = :to_s)
Makes sure that the object is a string.
-
- (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) is_boolean?
Checks if the object is a valid boolean value.
-
- (Boolean) is_float?
(also: #is_number?)
Checks if the object is a valid float.
-
- (Boolean) is_integer?
Checks if the object is a valid integer.
-
- (String) normalize_number
Normalizes a number for conversion.
-
- (Float) round_to_precision(precision = 2)
Returns the rounded float representaton of 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
- (String) ensure(default_value, verifier = :blank?)
Makes sure that the object is set to something meaningful.
59 60 61 62 |
# File 'lib/lazier/object.rb', line 59 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 = nil, sanitizer = nil, &block)
Makes sure that the object is an array. For non array objects, return a single element array containing the object.
82 83 84 85 86 87 88 89 |
# File 'lib/lazier/object.rb', line 82 def ensure_array(default_value = nil, uniq = false, compact = false, flatten = nil, sanitizer = nil, &block) rv = is_a?(::Array) ? dup : (default_value || [self]) rv.collect!(&(block || sanitizer)) rv.uniq! if uniq rv.compact! if compact rv.flatten! if flatten rv end |
- (Hash) ensure_hash(access = nil, default_value = nil, sanitizer = nil)
Makes sure that the object is an hash. For non hash objects, return an hash basing on the default_value
parameter.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lazier/object.rb', line 97 def ensure_hash(access = nil, default_value = nil, sanitizer = nil) default_value = {} if default_value.nil? rv = if is_a?(::Hash) then self elsif default_value.is_a?(::Hash) then default_value else key = default_value.is_a?(::String) || default_value.is_a?(::Symbol) ? default_value : :key {key => self} end if block_given? || sanitizer then rv = rv.inject({}) {|h, (k, v)| h[k] = block_given? ? yield(v) : v.send(sanitizer) h } end rv.ensure_access(access) end |
- (String) ensure_string(default_value = "", stringifier = :to_s)
Makes sure that the object is a string.
69 70 71 |
# File 'lib/lazier/object.rb', line 69 def ensure_string(default_value = "", stringifier = :to_s) !nil? ? (block_given? ? yield(self, default_value) : send(stringifier)) : default_value end |
- (String) for_debug(format = :yaml, as_exception = true)
Inspects an object.
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/lazier/object.rb', line 196 def for_debug(format = :yaml, as_exception = true) rv = case format when :pretty_json ::JSON.pretty_generate(self) else send("to_#{format}") end as_exception ? raise(::Lazier::Exceptions::Debug.new(rv)) : rv end |
- (String) format_boolean(true_name = nil, false_name = nil)
Formats a boolean.
176 177 178 179 |
# File 'lib/lazier/object.rb', line 176 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.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/lazier/object.rb', line 158 def format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil) if is_number? then settings = ::Lazier.settings.format_number add_string ||= settings[:add_string] format, unit = (add_string ? ["%n %u", add_string] : ["%n", ""]) number_to_currency(self, {precision: [precision || settings[:precision], 0].max, separator: decimal_separator || settings[:decimal_separator], delimiter: k_separator || settings[:k_separator], format: format, unit: unit}) 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...
.
187 188 189 |
# File 'lib/lazier/object.rb', line 187 def indexize(length = 2, filler = "0", formatter = :rjust) self.ensure_string.send(formatter, length, filler) end |
- (Boolean) is_boolean?
Checks if the object is a valid boolean value.
50 51 52 |
# File 'lib/lazier/object.rb', line 50 def is_boolean? is_a?(::TrueClass) || !self || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER end |
- (Boolean) is_float? Also known as: is_number?
Checks if the object is a valid float.
42 43 44 |
# File 'lib/lazier/object.rb', line 42 def is_float? is_a?(::Numeric) || is_a?(::TrueClass) || !self || normalize_number =~ ::Lazier::Object::FLOAT_MATCHER end |
- (Boolean) is_integer?
Checks if the object is a valid integer.
35 36 37 |
# File 'lib/lazier/object.rb', line 35 def is_integer? is_a?(::Integer) || is_a?(::TrueClass) || !self || normalize_number =~ ::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.
28 29 30 |
# File 'lib/lazier/object.rb', line 28 def normalize_number is_boolean? ? to_i.to_s : ensure_string.strip.gsub(/[\.,](?=(.*[\.,]))/, "").gsub(",", ".") end |
- (Float) round_to_precision(precision = 2)
Returns the rounded float representaton of the object.
146 147 148 |
# File 'lib/lazier/object.rb', line 146 def round_to_precision(precision = 2) is_number? ? number_with_precision(to_float, precision: [precision, 0].max) : nil end |
- (Boolean) to_boolean
Converts the object to a boolean.
122 123 124 |
# File 'lib/lazier/object.rb', line 122 def to_boolean is_a?(TrueClass) || self == 1.0 || self == 1 || ensure_string =~ ::Lazier::Object::BOOLEAN_TRUE_MATCHER || false end |
- (Float) to_float(default_value = 0.0)
Converts the object to a float.
138 139 140 |
# File 'lib/lazier/object.rb', line 138 def to_float(default_value = 0.0) is_float? ? ::Kernel.Float(is_a?(::Numeric) ? self : normalize_number) : default_value end |
- (Fixnum) to_integer(default_value = 0)
Converts the object to a integer.
130 131 132 |
# File 'lib/lazier/object.rb', line 130 def to_integer(default_value = 0) to_float(default_value).to_i end |