Module: VV::ObjectMethods

Included in:
Object
Defined in:
lib/vv/object_methods.rb

Instance Method Summary collapse

Instance Method Details

#_considerationObject



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

Returns:

  • (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")

  message = \
  "Assertion failure: @__vv_consideration_dismissed_level not defined"
  fail message unless defined

  !! @__vv_consideration_dismissed_level
end

#_dismiss_considerationObject



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")
  message = "Assertion failure: Consideration level not set"
  fail message unless defined

  message = \
  "Assertion failure: Consideration level mismatch."
  size = @__vv_considerations.size
  level_ok = size == ( @__vv_consideration_level + 1 )
  fail message unless level_ok
end

#blank?Boolean

Returns:

  • (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
  message = \
  "`cli_printable` requires `cli_print` on child class"
  fail NoMethodError, message
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
  message = \
  "Expected `#{klass}` instance, not `#{self.class}`."
  fail ArgumentError, message 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
  message = "#{klass} `#{self}` is invalid. Must be one of: #{collection}."
  fail message
end

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

Returns:

  • (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

  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

#otherwiseObject



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
  message = "Multiple otherwise methods in consideration."
  fail message if @__vv_consideration_others[level]
  @__vv_consideration_others[level] = true

  return if self._consideration_dismissed?

  response = yield
  self._dismiss_consideration
  response
end

#present?Boolean

Returns:

  • (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