Module: Lazier::Object
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/lazier/object.rb
Overview
Extensions for all objects.
Instance Method Summary (collapse)
-
- (String) debug_dump(format = :yaml, must_raise = true)
Inspects an object.
-
- (Array) ensure_array
Makes sure that the object is an array.
-
- (String) ensure_string
Makes sure that the object is a string.
-
- (String) format_boolean(true_name = nil, false_name = nil)
Formats a boolean.
-
- (String) format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
Formats a number.
-
- (Boolean) is_boolean?
Checks if the object is a valid boolean value.
-
- (Boolean) is_float?
Checks if the object is a valid float.
-
- (Boolean) is_integer?
Checks if the object is a valid integer.
-
- (Boolean) is_number?
Checks if the object is a valid number.
-
- (String) normalize_number
Normalizes a number for conversion.
-
- (Float) round_to_precision(prec = 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) debug_dump(format = :yaml, must_raise = true)
Inspects an object.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/lazier/object.rb', line 162 def debug_dump(format = :yaml, must_raise = true) rv = "" begin if format == :pretty_json then rv = ::JSON.pretty_generate(self) else rv = self.send("to_#{format}") end rescue rv = self.inspect end must_raise ? raise(::Lazier::Exceptions::Dump.new(rv)) : rv end |
- (Array) ensure_array
Makes sure that the object is an array. For non array objects, return a single element array containing the object.
64 65 66 |
# File 'lib/lazier/object.rb', line 64 def ensure_array self.is_a?(::Array) ? self : [self] end |
- (String) ensure_string
Makes sure that the object is a string. For nil
, it returns "".
71 72 73 74 75 76 77 |
# File 'lib/lazier/object.rb', line 71 def ensure_string if self.is_a?(::String) then self else self.present? ? self.to_s : "" end end |
- (String) format_boolean(true_name = nil, false_name = nil)
Formats a boolean.
148 149 150 151 152 153 154 155 |
# File 'lib/lazier/object.rb', line 148 def format_boolean(true_name = nil, false_name = nil) names = { true => true_name || ::Lazier.settings.boolean_names[true], false => false_name || ::Lazier.settings.boolean_names[false] } names[self.to_boolean] end |
- (String) format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
Formats a number.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/lazier/object.rb', line 126 def format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil) prec ||= ::Lazier.settings.format_number[:prec] decimal_separator ||= ::Lazier.settings.format_number[:decimal_separator] add_string ||= ::Lazier.settings.format_number[:add_string] k_separator ||= ::Lazier.settings.format_number[:k_separator] format = "%n" unit = "" if add_string.present? then format = "%n %u" unit = add_string end (self.is_number? && prec >= 0) ? number_to_currency(self, {precision: prec, separator: decimal_separator, delimiter: k_separator, format: format, unit: unit}) : nil end |
- (Boolean) is_boolean?
Checks if the object is a valid boolean value.
57 58 59 |
# File 'lib/lazier/object.rb', line 57 def is_boolean? self.is_a?(::TrueClass) || self.is_a?(::FalseClass) || self.is_a?(::NilClass) || (self.ensure_string.strip =~ /^(1|0|true|false|yes|no|t|f|y|n)$/i) end |
- (Boolean) is_float?
Checks if the object is a valid float.
50 51 52 |
# File 'lib/lazier/object.rb', line 50 def is_float? self.is_a?(::Float) || /^([+-]?)(\d+)([.,]\d+)?$/.match(self.normalize_number) end |
- (Boolean) is_integer?
Checks if the object is a valid integer.
43 44 45 |
# File 'lib/lazier/object.rb', line 43 def is_integer? self.is_a?(::Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number) end |
- (Boolean) is_number?
Checks if the object is a valid number.
36 37 38 |
# File 'lib/lazier/object.rb', line 36 def is_number? self.is_float? end |
- (String) normalize_number
Normalizes a number for conversion. Basically this methods removes all separator and ensures that .
is used for decimal separator.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lazier/object.rb', line 16 def normalize_number rv = "" if self == true then rv = "1" elsif !self then rv = "0" else rv = self.ensure_string.strip rv = rv.split(/[\.,]/) rv[-1] = "." + rv[-1] if rv.length > 1 rv = rv.join("") end rv end |
- (Float) round_to_precision(prec = 2)
Returns the rounded float representaton of the object.
114 115 116 |
# File 'lib/lazier/object.rb', line 114 def round_to_precision(prec = 2) (self.is_number? && prec >= 0) ? number_with_precision(self, precision: prec) : nil end |
- (Boolean) to_boolean
Converts the object to a boolean.
104 105 106 107 108 |
# File 'lib/lazier/object.rb', line 104 def to_boolean rv = self rv = rv.to_i if rv.is_a?(::Float) (rv.is_a?(TrueClass) || /^(1|on|true|yes|t|y)$/i.match(rv.ensure_string.strip)) ? true : false end |
- (Float) to_float(default_value = 0.0)
Converts the object to a float.
83 84 85 86 87 88 89 90 91 |
# File 'lib/lazier/object.rb', line 83 def to_float(default_value = 0.0) if self.is_a?(::Float) self elsif self.is_a?(::Integer) self.to_f else self.is_float? ? ::Kernel.Float(self.normalize_number) : default_value end end |
- (Fixnum) to_integer(default_value = 0)
Converts the object to a integer.
97 98 99 |
# File 'lib/lazier/object.rb', line 97 def to_integer(default_value = 0) self.is_a?(::Integer) ? self : self.to_float(default_value).to_i end |