Class: Idolent::LazyArray

Inherits:
Object
  • Object
show all
Defined in:
lib/idolent.rb

Instance Method Summary collapse

Constructor Details

#initialize(array = [], clazz) ⇒ LazyArray

Returns a new instance of LazyArray.



24
25
26
27
28
29
# File 'lib/idolent.rb', line 24

def initialize(array = [], clazz)
  raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load)
  raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump)
  @array = array
  @clazz = clazz
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/idolent.rb', line 31

def method_missing(method, *args, &block)
  case method
    when :each, :select, :collect, :map, :reject, :delete_if
      @array.send(method) { |e| yield @clazz.load(e) }
    else
      @array.send(method, *args, &block)
  end
end

Instance Method Details

#<<(value) ⇒ Object



56
57
58
# File 'lib/idolent.rb', line 56

def <<(value)
  @array << @clazz.dump(value)
end

#[](index) ⇒ Object



48
49
50
# File 'lib/idolent.rb', line 48

def [](index)
  @clazz.load(@array[index])
end

#[]=(index, value) ⇒ Object



52
53
54
# File 'lib/idolent.rb', line 52

def []=(index, value)
  @array[index] = @clazz.dump(value)
end

#firstObject



40
41
42
# File 'lib/idolent.rb', line 40

def first
  @clazz.load(@array.first)
end

#lastObject



44
45
46
# File 'lib/idolent.rb', line 44

def last
  @clazz.load(@array.last)
end