Skip to content

Commit

Permalink
Merge pull request #26 from terceiro/ruby-style
Browse files Browse the repository at this point in the history
Add Ruby-style method to GObject objects
  • Loading branch information
mvz committed Mar 18, 2012
2 parents 0581c0a + c55eef1 commit 8f58ae4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/ffi-gobject/object.rb
@@ -1,8 +1,13 @@
require 'ffi-gobject/ruby_style'

module GObject
load_class :Object

# Overrides for GObject, GObject's generic base class.
class Object

include RubyStyle

_setup_method "new"
_setup_instance_method "get_property"
_setup_instance_method "set_property"
Expand Down
21 changes: 21 additions & 0 deletions lib/ffi-gobject/ruby_style.rb
@@ -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
4 changes: 4 additions & 0 deletions test/ffi-gobject/object_test.rb
Expand Up @@ -8,5 +8,9 @@
assert_equal 1,
GObject::Object.instance_method("get_property").arity
end

it 'includes GObject::RubyStyle' do
assert GObject::Object.included_modules.include?(GObject::RubyStyle)
end
end
end
39 changes: 39 additions & 0 deletions test/ffi-gobject/ruby_style.rb
@@ -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

0 comments on commit 8f58ae4

Please sign in to comment.