class Stockboy::ProviderRepeater

Constants

ProviderStats
YIELD_ONCE

Attributes

base_provider[R]

Public Class Methods

new(provider, &yielder) click to toggle source
# File lib/stockboy/provider_repeater.rb, line 18
def initialize(provider, &yielder)
  @orig_provider = provider
  @base_provider = provider.dup
  @iterations = []
  @yielder = yielder || YIELD_ONCE
end

Public Instance Methods

clear() click to toggle source
# File lib/stockboy/provider_repeater.rb, line 64
def clear
  @base_provider = @orig_provider.dup
  @iterations.clear
end
data() { |fetch_iteration_data(nth_provider)| ... } click to toggle source
# File lib/stockboy/provider_repeater.rb, line 54
def data
  unless block_given?
    raise ArgumentError, "expect a block for yielding data iterations"
  end

  each do |nth_provider|
    yield fetch_iteration_data(nth_provider)
  end
end
data?(reduction = :all?) click to toggle source

Determine if there was any returned data after processing iterations @param [:all?,:any?,:one?] reduction

Specify if all iterations must return data to be valid, or just any
# File lib/stockboy/provider_repeater.rb, line 29
def data?(reduction = :all?)
  return nil if data_iterations == 0
  @iterations.send(reduction, &:data?)
end
data_iterations() click to toggle source
# File lib/stockboy/provider_repeater.rb, line 50
def data_iterations
  @iterations.size
end
data_size() click to toggle source

Get the total data size returned after processing iterations

# File lib/stockboy/provider_repeater.rb, line 36
def data_size
  @iterations.reduce(nil) { |sum, source|
    source.data_size ? source.data_size + sum.to_i : sum
  }
end
data_time() click to toggle source

Get the last data time returned after processing iterations

# File lib/stockboy/provider_repeater.rb, line 44
def data_time
  @iterations.reduce(nil) { |max, source|
    source.data_time && (max.nil? || source.data_time > max) ? source.data_time : max
  }
end
each() { |provider| ... } click to toggle source
# File lib/stockboy/provider_repeater.rb, line 69
def each
  return to_enum unless block_given?
  enum = to_enum
  while true
    begin
      provider = enum.next
      unless provider.respond_to? :data
        raise ArgumentError, "expected Provider, got #{provider.class}"
      end
    rescue StopIteration
      return provider
    end
    yield provider
    provider.clear
  end
end
to_enum() click to toggle source
# File lib/stockboy/provider_repeater.rb, line 86
def to_enum
  Enumerator.new do |y|
    begin
      @yielder.call(y, base_provider)
    rescue LocalJumpError
      raise $!, "use output << provider instead of yield", $!.backtrace
    end
  end
end

Private Instance Methods

fetch_iteration_data(provider) click to toggle source
# File lib/stockboy/provider_repeater.rb, line 102
def fetch_iteration_data(provider)
  if provider.data
    @iterations << ProviderStats.from(provider)
  end
  provider.data
end
method_missing(method, *args, &block) click to toggle source
# File lib/stockboy/provider_repeater.rb, line 98
def method_missing(method, *args, &block)
  base_provider.public_send(method, *args, &block)
end