Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from terceiro/ruby-style
Add Ruby-style method to GObject objects
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module GObject | ||
|
||
module RubyStyle | ||
|
||
def method_missing(method, *args) | ||
if respond_to?("get_#{method}") | ||
return send("get_#{method}", *args) | ||
end | ||
if method.to_s =~ /(.*)=$/ && respond_to?("set_#{$1}") | ||
return send("set_#{$1}", *args) | ||
end | ||
super | ||
end | ||
|
||
def signal_connect(event, &block) | ||
GObject.signal_connect(self, event, &block) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require File.expand_path('../gir_ffi_test_helper.rb', File.dirname(__FILE__)) | ||
|
||
require 'ffi-gobject/ruby_style' | ||
|
||
describe GObject::RubyStyle do | ||
class RubyStyleTest | ||
include GObject::RubyStyle | ||
def get_x | ||
@x | ||
end | ||
def set_x(val) | ||
@x = val | ||
end | ||
end | ||
|
||
subject { RubyStyleTest.new } | ||
|
||
it 'reads x by calling get_x' do | ||
subject.set_x(1) | ||
assert_equal 1, subject.x | ||
end | ||
|
||
it 'writes x by calling set_x' do | ||
subject.x = 2 | ||
assert_equal 2, subject.x | ||
end | ||
|
||
it 'delegates signal_connect to GObject' do | ||
block = lambda {} | ||
mock(GObject).signal_connect(subject, 'some-event') | ||
subject.signal_connect('some-event') do | ||
nothing | ||
end | ||
|
||
RR.verify | ||
end | ||
|
||
end | ||
|