Class: Object

Inherits:
BasicObject
Defined in:
lib/vv/object_methods.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/vv/object_methods.rb', line 5

def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

#cli_printable(**kwargs) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/vv/object_methods.rb', line 9

def cli_printable **kwargs
  String.get_stdout { self.cli_print( **kwargs ) }
rescue NoMethodError
  message = \
  "`cli_printable` requires `cli_print` on child class"
  fail NoMethodError, message
end

#is_a!(klass) ⇒ Object Also known as: must_be_a!



50
51
52
53
54
# File 'lib/vv/object_methods.rb', line 50

def is_a! klass
  message = \
  "Expected `#{klass}` instance, not `#{self.class}`."
  fail ArgumentError, message unless self.is_a? klass
end

#one_of!(*collection, mixed: false) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/vv/object_methods.rb', line 41

def one_of! *collection, mixed: false
  return true if self.one_of?( *collection, mixed: mixed )

  klass = self.class
  collection = collection.stringify_collection grave: true
  message = "#{klass} `#{self}` is invalid. Must be one of: #{collection}."
  fail message
end

#one_of?(*collection, mixed: false, allow_unsafe: false) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vv/object_methods.rb', line 21

def one_of? *collection, mixed: false, allow_unsafe: false
  nested   = collection.first.is_a? Array
  nested ||= collection.first.is_a? Hash
  nested ||= collection.first.is_a? Set

  message = \
  "Unexpected nested argument. If desired set `allow_unsafe: true`."
  fail ArgumentError, message if nested unless allow_unsafe

  return collection.include? self if mixed

  klass = self.class
  ok = collection.reject {|s| s.is_a? klass }.blank?

  message = "Invalid types: #{klass} collection required."
  fail ArgumentError, message unless ok

  collection.include? self
end

#present?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/vv/object_methods.rb', line 17

def present?
  !blank?
end