Module: Cowtech::Extensions::Object
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/cowtech-extensions/object.rb
Overview
Extensions for all objects.
Instance Method Summary collapse
-
#debug_dump(format = :yaml, must_raise = true) ⇒ String
Inspects an object.
-
#ensure_array ⇒ Array
Makes sure that the object is an array.
-
#ensure_string ⇒ String
Makes sure that the object is a string.
-
#format_boolean(true_name = nil, false_name = nil) ⇒ String
Formats a boolean.
-
#format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil) ⇒ String
Formats a number.
-
#is_boolean? ⇒ Boolean
Checks if the object is a valid boolean value.
-
#is_float? ⇒ Boolean
Checks if the object is a valid float.
-
#is_integer? ⇒ Boolean
Checks if the object is a valid integer.
-
#is_number? ⇒ Boolean
Checks if the object is a valid number.
-
#normalize_number ⇒ String
Normalizes a number for conversion.
-
#round_to_precision(prec = 2) ⇒ Float
Returns the rounded float representaton of the object.
-
#to_boolean ⇒ Boolean
Converts the object to a boolean.
-
#to_float(default_value = 0.0) ⇒ Float
Converts the object to a float.
-
#to_integer(default_value = 0) ⇒ Fixnum
Converts the object to a integer.
Instance Method Details
#debug_dump(format = :yaml, must_raise = true) ⇒ String
Inspects an object.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/cowtech-extensions/object.rb', line 148 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(::Cowtech::Extensions::Exceptions::Dump.new(rv)) : rv end |
#ensure_array ⇒ Array
Makes sure that the object is an array. For non array objects, return a single element array containing the object.
55 56 57 |
# File 'lib/cowtech-extensions/object.rb', line 55 def ensure_array self.is_a?(::Array) ? self : [self] end |
#ensure_string ⇒ String
Makes sure that the object is a string. For nil
, it returns "".
62 63 64 |
# File 'lib/cowtech-extensions/object.rb', line 62 def ensure_string self.present? ? self.to_s : "" end |
#format_boolean(true_name = nil, false_name = nil) ⇒ String
Formats a boolean.
134 135 136 137 138 139 140 141 |
# File 'lib/cowtech-extensions/object.rb', line 134 def format_boolean(true_name = nil, false_name = nil) names = { true => true_name || ::Cowtech::Extensions.settings.boolean_names[true], false => false_name || ::Cowtech::Extensions.settings.boolean_names[false] } names[self.to_boolean] end |
#format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil) ⇒ String
Formats a number.
119 120 121 122 123 124 125 126 |
# File 'lib/cowtech-extensions/object.rb', line 119 def format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil) prec = ::Cowtech::Extensions.settings.format_number[:prec] if prec.nil? decimal_separator = ::Cowtech::Extensions.settings.format_number[:decimal_separator] if decimal_separator.nil? add_string = ::Cowtech::Extensions.settings.format_number[:add_string] if add_string.nil? k_separator = ::Cowtech::Extensions.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 |
#is_boolean? ⇒ Boolean
Checks if the object is a valid boolean value.
48 49 50 |
# File 'lib/cowtech-extensions/object.rb', line 48 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 |
#is_float? ⇒ Boolean
Checks if the object is a valid float.
41 42 43 |
# File 'lib/cowtech-extensions/object.rb', line 41 def is_float? self.is_a?(::Float) || /^([+-]?)(\d+)([.,]\d+)?$/.match(self.normalize_number) end |
#is_integer? ⇒ Boolean
Checks if the object is a valid integer.
34 35 36 |
# File 'lib/cowtech-extensions/object.rb', line 34 def is_integer? self.is_a?(::Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number) end |
#is_number? ⇒ Boolean
Checks if the object is a valid number.
27 28 29 |
# File 'lib/cowtech-extensions/object.rb', line 27 def is_number? self.is_float? end |
#normalize_number ⇒ String
Normalizes a number for conversion. Basically this methods removes all separator and ensures that .
is used for decimal separator.
17 18 19 20 21 22 |
# File 'lib/cowtech-extensions/object.rb', line 17 def normalize_number rv = self.ensure_string.strip rv = rv.split(/[\.,]/) rv[-1] = "." + rv[-1] if rv.length > 1 rv.join("") end |
#round_to_precision(prec = 2) ⇒ Float
Returns the rounded float representaton of the object.
107 108 109 |
# File 'lib/cowtech-extensions/object.rb', line 107 def round_to_precision(prec = 2) (self.is_number? && prec >= 0) ? number_with_precision(self, :precision => prec) : nil end |
#to_boolean ⇒ Boolean
Converts the object to a boolean.
97 98 99 100 101 |
# File 'lib/cowtech-extensions/object.rb', line 97 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 |
#to_float(default_value = 0.0) ⇒ Float
Converts the object to a float.
70 71 72 73 74 75 76 77 78 |
# File 'lib/cowtech-extensions/object.rb', line 70 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 |
#to_integer(default_value = 0) ⇒ Fixnum
Converts the object to a integer.
84 85 86 87 88 89 90 91 92 |
# File 'lib/cowtech-extensions/object.rb', line 84 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 |