Skip to content

Commit

Permalink
Add #native_reader, #native_writer and #native_accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Mar 17, 2014
1 parent 8ae7056 commit d9989fc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/opal/stdlib/native/native_reader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'native'

describe "Native.native_reader" do
it "refers to an attribute on @native" do
Class.new {
include Native

native_reader :a
}.new(`{ a: 2 }`).a.should == 2
end

it "uses multiple names" do
n = Class.new {
include Native

native_reader :a, :b
}.new(`{ a: 2, b: 3 }`)

n.a.should == 2
n.b.should == 3
end
end
30 changes: 30 additions & 0 deletions spec/opal/stdlib/native/native_writer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'native'

describe "Native.native_writer" do
it "refers to an attribute on @native" do
n = Class.new {
include Native

native_reader :a
native_writer :a
}.new(`{ a: 2 }`)

n.a = 4
n.a.should == 4
end

it "supports multiple names" do
n = Class.new {
include Native

native_reader :a, :b
native_writer :a, :b
}.new(`{ a: 2, b: 3 }`)

n.a = 4
n.b = 5

n.a.should == 4
n.b.should == 5
end
end
21 changes: 21 additions & 0 deletions stdlib/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ def alias_native(new, old = new, options = {})
end
end
end

def native_reader(*names)
names.each {|name|
define_method name do
Native(`#@native[name]`)
end
}
end

def native_writer(*names)
names.each {|name|
define_method "#{name}=" do |value|
Native(`#@native[name] = value`)
end
}
end

def native_accessor(*names)
native_reader(*names)
native_writer(*names)
end
end

def self.included(klass)
Expand Down

0 comments on commit d9989fc

Please sign in to comment.