Skip to content
Permalink
Browse files
Adding #drop support.
Pretty straightforward.
  • Loading branch information
steveklabnik committed Apr 28, 2013
1 parent a678f7f commit c844efb73b40353f9c9362bcffa86e1d28ff45e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
@@ -12,6 +12,7 @@ class Stream
require 'frappuccino/stream/map'
require 'frappuccino/stream/select'
require 'frappuccino/stream/zip'
require 'frappuccino/stream/drop'

def not_implemented(m, message)
define_method m do |*args, &blk|
@@ -60,6 +61,10 @@ def map(&blk)
end
alias :collect :map

def drop(n)
Drop.new(self, n)
end

def map_stream(hsh)
Map.new(self) do |event|
if hsh.has_key?(event)
@@ -0,0 +1,19 @@
module Frappuccino
class Drop < Stream
def initialize(source, n)
source.add_observer(self)

@count = 0
@dropped = 0
@n = n
end

def update(event)
@dropped += 1

if @dropped > @n
occur(event)
end
end
end
end
@@ -0,0 +1,14 @@
require 'test_helper'

describe "drop" do
it "ignores the first n events" do
button = Button.new

stream = Frappuccino::Stream.new(button)
dropped_stream = stream.drop(3)

5.times { button.push }

assert_equal 2, dropped_stream.count
end
end

0 comments on commit c844efb

Please sign in to comment.