class TranslatedCollection::Wrapper
Constants
- SENTINEL
Attributes
collection[R]
wrap_results[RW]
Public Class Methods
copy_clone_states(from,to)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 96 def self.copy_clone_states(from,to) to.taint if from.tainted? && ! to.tainted? to.freeze if from.frozen? && ! to.frozen? to end
new(collection, wrapfunc_in, wrapfunc_out, check = false)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 11 def initialize(collection, wrapfunc_in, wrapfunc_out, check = false) @collection = collection @wrapfunc_in = wrapfunc_in @wrapfunc_out = wrapfunc_out @wrap_results = true raise ArgumentError, "Non-conforming array provided" if check && !collection.empty? && !_conforming? end
Public Instance Methods
<<(value)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 45 def <<(value) @collection << @wrapfunc_in.call(value).tap do |xlated| changed notify_observers(self, :push, xlated) end end
Also aliased as: push
[](key)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 28 def [](key) @wrapfunc_out.call(@collection[key]) end
[]=(key, value)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 38 def []=(key, value) @collection[key] = @wrapfunc_in.call(value).tap do |xlated| changed notify_observers(self, :set, key, xlated) end end
_conforming?()
click to toggle source
Are all the elements of this collection unchanged when the “in” wrapper is applied to them?
# File lib/translated_collection/wrapper.rb, line 124 def _conforming? @collection.each do |elt| return false if @wrapfunc_in.call(elt) != elt end true end
_make_conforming!()
click to toggle source
# File lib/translated_collection/wrapper.rb, line 131 def _make_conforming! return if _conforming? xlated = @collection.map {|elt| @wrapfunc_in.call(elt) } @collection.clear xlated.each {|elt| @collection << elt } changed notify_observers(self, :misc) self end
_rewrap_array(result)
click to toggle source
Used to wrap results from various Enumerable methods that are defined to return an array
# File lib/translated_collection/wrapper.rb, line 152 def _rewrap_array(result) if @wrap_results newcoll = @collection.class.new(result) self.class.new(newcoll, @wrapfunc_in, @wrapfunc_out) else @collection.class.new(result.map(&@wrapfunc_out)) end end
_wrap_enumerator(enumerator)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 161 def _wrap_enumerator(enumerator) Enumerator.new do |y| loop do changed notify_observers(self, :misc) y << @wrapfunc_out.call(enumerator.next) end end end
clear()
click to toggle source
# File lib/translated_collection/wrapper.rb, line 88 def clear @collection.clear.tap do changed notify_observers(self, :clear) end self end
clone()
click to toggle source
# File lib/translated_collection/wrapper.rb, line 102 def clone dup.tap do |newobj| newobj.instance_variable_set("@collection", self.class.copy_clone_states(@collection, @collection.dup)) self.class.copy_clone_states(self, newobj) end end
delete(elt)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 70 def delete(elt) @collection.delete(@wrapfunc_in.call(elt)).tap do |removed| if removed changed notify_observers(self, :delete, removed) end end end
delete_at(pos)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 79 def delete_at(pos) @collection.delete_at(pos).tap do |removed| if removed changed notify_observers(self, :delete, removed) if removed end end end
dup()
click to toggle source
Calls superclass method
# File lib/translated_collection/wrapper.rb, line 109 def dup super.tap do |newobj| newobj.instance_variable_set("@collection", @collection.dup) end end
each() { |call| ... }
click to toggle source
# File lib/translated_collection/wrapper.rb, line 22 def each @collection.each do |elt| yield @wrapfunc_out.call(elt) end end
fetch(*args, &blk)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 34 def fetch(*args, &blk) @wrapfunc_out.call(@collection.__send__(:fetch, *args, &blk)) end
freeze()
click to toggle source
Calls superclass method
# File lib/translated_collection/wrapper.rb, line 115 def freeze @collection.freeze super end
is_a?(clazz)
click to toggle source
Calls superclass method
# File lib/translated_collection/wrapper.rb, line 142 def is_a?(clazz) super(clazz) || @collection.is_a?(clazz) end
Also aliased as: kind_of?
map!(&blk)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 206 def map!(&blk) @collection.map! {|x| @wrapfunc_in.call(blk.call(@wrapfunc_out.call(x))) } @wrap_results ? self : self.to_a end
Also aliased as: collect!
pop(count=nil)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 54 def pop(count=nil) if @collection.count > 0 if count == nil value = @wrapfunc_out.call(@collection.pop) else value = @collection.pop(count).map(&@wrapfunc_out) end changed notify_observers(self, :pop) else value = nil end value end
reject!(&blk)
click to toggle source
# File lib/translated_collection/wrapper.rb, line 198 def reject!(&blk) if blk @collection.reject! {|x| blk.call(@wrapfunc_out.call(x)) } && self else _wrap_enumerator(@collection.reject!) end end