krisleech/wisper — pull request #49 by jhoffner (2014-05-02 17:35:28+00:00)
Title: class prefix

Added a class prefix option that allows global (or temporary) listeners to easily be namespaced, without having to define it within each event broadcast.

So for example:

```ruby
class Project
include Wisper::Publisher

def create!
broadcast(:created, self)
end

def complete!
broadcast(:completed, self)
end
end

class NotificationsListener
def project_created(project)
end

def project_completed(project)
end

def project_task_completed(project_item)
end

def project_user_added
end

def user_created
end
end

Wisper.with_listeners(NotificationsListener, class_prefix: true) do
User.create!
project = Project.create!
project.complete!
end
```

Some advantages of dynamically prefixing the event names instead of just defining the fully qualified events manually are:

- You can define common functionality in a base class. For example a commit method. I believe I saw an example of this on a blog article you wrote.
- You can optionally choose to handle the event as a class specific event or not. For example maybe you just want to listen to any :created event and fire off a generic log message, now you don't need to define a handlers for each object that can be created. However you may also want to target a specific created event (like project_completed), you have the flexibility to do both.

Additionally, there is the ability to customize what the class prefix should be, on a class level. For example:

```ruby
class Project
include Wisper::Publisher

def create!
broadcast(:created, self)
end

def complete!
broadcast(:completed, self)
end

def publisher_class_prefix
:project
end
end

# events broadcasted out of this class will be prefixed with project_, not user_project_.
# If we had not defined the publisher_class_prefix class then this wouldn't be the case.
class UserProject < Project
attr_accessor :user
end
```