0.0
No commit activity in last 3 years
No release in over 3 years
simplest possible Ruby listener lib
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.6
>= 0
 Project Readme

Build Status Code Climate Gittens Open Thanks

SimpleListener

Simplest most light-weight possible implementation of Listener / Observer.

Installation

Add this line to your application's Gemfile:

gem 'simple_listener'

And then execute:

$ bundle

Plain Ruby usage:

class Book
  include SimpleListener
  
  attr_accessor :author_name

  def save
    call_listeners(:before_create)
    # ... + do some database stuff
  end
end

class AssignAuthorListener
  attr_reader :user
  
  def initialize(user)
    @user = user
  end

  def before_create(resource)
    resource.author_name = user.name
  end
end

require 'ostruct'
class DummyController
  def create
    my_listener = AssignAuthorListener.new(current_user)
    
    @book = Book.new
    @book.add_listener(my_listener)
    @book.save

    # @book.author_name # => Tomas
  end

  def current_user
    OpenStruct.new(name: "Tomas")
  end
end

Ruby on Rails usage

since Rails 3.something you have the option to do around_save.

class Book < ActiveRecord::Base
  include SimpleListener

  around_save :notify_listeners

  def notify_listeners
    is_create_save   = !persisted?
    if is_create_save
      call_listeners(:before_create)
    else
      call_listeners(:before_update)
    end

    yield # will do the actual saving to DB ...call :save

    if is_create_save
      call_listeners(:on_create)
    else
      call_listeners(:on_update)
    end
  end
end

More examples:

Credit

Idea for this gem is based on pattern shown in Avdi Grimm Ruby Tapas episode #031 Observer Variations

All hail Avdi !!!

Contributing

  1. Fork it ( https://github.com/equivalent/simple_listener/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request