0.0
No commit activity in last 3 years
No release in over 3 years
Enables an object to define an event method which to becomes a publisher/observable. Subscribers/observers can to be defined dynamically via a block given to the event.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.14
~> 10.0
~> 3.0
~> 0.48.1

Runtime

~> 1.0.0
 Project Readme

ObserveEvent

Enables an object to define an event method which to becomes a publisher/observable. Subscribers/observers can to be defined dynamically via a block given to the event.

Install

Bundler: gem "observe_event"

RubyGems: gem install observe_event

Usage

# frozen_string_literal: true

require 'observe_event'

# A simple "timer" class
class Timer
  extend ObserveEvent

  event :tick             # Define publisher/observable `tick`
  event_accessor :running # Define publisher/observable `running_changed`, which notifies it's subscribers/observers

  def state
    @running ? :running : :stopped
  end
end

timer = Timer.new

# Define subscriber to `running_changed` on the Timer instance
timer.running_changed do |from, to|
  puts "Timer#running changed from `#{from.inspect}` to `#{to.inspect}`"
  puts "Timer #{timer.state}"
end

# Define subscribers to `tick` on the Timer instance
timer.tick do
  puts 'Tick' if timer.running
end

timer.tick do
  puts 'Tock' if timer.running
end

timer.tick
timer.tick
timer.running = true
timer.tick
timer.tick
timer.running = false
timer.tick
timer.tick

# => Timer#running changed from `nil` to `true`
# => Timer running
# => Tick
# => Tock
# => Tick
# => Tock
# => Timer#running changed from `true` to `false`
# => Timer stopped

See the examples/ directory for more examples.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake to run the tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/RyanScottLewis/observe_event. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.