Skip to content

Commit d9989fc

Browse files
committedMar 17, 2014
Add #native_reader, #native_writer and #native_accessor
1 parent 8ae7056 commit d9989fc

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'native'
2+
3+
describe "Native.native_reader" do
4+
it "refers to an attribute on @native" do
5+
Class.new {
6+
include Native
7+
8+
native_reader :a
9+
}.new(`{ a: 2 }`).a.should == 2
10+
end
11+
12+
it "uses multiple names" do
13+
n = Class.new {
14+
include Native
15+
16+
native_reader :a, :b
17+
}.new(`{ a: 2, b: 3 }`)
18+
19+
n.a.should == 2
20+
n.b.should == 3
21+
end
22+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'native'
2+
3+
describe "Native.native_writer" do
4+
it "refers to an attribute on @native" do
5+
n = Class.new {
6+
include Native
7+
8+
native_reader :a
9+
native_writer :a
10+
}.new(`{ a: 2 }`)
11+
12+
n.a = 4
13+
n.a.should == 4
14+
end
15+
16+
it "supports multiple names" do
17+
n = Class.new {
18+
include Native
19+
20+
native_reader :a, :b
21+
native_writer :a, :b
22+
}.new(`{ a: 2, b: 3 }`)
23+
24+
n.a = 4
25+
n.b = 5
26+
27+
n.a.should == 4
28+
n.b.should == 5
29+
end
30+
end

‎stdlib/native.rb

+21
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ def alias_native(new, old = new, options = {})
8686
end
8787
end
8888
end
89+
90+
def native_reader(*names)
91+
names.each {|name|
92+
define_method name do
93+
Native(`#@native[name]`)
94+
end
95+
}
96+
end
97+
98+
def native_writer(*names)
99+
names.each {|name|
100+
define_method "#{name}=" do |value|
101+
Native(`#@native[name] = value`)
102+
end
103+
}
104+
end
105+
106+
def native_accessor(*names)
107+
native_reader(*names)
108+
native_writer(*names)
109+
end
89110
end
90111

91112
def self.included(klass)

0 commit comments

Comments
 (0)
Please sign in to comment.