Module: Lazier::Object

Extended by:
ActiveSupport::Concern
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)

Instance Method Details

- (Boolean) boolean? Also known as: is_boolean?

Checks if the object is a valid boolean value.

Returns:

  • (Boolean)

    true is a valid boolean value, false otherwise.



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

def boolean?
  is_a?(::TrueClass) || !self || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER
end

- (String) ensure(default_value, verifier = :blank?)

Makes sure that the object is set to something meaningful.

Parameters:

  • default_value (String)

    The default value to return if the verifier or the block returns true.

  • verifier (Symbol) (defaults to: :blank?)

    The method used to verify if the object is NOT meaningful. Ignored if a block is passed.

Returns:

  • (String)

    The current object or the default_value.



80
81
82
83
# File 'lib/lazier/object.rb', line 80

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 = false, sanitizer = nil, &block)

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

Parameters:

  • default_value (Array|NilClass) (defaults to: nil)

    The default array to use. If not specified, an array containing the object is returned.

  • uniq (Boolean) (defaults to: false)

    If to remove duplicates from the array before sanitizing.

  • compact (Boolean) (defaults to: false)

    If to compact the array before sanitizing.

  • flatten (Boolean) (defaults to: false)

    If to flatten the array before sanitizing.

  • sanitizer (Symbol|nil) (defaults to: nil)

    If not nil, the method to use to sanitize entries of the array. Ignored if a block is present.

  • block (Proc)

    A block to sanitize entries. It must accept the value as unique argument.

Returns:

  • (Array)

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lazier/object.rb', line 107

def ensure_array(default_value = nil, uniq = false, compact = false, flatten = false, sanitizer = nil, &block)
  rv =
    if is_a?(::Array)
      dup
    else
      default_value || (self.is_a?(NilClass) ? [] : [self])
    end

  rv = manipulate_array(rv, uniq, compact, flatten).map(&(block || sanitizer)) if block_given? || sanitizer
  manipulate_array(rv, uniq, compact, flatten)
end

- (Hash) ensure_hash(access = nil, default_value = nil, sanitizer = nil, &block)

Makes sure that the object is an hash. For non hash objects, return an hash basing on the default_value parameter.

Parameters:

  • access (Symbol|NilClass) (defaults to: nil)

    The requested access for the keys of the returned object. Can be :strings, :symbols or indifferent. If nil the keys are not modified.

  • default_value (Hash|String|Symbol|NilClass) (defaults to: nil)

    The default value to use. If it is an Hash, it is returned as value otherwise it is used to build as a key to build an hash with the current object as only value (everything but strings and symbols are mapped to key). Passing nil is equal to pass an empty Hash.

  • sanitizer (Symbol|nil) (defaults to: nil)

    If not nil, the method to use to sanitize values of the hash. Ignored if block is present.

  • block (Proc)

    A block to sanitize entries. It must accept the value as unique argument.

Returns:

  • (Hash)

    If the object is an hash, then the object itself, a hash with the object as single value otherwise.



129
130
131
132
133
134
135
136
# File 'lib/lazier/object.rb', line 129

def ensure_hash(access = nil, default_value = nil, sanitizer = nil, &block)
  default_value = {} if default_value.is_a?(NilClass)

  rv = convert_to_hash(default_value)
  rv = sanitize_hash(rv, sanitizer, block) if block || sanitizer

  rv.respond_to?(:ensure_access) ? rv.ensure_access(access) : rv
end

- (String) ensure_string(default_value = "", stringifier = :to_s)

Makes sure that the object is a string.

Parameters:

  • default_value (String) (defaults to: "")

    The default value to return if the object is nil. It is also passed to the block stringifier.

  • stringifier (Symbol) (defaults to: :to_s)

    The method used to convert the object to a string. Ignored if a block is passed.

Returns:

  • (String)

    The string representation of the object.



90
91
92
93
94
95
96
# File 'lib/lazier/object.rb', line 90

def ensure_string(default_value = "", stringifier = :to_s)
  if is_a?(NilClass)
    default_value
  else
    block_given? ? yield(self, default_value) : send(stringifier)
  end
end

- (Boolean) float? Also known as: number?, is_float?, is_number?

Checks if the object is a valid float.

Returns:

  • (Boolean)

    true is a valid float, false otherwise.



42
43
44
# File 'lib/lazier/object.rb', line 42

def float?
  numeric?(Numeric, ::Lazier::Object::FLOAT_MATCHER)
end

- (String) for_debug(format = :yaml, as_exception = true)

Inspects an object.

Parameters:

  • format (defaults to: :yaml)

    The format to use.

  • as_exception (Boolean) (defaults to: true)

    If raise an exception.

Returns:

  • (String)

    The object inspected and formatted.



221
222
223
224
225
226
227
228
229
# File 'lib/lazier/object.rb', line 221

def for_debug(format = :yaml, as_exception = true)
  rv =
    case format
    when :pretty_json then ::JSON.pretty_generate(self)
    else send("to_#{format}")
    end

  as_exception ? raise(::Lazier::Exceptions::Debug, rv) : rv
end

- (String) format_boolean(true_name = nil, false_name = nil)

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:



201
202
203
204
# File 'lib/lazier/object.rb', line 201

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.

Parameters:

  • precision (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:



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/lazier/object.rb', line 181

def format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
  if is_number?
    settings = ::Lazier.settings.format_number
    add_string ||= settings[:add_string]

    rv = format("%0.#{[precision || settings[:precision], 0].max}f", to_float).split(".")
    rv[0].gsub!(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{k_separator || settings[:k_separator]}")
    rv = rv.join(decimal_separator || settings[:decimal_separator])
    add_string ? rv + " #{add_string}" : rv
  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....

Parameters:

  • length (Fixnum) (defaults to: 2)

    The minimum length of the label.

  • filler (String) (defaults to: "0")

    The minimum length of the label.

  • formatter (Symbol) (defaults to: :rjust)

    The method to use to format the label. Must accept the length and the `filler arguments.

Returns:

  • (String)

    The object inspected and formatted.



212
213
214
# File 'lib/lazier/object.rb', line 212

def indexize(length = 2, filler = "0", formatter = :rjust)
  ensure_string.send(formatter, length, filler)
end

- (Boolean) integer? Also known as: is_integer?

Checks if the object is a valid integer.

Returns:

  • (Boolean)

    true is a valid integer, false otherwise.



34
35
36
# File 'lib/lazier/object.rb', line 34

def integer?
  numeric?(Integer, ::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.

Returns:

  • (String)

    The normalized number.



27
28
29
# File 'lib/lazier/object.rb', line 27

def normalize_number
  is_boolean? ? to_i.to_s : ensure_string.strip.gsub(/[\.,](?=(.*[\.,]))/, "").gsub(",", ".")
end

- (Boolean) numeric?(klass = Integer, matcher = ::Lazier::Object::INTEGER_MATCHER) Also known as: is_numeric?

Checks if the object is of a numeric class of matches a numeric string expression.

Returns:

  • (Boolean)

    true is a valid numeric object, false otherwise.



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

def numeric?(klass = Integer, matcher = ::Lazier::Object::INTEGER_MATCHER)
  is_a?(klass) || is_a?(::TrueClass) || !self || normalize_number =~ matcher
end

- (Float) round_to_precision(precision = 2)

Returns the rounded float representaton of the object.

Parameters:

  • precision (Fixnum) (defaults to: 2)

    The precision to keep.

Returns:

  • (Float)

    The rounded float representaton of the object.



169
170
171
# File 'lib/lazier/object.rb', line 169

def round_to_precision(precision = 2)
  is_number? ? to_float.round([precision, 0].max) : nil
end

- (Object|nil) safe_send(method, *args, &block)

Sends a method to the object. If the objects doesn’t not respond to the method, it returns nil instead of raising an exception.

Parameters:

  • method (Symbol)

    The method to send.

  • args (Array)

    The arguments to send.

  • block (Proc)

    The block to pass to the method.

Returns:

  • (Object|nil)

    The return value of the method or nil, if the object does not respond to the method.



71
72
73
# File 'lib/lazier/object.rb', line 71

def safe_send(method, *args, &block)
  respond_to?(method) ? send(method, *args, &block) : nil
end

- (Boolean) to_boolean

Converts the object to a boolean.

Returns:

  • (Boolean)

    The boolean representation of the object.



141
142
143
# File 'lib/lazier/object.rb', line 141

def to_boolean
  is_a?(TrueClass) || to_integer == 1 || ::Lazier::Object::BOOLEAN_TRUE_MATCHER.match(ensure_string).is_a?(MatchData)
end

- (Float) to_float(default_value = 0.0)

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.



157
158
159
160
161
162
163
# File 'lib/lazier/object.rb', line 157

def to_float(default_value = 0.0)
  if is_float?
    ::Kernel.Float(is_a?(::Numeric) ? self : normalize_number)
  else
    default_value
  end
end

- (Fixnum) to_integer(default_value = 0)

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.



149
150
151
# File 'lib/lazier/object.rb', line 149

def to_integer(default_value = 0)
  to_float(default_value).to_i
end