Module: TwelvedataRuby::Utils
- Defined in:
- lib/twelvedata_ruby/utils.rb
Class Method Summary collapse
-
.blank?(value) ⇒ Boolean
Checks if a value is blank (nil, empty, or whitespace-only).
-
.camelize(str) ⇒ String
Converts snake_case to CamelCase.
-
.demodulize(obj) ⇒ String
Removes module namespace from class name.
-
.empty_to_nil(obj) ⇒ Object?
Converts empty values to nil.
-
.execute_if_true(condition) { ... } ⇒ Object?
Executes block only if condition is exactly true.
-
.execute_if_truthy(condition, default_return = nil) { ... } ⇒ Object
Executes block if condition is truthy.
-
.present?(value) ⇒ Boolean
Validates that a value is not blank.
-
.to_array(objects) ⇒ Array
Ensures return value is an array.
-
.to_integer(obj, default_value = nil) ⇒ Integer?
Converts string to integer with default fallback.
Class Method Details
.blank?(value) ⇒ Boolean
Checks if a value is blank (nil, empty, or whitespace-only)
104 105 106 107 108 109 110 |
# File 'lib/twelvedata_ruby/utils.rb', line 104 def blank?(value) return true if value.nil? return true if value.respond_to?(:empty?) && value.empty? return true if value.is_a?(String) && value.strip.empty? false end |
.camelize(str) ⇒ String
Converts snake_case to CamelCase
40 41 42 |
# File 'lib/twelvedata_ruby/utils.rb', line 40 def camelize(str) str.to_s.split("_").map(&:capitalize).join end |
.demodulize(obj) ⇒ String
Removes module namespace from class name
14 15 16 |
# File 'lib/twelvedata_ruby/utils.rb', line 14 def demodulize(obj) obj.to_s.gsub(/^.+::/, "") end |
.empty_to_nil(obj) ⇒ Object?
Converts empty values to nil
52 53 54 55 56 57 |
# File 'lib/twelvedata_ruby/utils.rb', line 52 def empty_to_nil(obj) return nil if obj.nil? return nil if obj.respond_to?(:empty?) && obj.empty? obj end |
.execute_if_true(condition) { ... } ⇒ Object?
Executes block only if condition is exactly true
88 89 90 |
# File 'lib/twelvedata_ruby/utils.rb', line 88 def execute_if_true(condition, &block) execute_if_truthy(condition == true, &block) end |
.execute_if_truthy(condition, default_return = nil) { ... } ⇒ Object
Executes block if condition is truthy
77 78 79 80 81 |
# File 'lib/twelvedata_ruby/utils.rb', line 77 def execute_if_truthy(condition, default_return = nil) return default_return unless condition && block_given? yield end |
.present?(value) ⇒ Boolean
Validates that a value is not blank
96 97 98 |
# File 'lib/twelvedata_ruby/utils.rb', line 96 def present?(value) !blank?(value) end |
.to_array(objects) ⇒ Array
Ensures return value is an array
67 68 69 |
# File 'lib/twelvedata_ruby/utils.rb', line 67 def to_array(objects) objects.is_a?(Array) ? objects : [objects] end |
.to_integer(obj, default_value = nil) ⇒ Integer?
Converts string to integer with default fallback
27 28 29 30 31 |
# File 'lib/twelvedata_ruby/utils.rb', line 27 def to_integer(obj, default_value = nil) obj.is_a?(Integer) ? obj : Integer(obj.to_s) rescue ArgumentError default_value end |