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
-
#boolean? ⇒ Boolean
Checks if the object is a valid boolean value.
-
#ensure(default, verifier = :blank?) ⇒ String
Makes sure that the object is set to something meaningful.
-
#ensure_array(default: nil, no_duplicates: false, compact: false, flatten: false, sanitizer: nil, &block) ⇒ Array
Makes sure that the object is an array.
-
#ensure_hash(accesses: nil, default: {}, sanitizer: nil, &block) ⇒ Hash
Makes sure that the object is an hash.
-
#ensure_string(default = "", conversion_method = :to_s) ⇒ String
Makes sure that the object is a string.
-
#float? ⇒ Boolean
Checks if the object is a valid float.
-
#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|NilClass
Formats a number.
-
#indexize(length: 2, filler: "0", formatter: :rjust) ⇒ String
Prepares an object to be printed in list summaries, like.
-
#integer? ⇒ Boolean
Checks if the object is a valid integer.
-
#normalize_number ⇒ String
Normalizes a number for conversion.
-
#number?(klass = Integer, matcher = ::Lazier::Object::FLOAT_MATCHER) ⇒ Boolean
Checks if the object is of a numeric class of matches a numeric string expression.
-
#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_debug(format: :pretty_json, as_exception: true) ⇒ String
Inspects an object.
-
#to_float(default = 0.0) ⇒ Float
Converts the object to a float.
-
#to_integer(default = 0) ⇒ Fixnum
Converts the object to a integer.
-
#to_pretty_json ⇒ String
Converts an object to a pretty formatted JSON string.
Instance Method Details
#boolean? ⇒ Boolean
Checks if the object is a valid boolean value.
54 55 56 |
# File 'lib/lazier/object.rb', line 54 def boolean? nil? || is_a?(::TrueClass) || is_a?(::FalseClass) || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER end |
#ensure(default, verifier = :blank?) ⇒ String
Makes sure that the object is set to something meaningful.
75 76 77 78 |
# File 'lib/lazier/object.rb', line 75 def ensure(default, verifier = :blank?) valid = block_given? ? yield(self) : send(verifier) !valid ? self : default end |
#ensure_array(default: nil, no_duplicates: 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.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/lazier/object.rb', line 102 def ensure_array(default: nil, no_duplicates: false, compact: false, flatten: false, sanitizer: nil, &block) rv = if is_a?(::Array) self else default || [self].compact end rv = manipulate_array(rv, no_duplicates, compact, flatten).map(&(block || sanitizer)) if block_given? || sanitizer manipulate_array(rv, no_duplicates, compact, flatten) end |
#ensure_hash(accesses: nil, default: {}, sanitizer: nil, &block) ⇒ Hash
Makes sure that the object is an hash. For non hash objects, return an hash basing on the default_value
parameter.
125 126 127 128 129 130 |
# File 'lib/lazier/object.rb', line 125 def ensure_hash(accesses: nil, default: {}, sanitizer: nil, &block) rv = convert_to_hash(default) rv = sanitize_hash(rv, sanitizer, block) if block || sanitizer rv.respond_to?(:ensure_access) ? rv.ensure_access(accesses.ensure_array) : rv end |
#ensure_string(default = "", conversion_method = :to_s) ⇒ String
Makes sure that the object is a string.
85 86 87 88 89 90 91 |
# File 'lib/lazier/object.rb', line 85 def ensure_string(default = "", conversion_method = :to_s) if is_a?(NilClass) default else block_given? ? yield(self, default) : send(conversion_method) end end |
#float? ⇒ Boolean
Checks if the object is a valid float.
47 48 49 |
# File 'lib/lazier/object.rb', line 47 def float? number?(Numeric, ::Lazier::Object::FLOAT_MATCHER) end |
#format_boolean(true_name: nil, false_name: nil) ⇒ String
Formats a boolean.
209 210 211 212 |
# File 'lib/lazier/object.rb', line 209 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|NilClass
Formats a number.
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/lazier/object.rb', line 192 def format_number(precision: nil, decimal_separator: nil, add_string: nil, k_separator: nil) if number? precision, decimal_separator, add_string, k_separator = format_number_sanitize(precision, decimal_separator, add_string, k_separator) rv = format("%0.#{precision}f", to_float).split(".") rv[0].gsub!(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{k_separator}") rv = rv.join(decimal_separator) add_string ? rv + " #{add_string}" : rv end end |
#indexize(length: 2, filler: "0", formatter: :rjust) ⇒ String
Prepares an object to be printed in list summaries, like
[01/04] Opening this...
222 223 224 |
# File 'lib/lazier/object.rb', line 222 def indexize(length: 2, filler: "0", formatter: :rjust) ensure_string.send(formatter, length, filler) end |
#integer? ⇒ Boolean
Checks if the object is a valid integer.
40 41 42 |
# File 'lib/lazier/object.rb', line 40 def integer? number?(Integer, ::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.
26 27 28 |
# File 'lib/lazier/object.rb', line 26 def normalize_number boolean? ? to_i.to_s : ensure_string.strip.gsub(/[\.,](?=(.*[\.,]))/, "").gsub(",", ".") end |
#number?(klass = Integer, matcher = ::Lazier::Object::FLOAT_MATCHER) ⇒ Boolean
Checks if the object is of a numeric class of matches a numeric string expression.
33 34 35 |
# File 'lib/lazier/object.rb', line 33 def number?(klass = Integer, matcher = ::Lazier::Object::FLOAT_MATCHER) nil? || is_a?(klass) || boolean? || normalize_number =~ matcher end |
#round_to_precision(precision = 2) ⇒ Float
Returns the rounded float representaton of the object.
180 181 182 |
# File 'lib/lazier/object.rb', line 180 def round_to_precision(precision = 2) 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.
64 65 66 67 68 |
# File 'lib/lazier/object.rb', line 64 def safe_send(method, *args, &block) send(method, *args, &block) rescue NoMethodError nil end |
#to_boolean ⇒ Boolean
Converts the object to a boolean.
135 136 137 |
# File 'lib/lazier/object.rb', line 135 def to_boolean is_a?(TrueClass) || to_integer == 1 || ::Lazier::Object::BOOLEAN_TRUE_MATCHER.match(ensure_string).is_a?(MatchData) end |
#to_debug(format: :pretty_json, as_exception: true) ⇒ String
Inspects an object.
171 172 173 174 |
# File 'lib/lazier/object.rb', line 171 def to_debug(format: :pretty_json, as_exception: true) rv = send("to_#{format}") as_exception ? raise(::Lazier::Exceptions::Debug, rv) : rv end |
#to_float(default = 0.0) ⇒ Float
Converts the object to a float.
151 152 153 154 155 156 157 |
# File 'lib/lazier/object.rb', line 151 def to_float(default = 0.0) if float? ::Kernel.Float(is_a?(::Numeric) ? self : normalize_number) else default end end |
#to_integer(default = 0) ⇒ Fixnum
Converts the object to a integer.
143 144 145 |
# File 'lib/lazier/object.rb', line 143 def to_integer(default = 0) to_float(default).to_i end |
#to_pretty_json ⇒ String
Converts an object to a pretty formatted JSON string.
162 163 164 |
# File 'lib/lazier/object.rb', line 162 def to_pretty_json Lazier.platform != :java ? Oj.dump(self, mode: :compat, indent: 2) : ::JSON.pretty_generate(self) end |