https://github.com/roxasshadow/alakazam
Provides methods to observe all your things
https://github.com/roxasshadow/alakazam
Last synced: 12 months ago
JSON representation
Provides methods to observe all your things
- Host: GitHub
- URL: https://github.com/roxasshadow/alakazam
- Owner: RoxasShadow
- Created: 2014-02-01T12:36:43.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-02-07T22:46:13.000Z (over 12 years ago)
- Last Synced: 2025-04-02T11:49:38.544Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 195 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Alakazam
=========
What is
-------
Alakazam provides methods to observe all your things.
Install
-------
`$ gem install alakazam`
Examples
-------
Invoke a `block` on `#fire!` (observing an instance methods).
```ruby
require 'alakazam'
class Shiftry
include Alakazam
def lol
fire!
notify 'fired'
end
end
shiftry = Shiftry.new
shiftry.is_observed_by { |*things|
p things
}
shiftry.lol
```
Invoke a `block` on `#fire!` (observing a class methods).
```ruby
require 'alakazam'
class Shiftry
extend Alakazam
def self.lol
fire!
notify 'fired'
end
end
Shiftry.is_observed_by(nil, on_change: false) { |*things|
p things
}
Shiftry.lol
```
Invoke a `Proc` on `#fire!` (observing an instance methods).
```ruby
require 'alakazam'
class Shiftry
include Alakazam
def lol
fire!
notify 'fired'
end
end
logger = ->(*things) { p things }
shiftry = Shiftry.new
shiftry.is_observed_by logger
shiftry.lol
```
Invoke a `Proc` on `#fire!` (observing a class methods).
```ruby
require 'alakazam'
class Shiftry
extend Alakazam
def self.lol
fire!
notify 'fired'
end
end
logger = ->(*things) { p things }
Shiftry.is_observed_by logger, on_change: false
Shiftry.lol
```
Invoke a `Proc` without using `#fire!`.
```ruby
require 'alakazam'
class Shiftry
include Alakazam
def lol
notify 'fired'
end
end
logger = ->(*things) { p things }
shiftry = Shiftry.new
shiftry.is_observed_by logger, on_change: false
shiftry.lol
```
Invoke both the default (`#update`) and a custom method of the observer class.
```ruby
require 'alakazam'
class Shiftry
include Alakazam
def lol
fire!
notify 'fired'
end
end
class Logger
def update(*things)
p things
end
def self.on_fire(*things)
p things
end
end
shiftry = Shiftry.new
shiftry.is_observed_by Logger.new, methods: [ :on_fire ]
shiftry.is_observed_by Logger.new
shiftry.lol
```
Invoke `#update` when a variable changes in the observed class.
```ruby
require 'alakazam'
class Shiftry
include Alakazam
attr_accessor :lal
def lol
notify 'fired'
end
end
class Logger
def update(*things)
p things
end
def on_fire(*things)
p things
end
end
shiftry = Shiftry.new
shiftry.is_observed_by Logger.new, attributes: { var: :lal, notify: 'fired' }
shiftry.lal = 3
```