Class: Interfacets::Shared::Entities::CollectionProxy
- Inherits:
-
Object
- Object
- Interfacets::Shared::Entities::CollectionProxy
- Includes:
- Enumerable
- Defined in:
- lib/interfacets/shared/entities/collection_proxy.rb
Instance Attribute Summary collapse
-
#unwrap ⇒ Object
readonly
Returns the value of attribute unwrap.
-
#wrap ⇒ Object
readonly
Returns the value of attribute wrap.
Instance Method Summary collapse
-
#<<(item) ⇒ Object
Mutation operations - unwrap items and delegate to underlying collection.
- #[](index) ⇒ Object
- #[]=(index, item) ⇒ Object
- #at(index) ⇒ Object
- #clear ⇒ Object
- #compact ⇒ Object
- #concat(other_array) ⇒ Object
- #delete(item) ⇒ Object
- #delete_at(index) ⇒ Object
- #drop(n) ⇒ Object
- #drop_while(&block) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#first ⇒ Object
Read operations - wrap items when accessing.
-
#initialize(collection, wrap:, unwrap:) ⇒ CollectionProxy
constructor
A new instance of CollectionProxy.
- #insert(index, *items) ⇒ Object
- #last ⇒ Object
- #pop ⇒ Object
- #push(*items) ⇒ Object
- #reject(&block) ⇒ Object
- #replace(other_array) ⇒ Object
- #reverse ⇒ Object
-
#select(&block) ⇒ Object
(also: #filter)
Enumerable methods that return arrays should return CollectionProxy.
- #shift ⇒ Object
- #size ⇒ Object (also: #length, #count)
- #slice(*args) ⇒ Object
- #sort(&block) ⇒ Object
- #sort_by(&block) ⇒ Object
- #take(n) ⇒ Object
- #take_while(&block) ⇒ Object
- #uniq(&block) ⇒ Object
- #unshift(*items) ⇒ Object
Constructor Details
#initialize(collection, wrap:, unwrap:) ⇒ CollectionProxy
Returns a new instance of CollectionProxy.
10 11 12 13 14 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 10 def initialize(collection, wrap:, unwrap:) @collection = collection @wrap = wrap @unwrap = unwrap end |
Instance Attribute Details
#unwrap ⇒ Object (readonly)
Returns the value of attribute unwrap.
9 10 11 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 9 def unwrap @unwrap end |
#wrap ⇒ Object (readonly)
Returns the value of attribute wrap.
9 10 11 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 9 def wrap @wrap end |
Instance Method Details
#<<(item) ⇒ Object
Mutation operations - unwrap items and delegate to underlying collection
52 53 54 55 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 52 def <<(item) @collection << @unwrap.call(item) self end |
#[](index) ⇒ Object
27 28 29 30 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 27 def [](index) val = @collection[index] val.nil? ? nil : @wrap.call(val) end |
#[]=(index, item) ⇒ Object
77 78 79 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 77 def []=(index, item) @collection[index] = @unwrap.call(item) end |
#at(index) ⇒ Object
32 33 34 35 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 32 def at(index) val = @collection.at(index) val.nil? ? nil : @wrap.call(val) end |
#clear ⇒ Object
92 93 94 95 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 92 def clear @collection.clear self end |
#compact ⇒ Object
172 173 174 175 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 172 def compact compacted = @collection.compact CollectionProxy.new(compacted, wrap: @wrap, unwrap: @unwrap) end |
#concat(other_array) ⇒ Object
102 103 104 105 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 102 def concat(other_array) @collection.concat(other_array.map { |item| @unwrap.call(item) }) self end |
#delete(item) ⇒ Object
81 82 83 84 85 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 81 def delete(item) unwrapped = @unwrap.call(item) val = @collection.delete(unwrapped) val.nil? ? nil : @wrap.call(val) end |
#delete_at(index) ⇒ Object
87 88 89 90 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 87 def delete_at(index) val = @collection.delete_at(index) val.nil? ? nil : @wrap.call(val) end |
#drop(n) ⇒ Object
134 135 136 137 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 134 def drop(n) dropped = @collection.drop(n) CollectionProxy.new(dropped, wrap: @wrap, unwrap: @unwrap) end |
#drop_while(&block) ⇒ Object
139 140 141 142 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 139 def drop_while(&block) dropped = @collection.drop_while { |item| block.call(@wrap.call(item)) } CollectionProxy.new(dropped, wrap: @wrap, unwrap: @unwrap) end |
#each(&block) ⇒ Object
37 38 39 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 37 def each(&block) @collection.each { |item| block.call(@wrap.call(item)) } end |
#empty? ⇒ Boolean
47 48 49 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 47 def empty? @collection.empty? end |
#first ⇒ Object
Read operations - wrap items when accessing
17 18 19 20 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 17 def first val = @collection.first val.nil? ? nil : @wrap.call(val) end |
#insert(index, *items) ⇒ Object
97 98 99 100 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 97 def insert(index, *items) @collection.insert(index, *items.map { |item| @unwrap.call(item) }) self end |
#last ⇒ Object
22 23 24 25 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 22 def last val = @collection.last val.nil? ? nil : @wrap.call(val) end |
#pop ⇒ Object
62 63 64 65 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 62 def pop val = @collection.pop val.nil? ? nil : @wrap.call(val) end |
#push(*items) ⇒ Object
57 58 59 60 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 57 def push(*items) @collection.push(*items.map { |item| @unwrap.call(item) }) self end |
#reject(&block) ⇒ Object
119 120 121 122 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 119 def reject(&block) rejected = @collection.reject { |item| block.call(@wrap.call(item)) } CollectionProxy.new(rejected, wrap: @wrap, unwrap: @unwrap) end |
#replace(other_array) ⇒ Object
107 108 109 110 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 107 def replace(other_array) @collection.replace(other_array.map { |item| @unwrap.call(item) }) self end |
#reverse ⇒ Object
144 145 146 147 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 144 def reverse reversed = @collection.reverse CollectionProxy.new(reversed, wrap: @wrap, unwrap: @unwrap) end |
#select(&block) ⇒ Object Also known as: filter
Enumerable methods that return arrays should return CollectionProxy
113 114 115 116 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 113 def select(&block) selected = @collection.select { |item| block.call(@wrap.call(item)) } CollectionProxy.new(selected, wrap: @wrap, unwrap: @unwrap) end |
#shift ⇒ Object
67 68 69 70 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 67 def shift val = @collection.shift val.nil? ? nil : @wrap.call(val) end |
#size ⇒ Object Also known as: length, count
41 42 43 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 41 def size @collection.size end |
#slice(*args) ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 177 def slice(*args) sliced = @collection.slice(*args) if sliced.is_a?(Array) CollectionProxy.new(sliced, wrap: @wrap, unwrap: @unwrap) else # Single element access returns wrapped item sliced.nil? ? nil : @wrap.call(sliced) end end |
#sort(&block) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 149 def sort(&block) if block sorted = @collection.sort { |a, b| block.call(@wrap.call(a), @wrap.call(b)) } else sorted = @collection.sort end CollectionProxy.new(sorted, wrap: @wrap, unwrap: @unwrap) end |
#sort_by(&block) ⇒ Object
158 159 160 161 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 158 def sort_by(&block) sorted = @collection.sort_by { |item| block.call(@wrap.call(item)) } CollectionProxy.new(sorted, wrap: @wrap, unwrap: @unwrap) end |
#take(n) ⇒ Object
124 125 126 127 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 124 def take(n) taken = @collection.take(n) CollectionProxy.new(taken, wrap: @wrap, unwrap: @unwrap) end |
#take_while(&block) ⇒ Object
129 130 131 132 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 129 def take_while(&block) taken = @collection.take_while { |item| block.call(@wrap.call(item)) } CollectionProxy.new(taken, wrap: @wrap, unwrap: @unwrap) end |
#uniq(&block) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 163 def uniq(&block) if block uniqued = @collection.uniq { |item| block.call(@wrap.call(item)) } else uniqued = @collection.uniq end CollectionProxy.new(uniqued, wrap: @wrap, unwrap: @unwrap) end |
#unshift(*items) ⇒ Object
72 73 74 75 |
# File 'lib/interfacets/shared/entities/collection_proxy.rb', line 72 def unshift(*items) @collection.unshift(*items.map { |item| @unwrap.call(item) }) self end |