Module: ActiveObject::Object

Defined in:
lib/active_object/object.rb

Instance Method Summary collapse

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/active_object/object.rb', line 12

def array?
  is_a?(Array)
end

#blank?Boolean

Returns:

  • (Boolean)

16
17
18
19
20
# File 'lib/active_object/object.rb', line 16

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

#bool?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'lib/active_object/object.rb', line 22

def bool?
  true? || false?
end

#boolean?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/active_object/object.rb', line 26

def boolean?
  TRUE_VALUES.include?(self) || FALSE_VALUES.include?(self)
end

#date?Boolean

Returns:

  • (Boolean)

30
31
32
# File 'lib/active_object/object.rb', line 30

def date?
  is_a?(Date)
end

#false?Boolean

rubocop:disable Style/YodaCondition

Returns:

  • (Boolean)

35
36
37
# File 'lib/active_object/object.rb', line 35

def false?
  false == self
end

#falsey?Boolean

rubocop:enable Style/YodaCondition

Returns:

  • (Boolean)

40
41
42
# File 'lib/active_object/object.rb', line 40

def falsey?
  nil? || FALSE_VALUES.include?(self)
end

#float?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/active_object/object.rb', line 44

def float?
  is_a?(Float)
end

#hash?Boolean

Returns:

  • (Boolean)

48
49
50
# File 'lib/active_object/object.rb', line 48

def hash?
  is_a?(Hash)
end

#integer?Boolean

Returns:

  • (Boolean)

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

def integer?
  is_a?(Integer)
end

#numeral?Boolean

Returns:

  • (Boolean)

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

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

#numeric?Boolean

Returns:

  • (Boolean)

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

def numeric?
  is_a?(Numeric)
end

#palindrome?Boolean

Returns:

  • (Boolean)

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

def palindrome?
  to_s == to_s.reverse
end

#present?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/active_object/object.rb', line 68

def present?
  !blank?
end

#range?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'lib/active_object/object.rb', line 72

def range?
  is_a?(Range)
end

#safe_call(*keys) ⇒ Object


76
77
78
# File 'lib/active_object/object.rb', line 76

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

#safe_send(*keys) ⇒ Object


80
81
82
# File 'lib/active_object/object.rb', line 80

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

#safe_try(*obj, &block) ⇒ Object


84
85
86
# File 'lib/active_object/object.rb', line 84

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

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


88
89
90
# File 'lib/active_object/object.rb', line 88

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

#send_chain(*keys) ⇒ Object


92
93
94
# File 'lib/active_object/object.rb', line 92

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

#send_chain_if(*keys) ⇒ Object


96
97
98
# File 'lib/active_object/object.rb', line 96

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

#send_if(key, *args) ⇒ Object


100
101
102
103
104
# File 'lib/active_object/object.rb', line 100

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

  send(key, *args)
end

#string?Boolean

Returns:

  • (Boolean)

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

def string?
  is_a?(String)
end

#symbol?Boolean

Returns:

  • (Boolean)

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

def symbol?
  is_a?(Symbol)
end

#time?Boolean

Returns:

  • (Boolean)

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

def time?
  is_a?(Time)
end

#true?Boolean

rubocop:disable Style/YodaCondition

Returns:

  • (Boolean)

119
120
121
# File 'lib/active_object/object.rb', line 119

def true?
  true == self
end

#truthy?Boolean

rubocop:enable Style/YodaCondition

Returns:

  • (Boolean)

124
125
126
# File 'lib/active_object/object.rb', line 124

def truthy?
  TRUE_VALUES.include?(self)
end

#try(*obj, &block) ⇒ Object


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

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

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


132
133
134
135
136
137
138
# File 'lib/active_object/object.rb', line 132

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


140
141
142
143
144
# File 'lib/active_object/object.rb', line 140

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

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

#try_send(*keys) ⇒ Object


146
147
148
# File 'lib/active_object/object.rb', line 146

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