Class: Stockboy::ProviderRepeater

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

Defined Under Namespace

Classes: ProviderStats

Constant Summary collapse

YIELD_ONCE =
proc { |output, provider| output << provider }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, &yielder) ⇒ ProviderRepeater

Returns a new instance of ProviderRepeater.



18
19
20
21
22
23
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



98
99
100
# File 'lib/stockboy/provider_repeater.rb', line 98

def method_missing(method, *args, &block)
  base_provider.public_send(method, *args, &block)
end

Instance Attribute Details

#base_providerObject (readonly)

Returns the value of attribute base_provider.



16
17
18
# File 'lib/stockboy/provider_repeater.rb', line 16

def base_provider
  @base_provider
end

Instance Method Details

#clearObject



64
65
66
67
# File 'lib/stockboy/provider_repeater.rb', line 64

def clear
  @base_provider = @orig_provider.dup
  @iterations.clear
end

#dataObject



54
55
56
57
58
59
60
61
62
# 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?) ⇒ Boolean

Determine if there was any returned data after processing iterations

Parameters:

  • reduction (:all?, :any?, :one?) (defaults to: :all?)

    Specify if all iterations must return data to be valid, or just any

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/stockboy/provider_repeater.rb', line 29

def data?(reduction = :all?)
  return nil if data_iterations == 0
  @iterations.send(reduction, &:data?)
end

#data_iterationsObject



50
51
52
# File 'lib/stockboy/provider_repeater.rb', line 50

def data_iterations
  @iterations.size
end

#data_sizeObject

Get the total data size returned after processing iterations



36
37
38
39
40
# 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_timeObject

Get the last data time returned after processing iterations



44
45
46
47
48
# 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

#eachObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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_enumObject



86
87
88
89
90
91
92
93
94
# 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