Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mattetti/mrstuff
MacRuby experimental wrappers
https://github.com/mattetti/mrstuff
Last synced: about 12 hours ago
JSON representation
MacRuby experimental wrappers
- Host: GitHub
- URL: https://github.com/mattetti/mrstuff
- Owner: mattetti
- Created: 2009-12-06T02:14:24.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2009-12-07T09:39:02.000Z (about 15 years ago)
- Last Synced: 2024-11-27T12:46:22.454Z (2 months ago)
- Language: Ruby
- Homepage:
- Size: 125 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
The MrStuff package provides wrappers for Cocoa APIs that are more familiar to Rubyists.
So far, there is a wrapper for `NSTask` (`MrTask`), and associated wrappers for `NSNotificationCenter`,
`NSTask`, and `NSFileHandle`.MrNotificationCenter integrates with other Cocoa wrappers as follows:
- Wrappers have an `#ns_object` method which returns the original `NSObject`
- A `NOTIFICATIONS` constant is provided, which provides shorter `Symbol` names
for each notification provided by the original Cocoa class.For instance, you can do the following:
task = MrTask.new("/usr/bin/ls")
MrNotificationCenter.subscribe(task, :done) do |notification|
# notification.object is the MrTask
# notification.object.ns_object is the wrapped NSTask
endHowever, the wrapper provides improved async APIs. For instance:
task = MrTask.new("/usr/bin/ls")
task.on_output do |output, notification|
# When the task gets data in its stdout, this event
# is triggered. A String is provided, rather than
# forcing you to extract the data from notification.userInfo
# and initializing a new String from the data.
#
# Note that the block arguments are optional
end
task.launch("/")The above example could be reduced even further to:
MrTask.launch("/usr/bin/ls", "/") do |out, err, notification|
# This block is triggered once the task is terminated,
# and provides a String for both the standard output and
# error.
#
# The block arguments are optional
endOther events are also available as needed:
task = MrTask.new("/usr/bin/ls")
task.on_done do |notification|
# This event is triggered once the task is done.
#
# You can call notification.object.standard_output
# or notification.object.error_output to get a
# String for the outputs.
endAs you can see, the APIs are fairly flexible and allow you to approach the
same problem in different (asynchronous) ways. At the same time, common
tasks are wrapped up with appropriate async callbacks.task = MrTask.launch("/usr/bin/ruby", "-e", "'sleep'")
task.kill do |notification|
# This will be the :done event
end