Class: Object

Inherits:
BasicObject
Defined in:
lib/lite/ruby/object.rb

Instance Method Summary collapse

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/lite/ruby/object.rb', line 13

def array?
  is_a?(Array)
end

#blank?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/lite/ruby/object.rb', line 17

def blank?
  object = self
  object = object.strip if respond_to?(:strip)
  return object.empty? if respond_to?(:empty?)

  !object
end

#bool?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/lite/ruby/object.rb', line 25

def bool?
  true? || false?
end

#boolean?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/lite/ruby/object.rb', line 29

def boolean?
  val = to_s.downcase
  TRUE_VALUES.include?(val) || FALSE_VALUES.include?(val)
end

#date?Boolean

Returns:

  • (Boolean)


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

def date?
  is_a?(Date)
end

#deep_dupObject



38
39
40
# File 'lib/lite/ruby/object.rb', line 38

def deep_dup
  duplicable? ? dup : self
end

#duplicable?Boolean

Returns:

  • (Boolean)


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

def duplicable?
  true
end

#false?Boolean

rubocop:disable Style/YodaCondition

Returns:

  • (Boolean)


47
48
49
# File 'lib/lite/ruby/object.rb', line 47

def false?
  false == self
end

#falsey?Boolean

rubocop:enable Style/YodaCondition

Returns:

  • (Boolean)


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

def falsey?
  nil? || FALSE_VALUES.include?(to_s.downcase)
end

#float?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/lite/ruby/object.rb', line 56

def float?
  is_a?(Float)
end

#hash?Boolean

Returns:

  • (Boolean)


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

def hash?
  is_a?(Hash)
end

#integer?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/lite/ruby/object.rb', line 64

def integer?
  is_a?(Integer)
end

#is_any?(*objs) ⇒ Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


69
70
71
# File 'lib/lite/ruby/object.rb', line 69

def is_any?(*objs)
  objs.any? { |obj| is_a?(obj) }
end

#numeral?Boolean

rubocop:enable Naming/PredicateName

Returns:

  • (Boolean)


74
75
76
# File 'lib/lite/ruby/object.rb', line 74

def numeral?
  !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
end

#numeric?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/lite/ruby/object.rb', line 78

def numeric?
  is_a?(Numeric)
end

#open_struct?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/lite/ruby/object.rb', line 82

def open_struct?
  is_a?(OpenStruct)
end

#palindrome?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/lite/ruby/object.rb', line 86

def palindrome?
  to_s == to_s.reverse
end

#present?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/lite/ruby/object.rb', line 90

def present?
  !blank?
end

#range?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/lite/ruby/object.rb', line 94

def range?
  is_a?(Range)
end

#safe_call(*keys) ⇒ Object



98
99
100
# File 'lib/lite/ruby/object.rb', line 98

def safe_call(*keys)
  try_call(*keys) || self
end

#safe_send(*keys) ⇒ Object



102
103
104
# File 'lib/lite/ruby/object.rb', line 102

def safe_send(*keys)
  try_send(*keys) || self
end

#safe_try(*obj, &block) ⇒ Object



106
107
108
# File 'lib/lite/ruby/object.rb', line 106

def safe_try(*obj, &block)
  try(*obj, &block) || self
end

#salvage(placeholder = '---') ⇒ Object



110
111
112
# File 'lib/lite/ruby/object.rb', line 110

def salvage(placeholder = '---')
  blank? ? placeholder : self
end

#send_chain(*keys) ⇒ Object



114
115
116
# File 'lib/lite/ruby/object.rb', line 114

def send_chain(*keys)
  Array(keys).inject(self) { |obj, key| obj.send(*key) }
end

#send_chain_if(*keys) ⇒ Object



118
119
120
# File 'lib/lite/ruby/object.rb', line 118

def send_chain_if(*keys)
  Array(keys).inject(self) { |obj, key| obj.send_if(*key) }
end

#send_if(key, *args) ⇒ Object



122
123
124
125
126
# File 'lib/lite/ruby/object.rb', line 122

def send_if(key, *args)
  return self unless respond_to?(key)

  send(key, *args)
end

#set?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/lite/ruby/object.rb', line 128

def set?
  is_a?(Set)
end

#string?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/lite/ruby/object.rb', line 132

def string?
  is_a?(String)
end

#struct?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/lite/ruby/object.rb', line 136

def struct?
  is_a?(Struct)
end

#symbol?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/lite/ruby/object.rb', line 140

def symbol?
  is_a?(Symbol)
end

#time?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/lite/ruby/object.rb', line 144

def time?
  is_a?(Time)
end

#to_boolObject Also known as: to_b



148
149
150
151
152
153
# File 'lib/lite/ruby/object.rb', line 148

def to_bool
  return true if truthy?
  return false if falsey?

  nil
end

#true?Boolean

rubocop:disable Style/YodaCondition

Returns:

  • (Boolean)


158
159
160
# File 'lib/lite/ruby/object.rb', line 158

def true?
  true == self
end

#truthy?Boolean

rubocop:enable Style/YodaCondition

Returns:

  • (Boolean)


163
164
165
# File 'lib/lite/ruby/object.rb', line 163

def truthy?
  TRUE_VALUES.include?(to_s.downcase)
end

#try(*obj, &block) ⇒ Object



167
168
169
# File 'lib/lite/ruby/object.rb', line 167

def try(*obj, &block)
  try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
end

#try!(*obj, &block) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/lite/ruby/object.rb', line 171

def try!(*obj, &block)
  if obj.empty? && block_given?
    block.arity.zero? ? instance_eval(&block) : yield(self)
  else
    public_send(*obj, &block)
  end
end

#try_call(*keys) ⇒ Object



179
180
181
182
183
# File 'lib/lite/ruby/object.rb', line 179

def try_call(*keys)
  return unless respond_to?(:call)

  keys.blank? ? call : call(*keys)
end

#try_send(*keys) ⇒ Object



185
186
187
188
189
# File 'lib/lite/ruby/object.rb', line 185

def try_send(*keys)
  send(*keys)
rescue StandardError
  nil
end