Module: Plug::ArrayFeeder

Includes:
Base
Defined in:
lib/rbkb/plug/plug.rb

Overview

Uses an array of static messages as a datasource for opaque protocol messages. Useful as a generic blit-able loop

Instance Attribute Summary collapse

Attributes included from Base

#kind, #no_stop_on_unbind, #peers, #tls, #tls_opts, #transport

Instance Method Summary collapse

Methods included from Base

#name, #plug_peer, #plug_receive, #post_init, #receive_data, #unbind

Instance Attribute Details

#close_at_end ⇒ Object

Returns the value of attribute close_at_end.



163
164
165
# File 'lib/rbkb/plug/plug.rb', line 163

def close_at_end
  @close_at_end
end

#feed ⇒ Object

Returns the value of attribute feed.



163
164
165
# File 'lib/rbkb/plug/plug.rb', line 163

def feed
  @feed
end

#go_first ⇒ Object

Returns the value of attribute go_first.



163
164
165
# File 'lib/rbkb/plug/plug.rb', line 163

def go_first
  @go_first
end

#pos ⇒ Object

Returns the value of attribute pos.



163
164
165
# File 'lib/rbkb/plug/plug.rb', line 163

def pos
  @pos
end

#squelch_exhausted ⇒ Object

Returns the value of attribute squelch_exhausted.



163
164
165
# File 'lib/rbkb/plug/plug.rb', line 163

def squelch_exhausted
  @squelch_exhausted
end

#step ⇒ Object

Returns the value of attribute step.



163
164
165
# File 'lib/rbkb/plug/plug.rb', line 163

def step
  @step
end

Instance Method Details

#connection_completed ⇒ Object



182
183
184
185
186
# File 'lib/rbkb/plug/plug.rb', line 182

def connection_completed
  peer = super()
  go if @go_first
  peer
end

#feed_data(dst = plug_peer) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/rbkb/plug/plug.rb', line 200

def feed_data(dst = plug_peer)
  unless dat = @feed[@pos]
    UI.log '** FEED EXHAUSTED' unless @squelch_exhausted
    return nil
  end

  dst.say dat.to_s, self

  close_connection_after_writing if (@pos += 1) >= @feed.size and @close_at_end
end

#go ⇒ Object



175
176
177
178
179
180
# File 'lib/rbkb/plug/plug.rb', line 175

def go
  return unless @go_first

  feed_data
  @go_first = false
end

#initialize(*args) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/rbkb/plug/plug.rb', line 166

def initialize(*args)
  super(*args)

  @pos ||= 0
  @feed ||= []

  raise 'feed must be enumerable' unless @feed.is_a?(Enumerable)
end

#say(dat, sender) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rbkb/plug/plug.rb', line 188

def say(dat, sender)
  super(dat, sender)
  if @step
    EventMachine.defer(
      proc { UI.prompt ">> Hit [enter] to continue at #{@pos}:" },
      proc { |_x| feed_data }
    )
  else
    feed_data
  end
end