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

Instance Method Details

#debug_dump(format = :yaml, must_raise = true) ⇒ String

Inspects an object.

Parameters:

  • format (defaults to: :yaml)

    The format to use.

  • must_raise (Boolean) (defaults to: true)

    If raise a Dump exception.

Returns:

  • (String)

    The object inspected and formatted.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/lazier/object.rb', line 157

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

#ensure_arrayArray

Makes sure that the object is an array. For non array objects, return a single element array containing the object.

Returns:

  • (Array)

    If the object is an array, then the object itself, a single element array containing the object otherwise.



59
60
61
# File 'lib/lazier/object.rb', line 59

def ensure_array
  self.is_a?(::Array) ? self : [self]
end

#ensure_stringString

Makes sure that the object is a string. For nil, it returns "".

Returns:

  • (String)

    The string representation of the object.



66
67
68
69
70
71
72
# File 'lib/lazier/object.rb', line 66

def ensure_string
  if self.is_a?(::String) then
    self
  else
    self.present? ? self.to_s : ""
  end
end

#format_boolean(true_name = nil, false_name = nil) ⇒ String

Formats a boolean.

Parameters:

  • true_name (String) (defaults to: nil)

    The string representation of true. Defaults to Yes.

  • false_name (String) (defaults to: nil)

    The string representation of false. Defaults to No.

Returns:

  • (String)

    The string representation of the object.

See Also:



143
144
145
146
147
148
149
150
# File 'lib/lazier/object.rb', line 143

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

#format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil) ⇒ String

Formats a number.

Parameters:

  • prec (Fixnum) (defaults to: nil)

    The precision to show.

  • decimal_separator (String) (defaults to: nil)

    The string to use as decimal separator.

  • add_string (String) (defaults to: nil)

    The string to append to the number.

  • k_separator (String) (defaults to: nil)

    The string to use as thousands separator.

Returns:

  • (String)

    The string representation of the object.

See Also:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/lazier/object.rb', line 121

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

#is_boolean?Boolean

Checks if the object is a valid boolean value.

Returns:

  • (Boolean)

    true is a valid boolean value, false otherwise.



52
53
54
# File 'lib/lazier/object.rb', line 52

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.

Returns:

  • (Boolean)

    true is a valid float, false otherwise.



45
46
47
# File 'lib/lazier/object.rb', line 45

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.

Returns:

  • (Boolean)

    true is a valid integer, false otherwise.



38
39
40
# File 'lib/lazier/object.rb', line 38

def is_integer?
  self.is_a?(::Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number)
end

#is_number?Boolean

Checks if the object is a valid number.

Returns:

  • (Boolean)

    true is a valid number, false otherwise.



31
32
33
# File 'lib/lazier/object.rb', line 31

def is_number?
  self.is_float?
end

#normalize_numberString

Normalizes a number for conversion. Basically this methods removes all separator and ensures that . is used for decimal separator.

Returns:

  • (String)

    The normalized number.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lazier/object.rb', line 16

def normalize_number
  if self.is_a?(TrueClass) then
    "1"
  elsif !self then
    "0"
  else
    rv = self.ensure_string.strip.split(/[\.,]/)
    rv[-1] = "." + rv[-1] if rv.length > 1
    rv.join("")
  end
end

#round_to_precision(prec = 2) ⇒ Float

Returns the rounded float representaton of the object.

Parameters:

  • prec (Fixnum) (defaults to: 2)

    The precision to keep.

Returns:

  • (Float)

    The rounded float representaton of the object.



109
110
111
# File 'lib/lazier/object.rb', line 109

def round_to_precision(prec = 2)
  (self.is_number? && prec >= 0) ? number_with_precision(self, precision: prec) : nil
end

#to_booleanBoolean

Converts the object to a boolean.

Returns:

  • (Boolean)

    The boolean representation of the object.



99
100
101
102
103
# File 'lib/lazier/object.rb', line 99

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.

Parameters:

  • default_value (Float) (defaults to: 0.0)

    The value to return if the conversion is not possible.

Returns:

  • (Float)

    The float representation of the object.



78
79
80
81
82
83
84
85
86
# File 'lib/lazier/object.rb', line 78

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.

Parameters:

  • default_value (Fixnum) (defaults to: 0)

    The value to return if the conversion is not possible.

Returns:

  • (Fixnum)

    The integer representation of the object.



92
93
94
# File 'lib/lazier/object.rb', line 92

def to_integer(default_value = 0)
  self.is_a?(::Integer) ? self : self.to_float(default_value).to_i
end