Class: MoexRuby::LazyResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/moex_ruby/lazy_result.rb

Instance Method Summary collapse

Constructor Details

#initialize(enumerator) ⇒ LazyResult

Returns a new instance of LazyResult.



7
8
9
10
# File 'lib/moex_ruby/lazy_result.rb', line 7

def initialize(enumerator)
  @enumerator = enumerator
  @cached_array = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



80
81
82
83
84
# File 'lib/moex_ruby/lazy_result.rb', line 80

def method_missing(name, *args, &block)
  return super unless to_a.respond_to?(name)

  to_a.public_send(name, *args, &block)
end

Instance Method Details

#[](index) ⇒ Object



46
47
48
49
50
# File 'lib/moex_ruby/lazy_result.rb', line 46

def [](index)
  return @cached_array[index] if @cached_array

  to_a[index]
end

#each(&block) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/moex_ruby/lazy_result.rb', line 12

def each(&block)
  if block_given?
    return @cached_array.each(&block) if @cached_array

    @enumerator.each(&block)
  else
    to_enum(:each)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/moex_ruby/lazy_result.rb', line 40

def empty?
  return @cached_array.empty? if @cached_array

  size.zero?
end

#first(count = nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/moex_ruby/lazy_result.rb', line 52

def first(count = nil)
  if @cached_array
    return count ? @cached_array.first(count) : @cached_array.first
  end

  count ? to_a.first(count) : to_a.first
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/moex_ruby/lazy_result.rb', line 68

def is_a?(klass)
  super || klass == Array
end

#kind_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/moex_ruby/lazy_result.rb', line 72

def kind_of?(klass)
  is_a?(klass)
end

#last(count = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/moex_ruby/lazy_result.rb', line 60

def last(count = nil)
  if @cached_array
    return count ? @cached_array.last(count) : @cached_array.last
  end

  count ? to_a.last(count) : to_a.last
end

#lengthObject



36
37
38
# File 'lib/moex_ruby/lazy_result.rb', line 36

def length
  size
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/moex_ruby/lazy_result.rb', line 86

def respond_to_missing?(name, include_private = false)
  to_a.respond_to?(name, include_private) || super
end

#sizeObject



30
31
32
33
34
# File 'lib/moex_ruby/lazy_result.rb', line 30

def size
  return @cached_array.size if @cached_array
  @cached_array = @enumerator.to_a
  @cached_array.size
end

#to_aObject



22
23
24
# File 'lib/moex_ruby/lazy_result.rb', line 22

def to_a
  @cached_array ||= @enumerator.to_a
end

#to_aryObject



26
27
28
# File 'lib/moex_ruby/lazy_result.rb', line 26

def to_ary
  to_a
end

#to_enum(method = :each, *args) ⇒ Object



76
77
78
# File 'lib/moex_ruby/lazy_result.rb', line 76

def to_enum(method = :each, *args)
  @enumerator.to_enum(method, *args)
end