Module: Lazier::Object
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/object.rb
Overview
Extensions for all objects.
Constant Summary collapse
- 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
-
#ensure(default_value, verifier = :blank?) ⇒ String
Makes sure that the object is set to something meaningful.
-
#ensure_array(default_value = nil, uniq = false, compact = false, flatten = false, sanitizer = nil, &block) ⇒ Array
Makes sure that the object is an array.
-
#ensure_hash(access = nil, default_value = nil, sanitizer = nil) ⇒ Hash
Makes sure that the object is an hash.
-
#ensure_string(default_value = "", stringifier = :to_s) ⇒ String
Makes sure that the object is a string.
-
#for_debug(format = :yaml, as_exception = true) ⇒ String
Inspects an object.
-
#format_boolean(true_name = nil, false_name = nil) ⇒ String
Formats a boolean.
-
#format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil) ⇒ String
Formats a number.
-
#indexize(length = 2, filler = "0", formatter = :rjust) ⇒ String
Prepares an object to be printed in list summaries, like
[01/04] Opening this...
. -
#is_boolean? ⇒ Boolean
Checks if the object is a valid boolean value.
-
#is_float? ⇒ Boolean
(also: #is_number?)
Checks if the object is a valid float.
-
#is_integer? ⇒ Boolean
Checks if the object is a valid integer.
-
#normalize_number ⇒ String
Normalizes a number for conversion.
-
#round_to_precision(precision = 2) ⇒ Float
Returns the rounded float representaton of the object.
-
#safe_send(method, *args, &block) ⇒ Object|nil
Sends a method to 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
#ensure(default_value, verifier = :blank?) ⇒ String
Makes sure that the object is set to something meaningful.
68 69 70 71 |
# File 'lib/lazier/object.rb', line 68 def ensure(default_value, verifier = :blank?) valid = block_given? ? yield(self) : send(verifier) !valid ? self : default_value end |
#ensure_array(default_value = nil, uniq = false, compact = false, flatten = false, sanitizer = nil, &block) ⇒ Array
Makes sure that the object is an array. For non array objects, return a single element array containing the object.
91 92 93 94 95 |
# File 'lib/lazier/object.rb', line 91 def ensure_array(default_value = nil, uniq = false, compact = false, flatten = false, sanitizer = nil, &block) rv = is_a?(::Array) ? dup : (default_value || (self.is_a?(NilClass) ? [] : [self])) rv = manipulate_array(rv, uniq, compact, flatten).map(&(block || sanitizer)) if block_given? || sanitizer manipulate_array(rv, uniq, compact, flatten) end |
#ensure_hash(access = nil, default_value = nil, sanitizer = nil) ⇒ Hash
Makes sure that the object is an hash. For non hash objects, return an hash basing on the default_value
parameter.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/lazier/object.rb', line 103 def ensure_hash(access = nil, default_value = nil, sanitizer = nil) default_value = {} if default_value.nil? rv = if is_a?(::Hash) then self elsif default_value.is_a?(::Hash) then default_value else key = default_value.is_a?(::String) || default_value.is_a?(::Symbol) ? default_value : :key {key => self} end if block_given? || sanitizer then rv = rv.reduce({}) {|h, (k, v)| h[k] = block_given? ? yield(v) : v.send(sanitizer) h } end rv.respond_to?(:ensure_access) ? rv.ensure_access(access) :rv end |
#ensure_string(default_value = "", stringifier = :to_s) ⇒ String
Makes sure that the object is a string.
78 79 80 |
# File 'lib/lazier/object.rb', line 78 def ensure_string(default_value = "", stringifier = :to_s) !nil? ? (block_given? ? yield(self, default_value) : send(stringifier)) : default_value end |
#for_debug(format = :yaml, as_exception = true) ⇒ String
Inspects an object.
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/lazier/object.rb', line 205 def for_debug(format = :yaml, as_exception = true) rv = case format when :pretty_json ::JSON.pretty_generate(self) else send("to_#{format}") end as_exception ? raise(::Lazier::Exceptions::Debug.new(rv)) : rv end |
#format_boolean(true_name = nil, false_name = nil) ⇒ String
Formats a boolean.
185 186 187 188 |
# File 'lib/lazier/object.rb', line 185 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 |
#format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil) ⇒ String
Formats a number.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/lazier/object.rb', line 164 def format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil) if is_number? then settings = ::Lazier.settings.format_number add_string ||= settings[:add_string] rv = ("%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]) rv += " #{add_string}" if add_string rv else nil end end |
#indexize(length = 2, filler = "0", formatter = :rjust) ⇒ String
Prepares an object to be printed in list summaries, like [01/04] Opening this...
.
196 197 198 |
# File 'lib/lazier/object.rb', line 196 def indexize(length = 2, filler = "0", formatter = :rjust) self.ensure_string.send(formatter, length, filler) end |
#is_boolean? ⇒ Boolean
Checks if the object is a valid boolean value.
49 50 51 |
# File 'lib/lazier/object.rb', line 49 def is_boolean? is_a?(::TrueClass) || !self || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER end |
#is_float? ⇒ Boolean Also known as: is_number?
Checks if the object is a valid float.
41 42 43 |
# File 'lib/lazier/object.rb', line 41 def is_float? is_a?(::Numeric) || is_a?(::TrueClass) || !self || normalize_number =~ ::Lazier::Object::FLOAT_MATCHER end |
#is_integer? ⇒ Boolean
Checks if the object is a valid integer.
34 35 36 |
# File 'lib/lazier/object.rb', line 34 def is_integer? is_a?(::Integer) || is_a?(::TrueClass) || !self || normalize_number =~ ::Lazier::Object::INTEGER_MATCHER end |
#normalize_number ⇒ String
Normalizes a number for conversion. Basically this methods removes all separator and ensures that .
is used for decimal separator.
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 |
#round_to_precision(precision = 2) ⇒ Float
Returns the rounded float representaton of the object.
152 153 154 |
# File 'lib/lazier/object.rb', line 152 def round_to_precision(precision = 2) is_number? ? to_float.round([precision, 0].max) : nil end |
#safe_send(method, *args, &block) ⇒ Object|nil
Sends a method to the object. If the objects doesn't not respond to the method, it returns nil
instead of raising an exception.
59 60 61 |
# File 'lib/lazier/object.rb', line 59 def safe_send(method, *args, &block) respond_to?(method) ? send(method, *args, &block) : nil end |
#to_boolean ⇒ Boolean
Converts the object to a boolean.
128 129 130 |
# File 'lib/lazier/object.rb', line 128 def to_boolean is_a?(TrueClass) || self == 1.0 || self == 1 || !!(ensure_string =~ ::Lazier::Object::BOOLEAN_TRUE_MATCHER) || false end |
#to_float(default_value = 0.0) ⇒ Float
Converts the object to a float.
144 145 146 |
# File 'lib/lazier/object.rb', line 144 def to_float(default_value = 0.0) is_float? ? ::Kernel.Float(is_a?(::Numeric) ? self : normalize_number) : default_value end |
#to_integer(default_value = 0) ⇒ Fixnum
Converts the object to a integer.
136 137 138 |
# File 'lib/lazier/object.rb', line 136 def to_integer(default_value = 0) to_float(default_value).to_i end |