Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/lite/ruby/object.rb,
lib/lite/ruby/safe/object.rb
Constant Summary collapse
- FALSE_VALUES =
%w[ 0 f false n no off ].freeze
- TRUE_VALUES =
%w[ 1 t true y yes on ].freeze
Instance Method Summary collapse
- #array? ⇒ Boolean
- #blank? ⇒ Boolean
- #bool? ⇒ Boolean
- #boolean? ⇒ Boolean
- #date? ⇒ Boolean
- #deep_dup ⇒ Object
- #duplicable? ⇒ Boolean
-
#false? ⇒ Boolean
rubocop:disable Style/YodaCondition.
-
#falsey? ⇒ Boolean
rubocop:enable Style/YodaCondition.
- #float? ⇒ Boolean
- #hash? ⇒ Boolean
- #integer? ⇒ Boolean
-
#is_any?(*objects) ⇒ Boolean
rubocop:disable Naming/PredicateName.
-
#numeral? ⇒ Boolean
rubocop:enable Naming/PredicateName.
- #numeric? ⇒ Boolean
- #open_struct? ⇒ Boolean
- #palindrome? ⇒ Boolean
- #presence ⇒ Object
- #present? ⇒ Boolean
- #range? ⇒ Boolean
- #safe_call ⇒ Object
- #safe_send ⇒ Object
- #safe_try ⇒ Object
- #salvage(placeholder = '---') ⇒ Object
- #salvage_try(method_name = nil, *args, placeholder: '---', &block) ⇒ Object
- #send_chain(*args) ⇒ Object
- #send_chain_if(*args) ⇒ Object
- #send_if(key, *args, **kwargs, &block) ⇒ Object
- #set? ⇒ Boolean
- #string? ⇒ Boolean
- #struct? ⇒ Boolean
- #symbol? ⇒ Boolean
- #time? ⇒ Boolean
- #to_bool ⇒ Object (also: #to_b)
-
#true? ⇒ Boolean
rubocop:disable Style/YodaCondition.
-
#truthy? ⇒ Boolean
rubocop:enable Style/YodaCondition.
- #try(method_name = nil, *args, &block) ⇒ Object
- #try!(method_name = nil, *args, &block) ⇒ Object
- #try_call ⇒ Object
- #try_send ⇒ Object
Instance Method Details
#blank? ⇒ Boolean
5 6 7 8 9 |
# File 'lib/lite/ruby/safe/object.rb', line 5 def blank? object = self object = object.strip if respond_to?(:strip) respond_to?(:empty?) ? object.empty? : !object end |
#bool? ⇒ Boolean
21 22 23 |
# File 'lib/lite/ruby/object.rb', line 21 def bool? true? || false? end |
#boolean? ⇒ Boolean
25 26 27 28 |
# File 'lib/lite/ruby/object.rb', line 25 def boolean? val = to_s.downcase TRUE_VALUES.include?(val) || FALSE_VALUES.include?(val) end |
#deep_dup ⇒ Object
11 12 13 |
# File 'lib/lite/ruby/safe/object.rb', line 11 def deep_dup duplicable? ? dup : self end |
#duplicable? ⇒ Boolean
15 16 17 |
# File 'lib/lite/ruby/safe/object.rb', line 15 def duplicable? true end |
#false? ⇒ Boolean
rubocop:disable Style/YodaCondition
35 36 37 |
# File 'lib/lite/ruby/object.rb', line 35 def false? false == self end |
#falsey? ⇒ Boolean
rubocop:enable Style/YodaCondition
40 41 42 |
# File 'lib/lite/ruby/object.rb', line 40 def falsey? nil? || FALSE_VALUES.include?(to_s.downcase) end |
#float? ⇒ Boolean
44 45 46 |
# File 'lib/lite/ruby/object.rb', line 44 def float? is_a?(Float) end |
#integer? ⇒ Boolean
52 53 54 |
# File 'lib/lite/ruby/object.rb', line 52 def integer? is_a?(Integer) end |
#is_any?(*objects) ⇒ Boolean
rubocop:disable Naming/PredicateName
57 58 59 |
# File 'lib/lite/ruby/object.rb', line 57 def is_any?(*objects) objects.any? { |obj| is_a?(obj) } end |
#numeral? ⇒ Boolean
rubocop:enable Naming/PredicateName
62 63 64 |
# File 'lib/lite/ruby/object.rb', line 62 def numeral? !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil? end |
#numeric? ⇒ Boolean
66 67 68 |
# File 'lib/lite/ruby/object.rb', line 66 def numeric? is_a?(Numeric) end |
#open_struct? ⇒ Boolean
70 71 72 |
# File 'lib/lite/ruby/object.rb', line 70 def open_struct? is_a?(OpenStruct) end |
#palindrome? ⇒ Boolean
74 75 76 |
# File 'lib/lite/ruby/object.rb', line 74 def palindrome? to_s == to_s.reverse end |
#presence ⇒ Object
23 24 25 |
# File 'lib/lite/ruby/safe/object.rb', line 23 def presence self if present? end |
#present? ⇒ Boolean
19 20 21 |
# File 'lib/lite/ruby/safe/object.rb', line 19 def present? !blank? end |
#safe_call ⇒ Object
82 83 84 |
# File 'lib/lite/ruby/object.rb', line 82 def safe_call(...) try_call(...) || self end |
#safe_send ⇒ Object
86 87 88 |
# File 'lib/lite/ruby/object.rb', line 86 def safe_send(...) try_send(...) || self end |
#safe_try ⇒ Object
90 91 92 |
# File 'lib/lite/ruby/object.rb', line 90 def safe_try(...) try(...) || self end |
#salvage(placeholder = '---') ⇒ Object
94 95 96 |
# File 'lib/lite/ruby/object.rb', line 94 def salvage(placeholder = '---') blank? ? placeholder : self end |
#salvage_try(method_name = nil, *args, placeholder: '---', &block) ⇒ Object
98 99 100 |
# File 'lib/lite/ruby/object.rb', line 98 def salvage_try(method_name = nil, *args, placeholder: '---', &block) try(method_name, *args, &block).salvage(placeholder) end |
#send_chain(*args) ⇒ Object
102 103 104 |
# File 'lib/lite/ruby/object.rb', line 102 def send_chain(*args) Array(args).inject(self) { |obj, argz| obj.send(*argz) } end |
#send_chain_if(*args) ⇒ Object
106 107 108 |
# File 'lib/lite/ruby/object.rb', line 106 def send_chain_if(*args) Array(args).inject(self) { |obj, argz| obj.send_if(*argz) } end |
#send_if(key, *args, **kwargs, &block) ⇒ Object
110 111 112 113 114 |
# File 'lib/lite/ruby/object.rb', line 110 def send_if(key, *args, **kwargs, &block) return self unless respond_to?(key) send(key, *args, **kwargs, &block) end |
#set? ⇒ Boolean
116 117 118 |
# File 'lib/lite/ruby/object.rb', line 116 def set? is_a?(Set) end |
#string? ⇒ Boolean
120 121 122 |
# File 'lib/lite/ruby/object.rb', line 120 def string? is_a?(String) end |
#struct? ⇒ Boolean
124 125 126 |
# File 'lib/lite/ruby/object.rb', line 124 def struct? is_a?(Struct) end |
#symbol? ⇒ Boolean
128 129 130 |
# File 'lib/lite/ruby/object.rb', line 128 def symbol? is_a?(Symbol) end |
#to_bool ⇒ Object Also known as: to_b
136 137 138 139 140 141 |
# File 'lib/lite/ruby/object.rb', line 136 def to_bool return true if truthy? return false if falsey? nil end |
#true? ⇒ Boolean
rubocop:disable Style/YodaCondition
144 145 146 |
# File 'lib/lite/ruby/object.rb', line 144 def true? true == self end |
#truthy? ⇒ Boolean
rubocop:enable Style/YodaCondition
149 150 151 |
# File 'lib/lite/ruby/object.rb', line 149 def truthy? TRUE_VALUES.include?(to_s.downcase) end |
#try(method_name = nil, *args, &block) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/lite/ruby/safe/object.rb', line 27 def try(method_name = nil, *args, &block) if method_name.nil? && block block.arity.zero? ? instance_eval(&block) : yield(self) elsif respond_to?(method_name) public_send(method_name, *args, &block) end end |
#try!(method_name = nil, *args, &block) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/lite/ruby/safe/object.rb', line 35 def try!(method_name = nil, *args, &block) if method_name.nil? && block block.arity.zero? ? instance_eval(&block) : yield(self) else public_send(method_name, *args, &block) end end |
#try_call ⇒ Object
153 154 155 156 157 |
# File 'lib/lite/ruby/object.rb', line 153 def try_call(...) return unless respond_to?(:call) call(...) end |
#try_send ⇒ Object
159 160 161 162 163 |
# File 'lib/lite/ruby/object.rb', line 159 def try_send(...) send(...) rescue StandardError nil end |