# File lib/dm-core/support/lazy_array.rb, line 169 169: def <<(entry) 170: if loaded? 171: lazy_load 172: @array << entry 173: else 174: @tail << entry 175: end 176: self 177: end
# File lib/dm-core/support/lazy_array.rb, line 315 315: def ==(other) 316: if equal?(other) 317: return true 318: end 319: 320: unless other.respond_to?(:to_ary) 321: return false 322: end 323: 324: # if necessary, convert to something that can be compared 325: other = other.to_ary unless other.respond_to?(:[]) 326: 327: cmp?(other, :==) 328: end
# File lib/dm-core/support/lazy_array.rb, line 100 100: def [](*args) 101: index, length = extract_slice_arguments(*args) 102: 103: if length == 1 && args.size == 1 && args.first.kind_of?(Integer) 104: return at(index) 105: end 106: 107: if index >= 0 && lazy_possible?(@head, index + length) 108: @head[*args] 109: elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length) 110: @tail[*args] 111: else 112: lazy_load 113: @array[*args] 114: end 115: end
# File lib/dm-core/support/lazy_array.rb, line 132 132: def []=(*args) 133: index, length = extract_slice_arguments(*args[0..2]) 134: 135: if index >= 0 && lazy_possible?(@head, index + length) 136: @head.[]=(*args) 137: elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length) 138: @tail.[]=(*args) 139: else 140: lazy_load 141: @array.[]=(*args) 142: end 143: end
# File lib/dm-core/support/lazy_array.rb, line 92 92: def any?(&block) 93: (lazy_possible?(@tail) && @tail.any?(&block)) || 94: (lazy_possible?(@head) && @head.any?(&block)) || begin 95: lazy_load 96: @array.any?(&block) 97: end 98: end
# File lib/dm-core/support/lazy_array.rb, line 24 24: def at(index) 25: if index >= 0 && lazy_possible?(@head, index + 1) 26: @head.at(index) 27: elsif index < 0 && lazy_possible?(@tail, index.abs) 28: @tail.at(index) 29: else 30: lazy_load 31: @array.at(index) 32: end 33: end
# File lib/dm-core/support/lazy_array.rb, line 268 268: def clear 269: mark_loaded 270: @array.clear 271: self 272: end
# File lib/dm-core/support/lazy_array.rb, line 179 179: def concat(other) 180: if loaded? 181: lazy_load 182: @array.concat(other) 183: else 184: @tail.concat(other) 185: end 186: self 187: end
# File lib/dm-core/support/lazy_array.rb, line 239 239: def delete_at(index) 240: if index >= 0 && lazy_possible?(@head, index + 1) 241: @head.delete_at(index) 242: elsif index < 0 && lazy_possible?(@tail, index.abs) 243: @tail.delete_at(index) 244: else 245: lazy_load 246: @array.delete_at(index) 247: end 248: end
# File lib/dm-core/support/lazy_array.rb, line 250 250: def delete_if(&block) 251: if loaded? 252: lazy_load 253: @array.delete_if(&block) 254: else 255: @reapers << block 256: @head.delete_if(&block) 257: @tail.delete_if(&block) 258: end 259: self 260: end
# File lib/dm-core/support/lazy_array.rb, line 84 84: def empty? 85: (@tail.nil? || @tail.empty?) && 86: (@head.nil? || @head.empty?) && begin 87: lazy_load 88: @array.empty? 89: end 90: end
# File lib/dm-core/support/lazy_array.rb, line 330 330: def eql?(other) 331: if equal?(other) 332: return true 333: end 334: 335: unless other.class.equal?(self.class) 336: return false 337: end 338: 339: cmp?(other, :eql?) 340: end
# File lib/dm-core/support/lazy_array.rb, line 35 35: def fetch(*args, &block) 36: index = args.first 37: 38: if index >= 0 && lazy_possible?(@head, index + 1) 39: @head.fetch(*args, &block) 40: elsif index < 0 && lazy_possible?(@tail, index.abs) 41: @tail.fetch(*args, &block) 42: else 43: lazy_load 44: @array.fetch(*args, &block) 45: end 46: end
# File lib/dm-core/support/lazy_array.rb, line 6 6: def first(*args) 7: if lazy_possible?(@head, *args) 8: @head.first(*args) 9: else 10: lazy_load 11: @array.first(*args) 12: end 13: end
# File lib/dm-core/support/lazy_array.rb, line 300 300: def freeze 301: if loaded? 302: @array.freeze 303: else 304: @head.freeze 305: @tail.freeze 306: end 307: @frozen = true 308: self 309: end
# File lib/dm-core/support/lazy_array.rb, line 311 311: def frozen? 312: @frozen == true 313: end
# File lib/dm-core/support/lazy_array.rb, line 76 76: def include?(entry) 77: (lazy_possible?(@tail) && @tail.include?(entry)) || 78: (lazy_possible?(@head) && @head.include?(entry)) || begin 79: lazy_load 80: @array.include?(entry) 81: end 82: end
# File lib/dm-core/support/lazy_array.rb, line 69 69: def index(entry) 70: (lazy_possible?(@head) && @head.index(entry)) || begin 71: lazy_load 72: @array.index(entry) 73: end 74: end
# File lib/dm-core/support/lazy_array.rb, line 209 209: def insert(index, *entries) 210: if index >= 0 && lazy_possible?(@head, index) 211: @head.insert(index, *entries) 212: elsif index < 0 && lazy_possible?(@tail, index.abs - 1) 213: @tail.insert(index, *entries) 214: else 215: lazy_load 216: @array.insert(index, *entries) 217: end 218: self 219: end
# File lib/dm-core/support/lazy_array.rb, line 290 290: def kind_of?(klass) 291: super || @array.kind_of?(klass) 292: end
# File lib/dm-core/support/lazy_array.rb, line 15 15: def last(*args) 16: if lazy_possible?(@tail, *args) 17: @tail.last(*args) 18: else 19: lazy_load 20: @array.last(*args) 21: end 22: end
# File lib/dm-core/support/lazy_array.rb, line 342 342: def lazy_possible?(list, need_length = 1) 343: !loaded? && need_length <= list.size 344: end
# File lib/dm-core/support/lazy_array.rb, line 281 281: def load_with(&block) 282: @load_with_proc = block 283: self 284: end
# File lib/dm-core/support/lazy_array.rb, line 286 286: def loaded? 287: @loaded == true 288: end
# File lib/dm-core/support/lazy_array.rb, line 221 221: def pop(*args) 222: if lazy_possible?(@tail, *args) 223: @tail.pop(*args) 224: else 225: lazy_load 226: @array.pop(*args) 227: end 228: end
# File lib/dm-core/support/lazy_array.rb, line 189 189: def push(*entries) 190: if loaded? 191: lazy_load 192: @array.push(*entries) 193: else 194: @tail.push(*entries) 195: end 196: self 197: end
# File lib/dm-core/support/lazy_array.rb, line 262 262: def replace(other) 263: mark_loaded 264: @array.replace(other) 265: self 266: end
# File lib/dm-core/support/lazy_array.rb, line 296 296: def respond_to?(method, include_private = false) 297: super || @array.respond_to?(method) 298: end
# File lib/dm-core/support/lazy_array.rb, line 147 147: def reverse 148: dup.reverse! 149: end
# File lib/dm-core/support/lazy_array.rb, line 151 151: def reverse! 152: # reverse without kicking if possible 153: if loaded? 154: @array = @array.reverse 155: else 156: @head, @tail = @tail.reverse, @head.reverse 157: 158: proc = @load_with_proc 159: 160: @load_with_proc = lambda do |v| 161: proc.call(v) 162: v.instance_variable_get(:@array).reverse! 163: end 164: end 165: 166: self 167: end
# File lib/dm-core/support/lazy_array.rb, line 230 230: def shift(*args) 231: if lazy_possible?(@head, *args) 232: @head.shift(*args) 233: else 234: lazy_load 235: @array.shift(*args) 236: end 237: end
# File lib/dm-core/support/lazy_array.rb, line 119 119: def slice!(*args) 120: index, length = extract_slice_arguments(*args) 121: 122: if index >= 0 && lazy_possible?(@head, index + length) 123: @head.slice!(*args) 124: elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length) 125: @tail.slice!(*args) 126: else 127: lazy_load 128: @array.slice!(*args) 129: end 130: end
# File lib/dm-core/support/lazy_array.rb, line 274 274: def to_a 275: lazy_load 276: @array.to_a 277: end
# File lib/dm-core/support/lazy_array.rb, line 199 199: def unshift(*entries) 200: if loaded? 201: lazy_load 202: @array.unshift(*entries) 203: else 204: @head.unshift(*entries) 205: end 206: self 207: end
# File lib/dm-core/support/lazy_array.rb, line 48 48: def values_at(*args) 49: accumulator = [] 50: 51: lazy_possible = args.all? do |arg| 52: index, length = extract_slice_arguments(arg) 53: 54: if index >= 0 && lazy_possible?(@head, index + length) 55: accumulator.concat(head.values_at(*arg)) 56: elsif index < 0 && lazy_possible?(@tail, index.abs) 57: accumulator.concat(tail.values_at(*arg)) 58: end 59: end 60: 61: if lazy_possible 62: accumulator 63: else 64: lazy_load 65: @array.values_at(*args) 66: end 67: end
# File lib/dm-core/support/lazy_array.rb, line 430 430: def cmp?(other, operator) 431: unless loaded? 432: # compare the head against the beginning of other. start at index 433: # 0 and incrementally compare each entry. if other is a LazyArray 434: # this has a lesser likelyhood of triggering a lazy load 435: 0.upto(@head.size - 1) do |i| 436: return false unless @head[i].__send__(operator, other[i]) 437: end 438: 439: # compare the tail against the end of other. start at index 440: # -1 and decrementally compare each entry. if other is a LazyArray 441: # this has a lesser likelyhood of triggering a lazy load 442: 1.downto(@tail.size * 1) do |i| 443: return false unless @tail[i].__send__(operator, other[i]) 444: end 445: 446: lazy_load 447: end 448: 449: @array.send(operator, other.to_ary) 450: end
# File lib/dm-core/support/lazy_array.rb, line 408 408: def each 409: lazy_load 410: if block_given? 411: @array.each { |entry| yield entry } 412: self 413: else 414: @array.each 415: end 416: end
Extract arguments for # an # and return index and length
@param [Integer, Array(Integer), Range] *args the index,
index and length, or range indicating first and last position
@return [Integer] the index @return [Integer,NilClass] the length, if any
@api private
# File lib/dm-core/support/lazy_array.rb, line 389 389: def extract_slice_arguments(*args) 390: first_arg, second_arg = args 391: 392: if args.size == 2 && first_arg.kind_of?(Integer) && second_arg.kind_of?(Integer) 393: return first_arg, second_arg 394: elsif args.size == 1 395: if first_arg.kind_of?(Integer) 396: return first_arg, 1 397: elsif first_arg.kind_of?(Range) 398: index = first_arg.first 399: length = first_arg.last - index 400: length += 1 unless first_arg.exclude_end? 401: return index, length 402: end 403: end 404: 405: raise ArgumentError, "arguments may be 1 or 2 Integers, or 1 Range object, was: #{args.inspect}", caller(1) 406: end
# File lib/dm-core/support/lazy_array.rb, line 358 358: def initialize_copy(original) 359: @head = DataMapper::Ext.try_dup(@head) 360: @tail = DataMapper::Ext.try_dup(@tail) 361: @array = DataMapper::Ext.try_dup(@array) 362: end
# File lib/dm-core/support/lazy_array.rb, line 364 364: def lazy_load 365: return if loaded? 366: mark_loaded 367: @load_with_proc[self] 368: @array.unshift(*@head) 369: @array.concat(@tail) 370: @head = @tail = nil 371: @reapers.each { |r| @array.delete_if(&r) } if @reapers 372: @array.freeze if frozen? 373: end
# File lib/dm-core/support/lazy_array.rb, line 375 375: def mark_loaded 376: @loaded = true 377: end
delegate any not-explicitly-handled methods to @array, if possible. this is handy for handling methods mixed-into Array like group_by
# File lib/dm-core/support/lazy_array.rb, line 420 420: def method_missing(method, *args, &block) 421: if @array.respond_to?(method) 422: lazy_load 423: results = @array.send(method, *args, &block) 424: results.equal?(@array) ? self : results 425: else 426: super 427: end 428: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.