Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/vv/object_methods.rb
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #cli_printable(**kwargs) ⇒ Object
- #is_a!(klass) ⇒ Object (also: #must_be_a!)
- #one_of!(*collection, mixed: false) ⇒ Object
- #one_of?(*collection, mixed: false, allow_unsafe: false) ⇒ Boolean
- #present? ⇒ Boolean
Instance Method Details
#blank? ⇒ 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 = \ "`cli_printable` requires `cli_print` on child class" fail NoMethodError, 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 = \ "Expected `#{klass}` instance, not `#{self.class}`." fail ArgumentError, 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 = "#{klass} `#{self}` is invalid. Must be one of: #{collection}." fail end |
#one_of?(*collection, mixed: false, allow_unsafe: false) ⇒ 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 = \ "Unexpected nested argument. If desired set `allow_unsafe: true`." fail ArgumentError, if nested unless allow_unsafe return collection.include? self if mixed klass = self.class ok = collection.reject {|s| s.is_a? klass }.blank? = "Invalid types: #{klass} collection required." fail ArgumentError, unless ok collection.include? self end |
#present? ⇒ Boolean
17 18 19 |
# File 'lib/vv/object_methods.rb', line 17 def present? !blank? end |