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.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/lazier/object.rb', line 147 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.
54 55 56 |
# File 'lib/lazier/object.rb', line 54 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 "".
61 62 63 |
# File 'lib/lazier/object.rb', line 61 def ensure_string self.present? ? self.to_s : "" end |
- (String) format_boolean(true_name = nil, false_name = nil)
Formats a boolean.
133 134 135 136 137 138 139 140 |
# File 'lib/lazier/object.rb', line 133 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.
118 119 120 121 122 123 124 125 |
# File 'lib/lazier/object.rb', line 118 def format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil) prec = ::Lazier.settings.format_number[:prec] if prec.nil? decimal_separator = ::Lazier.settings.format_number[:decimal_separator] if decimal_separator.nil? add_string = ::Lazier.settings.format_number[:add_string] if add_string.nil? k_separator = ::Lazier.settings.format_number[:k_separator] if k_separator.nil? (self.is_number? && prec >= 0) ? number_to_currency(self, {:precision => prec, :separator => decimal_separator, :delimiter => k_separator, :format => add_string.blank? ? "%n" : "%n %u", :unit => add_string.blank? ? "" : add_string.strip}) : nil end |
- (Boolean) is_boolean?
Checks if the object is a valid boolean value.
47 48 49 |
# File 'lib/lazier/object.rb', line 47 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.
40 41 42 |
# File 'lib/lazier/object.rb', line 40 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.
33 34 35 |
# File 'lib/lazier/object.rb', line 33 def is_integer? self.is_a?(::Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number) end |
- (Boolean) is_number?
Checks if the object is a valid number.
26 27 28 |
# File 'lib/lazier/object.rb', line 26 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 |
# File 'lib/lazier/object.rb', line 16 def normalize_number rv = self.ensure_string.strip rv = rv.split(/[\.,]/) rv[-1] = "." + rv[-1] if rv.length > 1 rv.join("") end |
- (Float) round_to_precision(prec = 2)
Returns the rounded float representaton of the object.
106 107 108 |
# File 'lib/lazier/object.rb', line 106 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.
96 97 98 99 100 |
# File 'lib/lazier/object.rb', line 96 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.
69 70 71 72 73 74 75 76 77 |
# File 'lib/lazier/object.rb', line 69 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.
83 84 85 86 87 88 89 90 91 |
# File 'lib/lazier/object.rb', line 83 def to_integer(default_value = 0) if self.is_a?(::Integer) self elsif self.is_a?(::Float) self.to_i else self.is_integer? ? ::Kernel.Integer(self.normalize_number) : default_value end end |