Module: VV::SetMethods::SetAndArrayMethods
- Defined in:
- lib/vv/set_methods.rb
Instance Method Summary collapse
- #gravify ⇒ Object
- #gravify! ⇒ Object
- #includes!(other) ⇒ Object (also: #include!)
- #includes_all!(other) ⇒ Object (also: #include_all!)
- #includes_all?(other) ⇒ Boolean (also: #include_all?)
- #includes_any!(other) ⇒ Object (also: #include_any!)
- #includes_any?(other) ⇒ Boolean (also: #include_any?)
- #includes_one!(other) ⇒ Object (also: #include_one!)
- #includes_one?(other) ⇒ Boolean (also: #include_one?)
- #stringify_collection(grave: false) ⇒ Object
Instance Method Details
#gravify ⇒ Object
24 25 26 27 28 |
# File 'lib/vv/set_methods.rb', line 24 def gravify self.collect do |elem| "`#{elem}`" end end |
#gravify! ⇒ Object
30 31 32 33 34 |
# File 'lib/vv/set_methods.rb', line 30 def gravify! self.collect! do |elem| "`#{elem}`" end end |
#includes!(other) ⇒ Object Also known as: include!
36 37 38 39 |
# File 'lib/vv/set_methods.rb', line 36 def includes! other return if self.include? other fail "Collection does not include `#{other}`." end |
#includes_all!(other) ⇒ Object Also known as: include_all!
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/vv/set_methods.rb', line 84 def includes_all! other return true if includes_all? other other = other.to_set remaining = other - ( other & self ) count = remaining.count = "Collection " including = remaining.first(3).stringify_collection grave: true fail "Assertion error" if count < 1 if count < 4 += "does not include: #{including}." else += \ "does not include #{count} items, including: #{including}." end fail end |
#includes_all?(other) ⇒ Boolean Also known as: include_all?
75 76 77 78 79 80 81 |
# File 'lib/vv/set_methods.rb', line 75 def includes_all? other ok_type = other.is_a? Array ok_type ||= other.is_a? Set fail TypeError, "Expecting array or set" unless ok_type ( other.to_set & self ) == other.to_set end |
#includes_any!(other) ⇒ Object Also known as: include_any!
68 69 70 71 72 |
# File 'lib/vv/set_methods.rb', line 68 def includes_any! other return true if includes_any? other = "Collections did not share exactly any elements." fail end |
#includes_any?(other) ⇒ Boolean Also known as: include_any?
59 60 61 62 63 64 65 |
# File 'lib/vv/set_methods.rb', line 59 def includes_any?(other) ok_type = other.is_a? Array ok_type ||= other.is_a? Set fail TypeError, "Expecting array or set" unless ok_type ( self & other ).any? end |
#includes_one!(other) ⇒ Object Also known as: include_one!
52 53 54 55 56 |
# File 'lib/vv/set_methods.rb', line 52 def includes_one!(other) return true if includes_one? other = "Collections did not share exactly one element." fail end |
#includes_one?(other) ⇒ Boolean Also known as: include_one?
42 43 44 45 46 47 48 49 |
# File 'lib/vv/set_methods.rb', line 42 def includes_one?(other) ok_type = other.is_a? Array ok_type ||= other.is_a? Set fail TypeError, "Expecting array or set" unless ok_type ( self & other ).one? end |
#stringify_collection(grave: false) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/vv/set_methods.rb', line 106 def stringify_collection grave: false return self.gravify.stringify_collection if grave return String.empty_string if self.blank? return self.first if self.size == 1 return self.join " and " if self.size == 2 new_collection = self[0..-3] back_fragment = self[-2..-1].join ", and " new_collection << back_fragment new_collection.join ", " end |