Module: VV::ObjectMethods
- Included in:
- Object
- Defined in:
- lib/vv/object_methods.rb
Instance Method Summary collapse
- #_consideration ⇒ Object
- #_consideration_dismissed? ⇒ Boolean
- #_dismiss_consideration ⇒ Object
- #_ensure_consideration_level! ⇒ Object
- #blank? ⇒ Boolean
- #cli_printable(**kwargs) ⇒ Object
- #consider(input, &block) ⇒ Object
- #given(potential_match) ⇒ Object
- #is_a!(klass) ⇒ Object (also: #must_be_a!)
- #one_of!(*collection, mixed: false) ⇒ Object
- #one_of?(*collection, mixed: false, allow_unsafe: false) ⇒ Boolean
- #otherwise ⇒ Object
- #present? ⇒ Boolean
- #set_attrs_via(*attributes, document:) ⇒ Object
- #within(potential_enum) ⇒ Object
Instance Method Details
#_consideration ⇒ Object
169 170 171 172 |
# File 'lib/vv/object_methods.rb', line 169 def _consideration self._ensure_consideration_level! @__vv_considerations.last end |
#_consideration_dismissed? ⇒ Boolean
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/vv/object_methods.rb', line 143 def _consideration_dismissed? defined = \ instance_variable_defined?("@__vv_consideration_dismissed_level") = \ "Assertion failure: @__vv_consideration_dismissed_level not defined" fail unless defined !! @__vv_consideration_dismissed_level end |
#_dismiss_consideration ⇒ Object
137 138 139 140 141 |
# File 'lib/vv/object_methods.rb', line 137 def _dismiss_consideration self._ensure_consideration_level! @__vv_consideration_dismissed_level = \ @__vv_consideration_level end |
#_ensure_consideration_level! ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/vv/object_methods.rb', line 154 def _ensure_consideration_level! defined = instance_variable_defined?("@__vv_considerations") fail "No current consideration active" unless defined defined = instance_variable_defined?("@__vv_consideration_level") = "Assertion failure: Consideration level not set" fail unless defined = \ "Assertion failure: Consideration level mismatch." size = @__vv_considerations.size level_ok = size == ( @__vv_consideration_level + 1 ) fail unless level_ok end |
#blank? ⇒ Boolean
85 86 87 |
# File 'lib/vv/object_methods.rb', line 85 def blank? respond_to?(:empty?) ? !!empty? : !self end |
#cli_printable(**kwargs) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/vv/object_methods.rb', line 89 def cli_printable **kwargs String.get_stdout { self.cli_print( **kwargs ) } rescue NoMethodError = \ "`cli_printable` requires `cli_print` on child class" fail NoMethodError, end |
#consider(input, &block) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/vv/object_methods.rb', line 7 def consider input, &block @__vv_consideration_dismissed_level ||= false # For a potential parent consideration return if self._consideration_dismissed? @__vv_consideration_others ||= Hash.new @__vv_consideration_level ||= -1 @__vv_consideration_level += 1 @__vv_considerations ||= Array.new @__vv_considerations << input yield ensure if @__vv_consideration_dismissed_level == \ @__vv_consideration_level remove_instance_variable :@__vv_consideration_dismissed_level end @__vv_consideration_level -= 1 if @__vv_consideration_level < 0 remove_instance_variable :@__vv_consideration_level remove_instance_variable :@__vv_consideration_others end @__vv_considerations.pop unless @__vv_considerations.any? remove_instance_variable :@__vv_considerations end end |
#given(potential_match) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/vv/object_methods.rb', line 37 def given potential_match return if self._consideration_dismissed? return unless potential_match == self._consideration response = yield self._dismiss_consideration response end |
#is_a!(klass) ⇒ Object Also known as: must_be_a!
130 131 132 133 134 |
# File 'lib/vv/object_methods.rb', line 130 def is_a! klass = \ "Expected `#{klass}` instance, not `#{self.class}`." fail ArgumentError, unless self.is_a? klass end |
#one_of!(*collection, mixed: false) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/vv/object_methods.rb', line 121 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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/vv/object_methods.rb', line 101 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 |
#otherwise ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vv/object_methods.rb', line 55 def otherwise self._ensure_consideration_level! level = @__vv_consideration_level = "Multiple otherwise methods in consideration." fail if @__vv_consideration_others[level] @__vv_consideration_others[level] = true return if self._consideration_dismissed? response = yield self._dismiss_consideration response end |
#present? ⇒ Boolean
97 98 99 |
# File 'lib/vv/object_methods.rb', line 97 def present? !blank? end |
#set_attrs_via(*attributes, document:) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vv/object_methods.rb', line 70 def set_attrs_via( *attributes, document: ) attributes.flatten! document.keys.to_set.includes_all! attributes attributes.each do |attribute| value = document.fetch(attribute) setter = attribute.setter_sym insta = attribute.insta_sym if self.respond_to? setter self.public_send setter, value else self.instance_variable_set insta, value end end end |
#within(potential_enum) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/vv/object_methods.rb', line 46 def within potential_enum return if self._consideration_dismissed? return unless potential_enum.include? self._consideration response = yield self._dismiss_consideration response end |