diff --git a/samples/middleware.cr b/samples/middleware.cr index 4f6cfb1..86b6c28 100644 --- a/samples/middleware.cr +++ b/samples/middleware.cr @@ -17,8 +17,8 @@ chat = Chat.new chat.send_message.add { |body, yielder| yielder.call(body.upcase) } # Prepend our user name -chat.send_message.add { |body, yielder| yielder.call("Alice: #{body}")} +chat.send_message.add { |body, yielder| yielder.call("Alice: #{body}") } # Send a message -chat.send_message.call("Hello") #=> 13 +chat.send_message.call("Hello") # => 13 # Prints "Sending Alice: HELLO" diff --git a/samples/real_world.cr b/samples/real_world.cr index cb1e085..c820802 100644 --- a/samples/real_world.cr +++ b/samples/real_world.cr @@ -43,7 +43,7 @@ class DataSource spawn do loop do changed.emit rand(VALUE_RANGE) # Result of a complex calculation - sleep rand(SLEEP_RANGE) # Next value will take a bit + sleep rand(SLEEP_RANGE) # Next value will take a bit end end end @@ -53,7 +53,7 @@ end class DataApplication DISPLAYS = 5 - @displays = [ ] of DataDisplay + @displays = [] of DataDisplay def initialize # We're using an interval sink, meaning, we want to get notified if anything @@ -61,19 +61,19 @@ class DataApplication @sink = Cute::IntervalSink(Nil).new(interval: 1.seconds) # If something happened, then redraw the application - @sink.on{|hits| redraw hits.size} + @sink.on { |hits| redraw hits.size } # Create something that can change - DISPLAYS.times{ @displays << create_display } + DISPLAYS.times { @displays << create_display } redraw # Don't forget to draw something initially! end private def create_display - source = DataSource.new # We have a source + source = DataSource.new # We have a source display = DataDisplay.new # And something to show incoming data # If the source gets new data, supply it to the display - source.changed.on{|value| display.data = value} + source.changed.on { |value| display.data = value } # If the display changes somehow, schedule a redraw display.changed.on(@sink) diff --git a/samples/sink_synchronisation.cr b/samples/sink_synchronisation.cr index f6963ba..785e263 100644 --- a/samples/sink_synchronisation.cr +++ b/samples/sink_synchronisation.cr @@ -23,7 +23,7 @@ sink = Cute::BufferSink(Int32).new(size: 5) 10.times do |index| # Create ten jobs thing = SomethingComplex.new(index) thing.finished.on(sink) # Connect the signal to our sink - thing.run # Run the complex calculation! + thing.run # Run the complex calculation! end # Ten jobs, at batches of five, so wait two times. diff --git a/samples/testing.cr b/samples/testing.cr index 5a48265..24c7d26 100644 --- a/samples/testing.cr +++ b/samples/testing.cr @@ -24,7 +24,7 @@ describe Button do # The signal spy inherits from an array, so verification is easy clicked_spy.size.should eq 1 - clicked_spy.first.should eq({ 4, 5 }) + clicked_spy.first.should eq({4, 5}) end end end diff --git a/samples/wait.cr b/samples/wait.cr index bee5032..35620f4 100644 --- a/samples/wait.cr +++ b/samples/wait.cr @@ -7,7 +7,7 @@ class Button end btn = Button.new -spawn{ btn.clicked.emit 5, 4 } +spawn { btn.clicked.emit 5, 4 } puts "Waiting for a click" x, y = btn.clicked.wait diff --git a/spec/buffer_sink_spec.cr b/spec/buffer_sink_spec.cr index 2640887..cc905af 100644 --- a/spec/buffer_sink_spec.cr +++ b/spec/buffer_sink_spec.cr @@ -7,24 +7,24 @@ describe Cute::BufferSink do emits = [] of Array(Int32) empty = [] of Array(Int32) - subject.on{|x| emits << x} + subject.on { |x| emits << x } subject.notify 1 subject.notify 2 subject.notify 3 Fiber.yield - emits.should eq [ [ 1, 2, 3 ] ] + emits.should eq [[1, 2, 3]] - (4..8).each{|i| subject.notify i} + (4..8).each { |i| subject.notify i } Fiber.yield - emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + emits.should eq [[1, 2, 3], [4, 5, 6]] subject.notify 9 Fiber.yield - emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] + emits.should eq [[1, 2, 3], [4, 5, 6], [7, 8, 9]] end end end diff --git a/spec/connect_spec.cr b/spec/connect_spec.cr index 7df71bb..8dc8fc8 100644 --- a/spec/connect_spec.cr +++ b/spec/connect_spec.cr @@ -19,7 +19,7 @@ private class Listener end def on_move(x, y) - @move_data = { x, y } + @move_data = {x, y} end def on_one(msg) @@ -53,6 +53,6 @@ describe "Cute.connect" do subject = Listener.new(widget) widget.moved.emit(5, 6) - subject.move_data.should eq({ 5, 6 }) + subject.move_data.should eq({5, 6}) end end diff --git a/spec/interval_sink_spec.cr b/spec/interval_sink_spec.cr index 4b09d15..b958e96 100644 --- a/spec/interval_sink_spec.cr +++ b/spec/interval_sink_spec.cr @@ -7,28 +7,28 @@ describe Cute::IntervalSink do emits = [] of Array(Int32) empty = [] of Array(Int32) - subject.on{|x| emits << x} + subject.on { |x| emits << x } subject.notify 1 subject.notify 2 subject.notify 3 sleep 20.milliseconds - emits.should eq [ [ 1, 2, 3 ] ] + emits.should eq [[1, 2, 3]] - (4..8).each{|i| subject.notify i} + (4..8).each { |i| subject.notify i } sleep 20.milliseconds - emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6, 7, 8 ] ] + emits.should eq [[1, 2, 3], [4, 5, 6, 7, 8]] subject.notify 9 sleep 20.milliseconds - emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6, 7, 8 ], [ 9 ] ] + emits.should eq [[1, 2, 3], [4, 5, 6, 7, 8], [9]] # Emits nothing if no notifications are received sleep 20.milliseconds - emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6, 7, 8 ], [ 9 ] ] + emits.should eq [[1, 2, 3], [4, 5, 6, 7, 8], [9]] end end end diff --git a/spec/middleware_spec.cr b/spec/middleware_spec.cr index 0f88159..01ffeb0 100644 --- a/spec/middleware_spec.cr +++ b/spec/middleware_spec.cr @@ -1,7 +1,7 @@ require "./spec_helper" private class Chat - getter sent = [ ] of String + getter sent = [] of String Cute.middleware def send_message(body : String) : Int32 @sent << body @@ -17,7 +17,7 @@ describe "Cute.middleware" do describe "without arguments" do it "works fine" do chat = Chat.new - chat.ping.add{|y| y.call * 2} + chat.ping.add { |y| y.call * 2 } chat.ping.call.should eq 123 * 2 end end @@ -27,34 +27,34 @@ describe "Cute.middleware" do it "calls the method" do chat = Chat.new chat.send_message.call("Hello").should eq 5 - chat.sent.should eq [ "Hello" ] + chat.sent.should eq ["Hello"] end end context "if one middleware was added" do it "calls the middleware, then the method" do chat = Chat.new - chat.send_message.add{|m, y| y.call m.upcase} + chat.send_message.add { |m, y| y.call m.upcase } chat.send_message.call("Hello").should eq 5 - chat.sent.should eq [ "HELLO" ] + chat.sent.should eq ["HELLO"] end end context "if multiple middlewares are added" do it "calls the middlewares in order, then the method" do chat = Chat.new - chat.send_message.add{|m, y| y.call m.upcase} - chat.send_message.add{|m, y| y.call "saying #{m}"} + chat.send_message.add { |m, y| y.call m.upcase } + chat.send_message.add { |m, y| y.call "saying #{m}" } chat.send_message.call("Hello").should eq "saying HELLO".size - chat.sent.should eq [ "saying HELLO" ] + chat.sent.should eq ["saying HELLO"] end end context "if a middleware doesn't call onwards" do it "the next stage is not called" do chat = Chat.new - chat.send_message.add{|m, y| -1} - chat.send_message.add{|m, y| y.call m.upcase} + chat.send_message.add { |m, y| -1 } + chat.send_message.add { |m, y| y.call m.upcase } chat.send_message.call("Hello").should eq -1 chat.sent.empty?.should eq true end diff --git a/spec/signal_spec.cr b/spec/signal_spec.cr index 5a98105..4a235e9 100644 --- a/spec/signal_spec.cr +++ b/spec/signal_spec.cr @@ -50,13 +50,13 @@ describe "Cute.signal" do subject = Widget.new calls = Array(Int32).new - subject.closed.on{ calls << 1 } - subject.closed.on{ calls << 2 } - subject.closed.on{ calls << 3 } + subject.closed.on { calls << 1 } + subject.closed.on { calls << 2 } + subject.closed.on { calls << 3 } calls.size.should eq 0 subject.closed.emit - calls.should eq [ 1, 2, 3 ] + calls.should eq [1, 2, 3] end context "with async: true" do @@ -64,8 +64,8 @@ describe "Cute.signal" do subject = Widget.new fibers = Array(Fiber).new - subject.asynced.on{ fibers << Fiber.current } - subject.asynced.on{ fibers << Fiber.current; Fiber.yield } + subject.asynced.on { fibers << Fiber.current } + subject.asynced.on { fibers << Fiber.current; Fiber.yield } subject.asynced.emit Fiber.yield # Call slot fiber @@ -86,7 +86,7 @@ describe "Cute.signal" do widget.one.on(sink) widget.one.emit "Foo" - sink.collected.should eq [ nil ] + sink.collected.should eq [nil] end end @@ -98,7 +98,7 @@ describe "Cute.signal" do widget.moved.on(sink) widget.moved.emit 4, 5 - sink.collected.should eq [ nil ] + sink.collected.should eq [nil] end end @@ -110,7 +110,7 @@ describe "Cute.signal" do widget.closed.on(sink) widget.closed.emit - sink.collected.should eq [ nil ] + sink.collected.should eq [nil] end end @@ -123,7 +123,7 @@ describe "Cute.signal" do widget.one.emit "Foo" widget.one.emit "Bar" - sink.collected.should eq [ "Foo", "Bar" ] + sink.collected.should eq ["Foo", "Bar"] end end @@ -136,7 +136,7 @@ describe "Cute.signal" do widget.moved.emit 4, 5 widget.moved.emit 6, 7 - sink.collected.should eq [ { 4, 5 }, { 6, 7 } ] + sink.collected.should eq [{4, 5}, {6, 7}] end end end @@ -146,14 +146,14 @@ describe "Cute.signal" do subject = Widget.new calls = Array(Int32).new - subject.closed.on{ calls << 1 } - handler = subject.closed.on{ calls << 2 } - subject.closed.on{ calls << 3 } + subject.closed.on { calls << 1 } + handler = subject.closed.on { calls << 2 } + subject.closed.on { calls << 3 } calls.size.should eq 0 subject.closed.disconnect(handler) subject.closed.emit - calls.should eq [ 1, 3 ] + calls.should eq [1, 3] end end @@ -162,8 +162,8 @@ describe "Cute.signal" do subject = Widget.new calls = Array(Int32).new - subject.closed.on{ calls << 1 } - subject.closed.on{ calls << 2 } + subject.closed.on { calls << 1 } + subject.closed.on { calls << 2 } calls.empty?.should be_true subject.closed.disconnect @@ -177,7 +177,7 @@ describe "Cute.signal" do subject = Widget.new ch, handler = subject.one.new_channel - fut = future{ ch.receive } + fut = future { ch.receive } subject.one.emit "Okay" handler.should_not be_nil @@ -189,19 +189,19 @@ describe "Cute.signal" do subject = Widget.new ch, handler = subject.moved.new_channel - fut = future{ ch.receive } + fut = future { ch.receive } subject.moved.emit 4, 5 fut.completed?.should be_true handler.should_not be_nil - fut.get.should eq({ 4, 5 }) + fut.get.should eq({4, 5}) end context "with no signal arguments" do subject = Widget.new ch, handler = subject.closed.new_channel - fut = future{ ch.receive } + fut = future { ch.receive } subject.closed.emit handler.should_not be_nil @@ -213,7 +213,7 @@ describe "Cute.signal" do context "with one signal argument" do subject = Widget.new - spawn{ subject.one.emit "Okay" } + spawn { subject.one.emit "Okay" } result = subject.one.wait result.should eq "Okay" end @@ -221,15 +221,15 @@ describe "Cute.signal" do context "with many signal arguments" do subject = Widget.new - spawn{ subject.moved.emit 4, 5 } + spawn { subject.moved.emit 4, 5 } result = subject.moved.wait - result.should eq({ 4, 5 }) + result.should eq({4, 5}) end context "with no signal arguments" do subject = Widget.new - spawn{ subject.closed.emit } + spawn { subject.closed.emit } subject.closed.wait end end diff --git a/spec/spec_spec.cr b/spec/spec_spec.cr index 74a4a09..6dbf28e 100644 --- a/spec/spec_spec.cr +++ b/spec/spec_spec.cr @@ -21,7 +21,7 @@ describe Cute::SignalSpy do thing.none.emit spy.size.should eq 2 - spy.should eq [ nil, nil ] + spy.should eq [nil, nil] end end @@ -38,7 +38,7 @@ describe Cute::SignalSpy do thing.one.emit "Second" spy.size.should eq 2 - spy.should eq [ "First", "Second" ] + spy.should eq ["First", "Second"] end end @@ -55,7 +55,7 @@ describe Cute::SignalSpy do thing.many.emit false, 2 spy.size.should eq 2 - spy.should eq [ { true, 1 }, { false, 2 } ] + spy.should eq [{true, 1}, {false, 2}] end end end diff --git a/spec/timed_buffer_sink_spec.cr b/spec/timed_buffer_sink_spec.cr index 90440a0..e83e2d0 100644 --- a/spec/timed_buffer_sink_spec.cr +++ b/spec/timed_buffer_sink_spec.cr @@ -7,19 +7,19 @@ describe Cute::TimedBufferSink do emits = [] of Array(Int32) empty = [] of Array(Int32) - subject.on{|x| emits << x} + subject.on { |x| emits << x } # Both buffer and time constraints - (1..5).each{|i| subject.notify i} + (1..5).each { |i| subject.notify i } sleep 20.milliseconds - emits.should eq [ [ 1, 2, 3 ], [ 4, 5 ] ] + emits.should eq [[1, 2, 3], [4, 5]] # Time constraint - (6..7).each{|i| subject.notify i} + (6..7).each { |i| subject.notify i } sleep 20.milliseconds - emits.should eq [ [ 1, 2, 3 ], [ 4, 5 ], [ 6, 7 ] ] + emits.should eq [[1, 2, 3], [4, 5], [6, 7]] # Size constraint subject.notify 8 @@ -30,11 +30,11 @@ describe Cute::TimedBufferSink do Fiber.yield # We're not hitting the time constraint, right? (start - Time.utc).should be < 10.milliseconds - emits.should eq [ [ 1, 2, 3 ], [ 4, 5 ], [ 6, 7 ], [ 8, 9, 0 ] ] + emits.should eq [[1, 2, 3], [4, 5], [6, 7], [8, 9, 0]] # Does nothing if no notifications are received sleep 20.milliseconds - emits.should eq [ [ 1, 2, 3 ], [ 4, 5 ], [ 6, 7 ], [ 8, 9, 0 ] ] + emits.should eq [[1, 2, 3], [4, 5], [6, 7], [8, 9, 0]] end end end diff --git a/src/cute.cr b/src/cute.cr index 686fb00..1edb633 100644 --- a/src/cute.cr +++ b/src/cute.cr @@ -34,7 +34,7 @@ module Cute # # btn = Button.new # btn.clicked.on { |x, y| p x, y } - # btn.clicked.emit 5, 4 #=> Will print 5, 4 + # btn.clicked.emit 5, 4 # => Will print 5, 4 # ``` # # **Note:** You have to fully qualify all argument types to `Cute.signal`. @@ -43,7 +43,7 @@ module Cute # ``` # class MyIO # Cute.signal data_received() # With empty parantheses - # Cute.signal closed # Or without. Both are fine. + # Cute.signal closed # Or without. Both are fine. # end # ``` # @@ -61,8 +61,8 @@ module Cute # ``` # btn = Button.new # ch, handle = btn.clicked.new_channel # Create channel - # x, y = ch.retrieve # Wait for event - # btn.clicked.disconnect(handle) # Remove channel + # x, y = ch.retrieve # Wait for event + # btn.clicked.disconnect(handle) # Remove channel # ``` # # The channel will transport the signal argument, or will use a `Tuple` if the @@ -146,8 +146,8 @@ module Cute {% if handler.args.empty? %} {{ signal }}.on{ {{ handler.name }} } {% else %} - {{ signal }}.on do |{{ handler.args.map{|c| c.is_a?(Call) ? c : c.var }.splat }}| - {{ handler.name }}({{ handler.args.map{|c| c.is_a?(Call) ? c : c.var }.splat }}) + {{ signal }}.on do |{{ handler.args.map { |c| c.is_a?(Call) ? c : c.var }.splat }}| + {{ handler.name }}({{ handler.args.map { |c| c.is_a?(Call) ? c : c.var }.splat }}) end {% end %} end @@ -178,7 +178,7 @@ module Cute # chat = Chat.new # chat.send_message.add { |body, yielder| yielder.call body.upcase } # - # chat.send_message.call("Hello") #=> "Sent 5 bytes" + # chat.send_message.call("Hello") # => "Sent 5 bytes" # # Prints "Sending HELLO" # ``` # diff --git a/src/cute/buffer_sink.cr b/src/cute/buffer_sink.cr index c313431..292c86e 100644 --- a/src/cute/buffer_sink.cr +++ b/src/cute/buffer_sink.cr @@ -4,7 +4,6 @@ module Cute # # See `Sink` for documentation on the usage. class BufferSink(T) < Sink(T) - # Size of the buffer property size : Int32 diff --git a/src/cute/interval_sink.cr b/src/cute/interval_sink.cr index 055f26d..439955e 100644 --- a/src/cute/interval_sink.cr +++ b/src/cute/interval_sink.cr @@ -4,7 +4,6 @@ module Cute # # See `Sink` for documentation on the usage. class IntervalSink(T) < Sink(T) - # Check interval property interval : Time::Span diff --git a/src/cute/signal.cr b/src/cute/signal.cr index edfd4a9..b52988d 100644 --- a/src/cute/signal.cr +++ b/src/cute/signal.cr @@ -8,7 +8,7 @@ module Cute # Removes the handler with the handle *handler_hash*. def disconnect(handler_hash) - @listeners.reject!{|handler| handler.hash == handler_hash} + @listeners.reject! { |handler| handler.hash == handler_hash } end # Removes all handlers @@ -36,11 +36,11 @@ module Cute {% handler_type = "Proc(Nil)" %} {% channel_type = "Channel(Nil)" %} {% else %} - {% handler_type = "Proc(#{ call.args.map(&.type).splat }, Nil)" %} + {% handler_type = "Proc(#{call.args.map(&.type).splat}, Nil)" %} {% if call.args.size == 1 %} - {% channel_type = "Channel(#{ call.args[0].type })" %} + {% channel_type = "Channel(#{call.args[0].type})" %} {% else %} - {% channel_type = "Channel(Tuple(#{ call.args.map(&.type).splat }))" %} + {% channel_type = "Channel(Tuple(#{call.args.map(&.type).splat}))" %} {% end %} {% end %} diff --git a/src/cute/sink.cr b/src/cute/sink.cr index be16a1e..6c13c34 100644 --- a/src/cute/sink.cr +++ b/src/cute/sink.cr @@ -12,7 +12,7 @@ module Cute # btn = Button.new # sink = Cute::BufferSink(Nil).new(size: 2) # btn.clicked.on(sink) - # sink.on{ puts "Clicked twice!" } + # sink.on { puts "Clicked twice!" } # ``` # # The generic argument to the class is what the signal emits as type. The @@ -24,7 +24,7 @@ module Cute # btn = Button.new # sink = Cute::BufferSink(Tuple(Int32, Int32)).new(size: 2) # btn.clicked.on(sink) - # sink.on{|collected| puts "Clicked twice, positions: #{collected}" } + # sink.on { |collected| puts "Clicked twice, positions: #{collected}" } # ``` # # **Note**: When you're using a sink to collect from a signal passing multiple @@ -49,8 +49,8 @@ module Cute # sink = Cute::BufferSink(HttpResponse).new(size: urls.size) # Create a sink # urls.each do |url| # down = AsynchronousDownloader.new(url) - # down.progress.on{|percent| show_progress(down, percent)} # Update the UI - # down.finished.on(sink) # Once finished, notify the sink + # down.progress.on { |percent| show_progress(down, percent) } # Update the UI + # down.finished.on(sink) # Once finished, notify the sink # end # sink # And return the sink # end @@ -61,7 +61,7 @@ module Cute # # ``` # sink = download_all(the_urls) # Start the asynchronous download - # results = sink.wait # Use Signal#wait to wait + # results = sink.wait # Use Signal#wait to wait # # results will be a `Array(HttpResponse)` # pp results # Do something with the results. # ``` @@ -78,9 +78,9 @@ module Cute # For this, you can use `Sink#notify`: # ``` # sink = Cute::BufferSink(Int32).new(size: 10) # Set it up - # sink.on{|data| puts "Calculation results: #{data}"} + # sink.on { |data| puts "Calculation results: #{data}" } # - # 10.times{ sink.notify rand(1..100) } # Calculate and provide data + # 10.times { sink.notify rand(1..100) } # Calculate and provide data # ``` # # ### Creating a custom sink @@ -96,7 +96,7 @@ module Cute def initialize super() @listeners = Array(Proc(Array(T), Nil)).new - @collected = [ ] of T + @collected = [] of T end def notify(data : T) @@ -106,7 +106,7 @@ module Cute protected def emit_now list = @collected - @collected = [ ] of T + @collected = [] of T emit list end diff --git a/src/cute/timed_buffer_sink.cr b/src/cute/timed_buffer_sink.cr index 2861338..0725932 100644 --- a/src/cute/timed_buffer_sink.cr +++ b/src/cute/timed_buffer_sink.cr @@ -4,7 +4,6 @@ module Cute # # See `Sink` for documentation on the usage. class TimedBufferSink(T) < IntervalSink(T) - # Size of the buffer property size : Int32 diff --git a/src/spec.cr b/src/spec.cr index 51a9053..466694d 100644 --- a/src/spec.cr +++ b/src/spec.cr @@ -18,7 +18,10 @@ module Cute # # class Button # Cute.signal clicked(x : Int32, y : Int32) - # def click; clicked.emit(4, 5); end + # + # def click + # clicked.emit(4, 5) + # end # end # # describe Button do @@ -26,9 +29,9 @@ module Cute # it "emits #clicked" do # btn = Button.new # clicked_spy = Cute.spy btn, clicked(x : Int32, y : Int32) # Create the spy - # btn.click # Somehow emit the spied upon signal - # clicked_spy.size.should eq 1 # Verify - # clicked_spy[0].should eq({ 4, 5 }) + # btn.click # Somehow emit the spied upon signal + # clicked_spy.size.should eq 1 # Verify + # clicked_spy[0].should eq({4, 5}) # end # end # end