Class: Module

Inherits:
Object show all
Defined in:
lib/opal/native/bridge.rb

Overview

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                  Version 2, December 2004

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Instance Method Summary collapse

Instance Method Details

#attr_accessor_bridge(target, *attrs) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/opal/native/bridge.rb', line 62

def attr_accessor_bridge (target, *attrs)
	attrs.each {|attr|
		define_attr_bridge(target, attr, true, true)
	}

	self
end

#attr_bridge(target, name, setter = false) ⇒ Object



86
87
88
89
90
# File 'lib/opal/native/bridge.rb', line 86

def attr_bridge (target, name, setter = false)
	define_attr_bridge(target, name, true, setter)

	self
end

#attr_reader_bridge(target, *attrs) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/opal/native/bridge.rb', line 70

def attr_reader_bridge (target, *attrs)
	attrs.each {|attr|
		define_attr_bridge(target, attr, true, false)
	}

	self
end

#attr_writer_bridge(target, *attrs) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/opal/native/bridge.rb', line 78

def attr_writer_bridge (target, *attrs)
	attrs.each { |attr|
		define_attr_bridge(target, attr, false, true)
	}

	self
end

#define_attr_bridge(target, name, getter, setter) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opal/native/bridge.rb', line 12

def define_attr_bridge (target, name, getter, setter)
	if getter
		if Symbol === target
			if target.start_with? '@'
				define_method name do
					object = instance_variable_get(target)

					Kernel.Native(`#{Native === object ? object : object.to_native}[name]`)
				end
			else
				define_method name do
					object = __send__(target)

					Kernel.Native(`#{Native === object ? object : object.to_native}[name]`)
				end
			end
		else
			define_method name do |*args, &block|
				Kernel.Native(`target[name]`)
			end
		end
	end

	if setter
		if Symbol === target
			if target.start_with? '@'
				define_method "#{name}=" do |value|
					object = instance_variable_get(target)
					value  = value.to_native unless Native === value

					Kernel.Native(`#{Native === object ? object : object.to_native}[name] = value`)
				end
			else
				define_method "#{name}=" do |value|
					object = __send__(target)
					value  = value.to_native unless Native === value

					Kernel.Native(`#{Native === object ? object : object.to_native}[name] = value`)
				end
			end
		else
			define_method "#{name}=" do |value|
				value = value.to_native unless Native === value

				Kernel.Native(`target[name] = value`)
			end
		end
	end
end

#define_method_bridge(target, name, ali = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/opal/native/bridge.rb', line 92

def define_method_bridge (target, name, ali = nil)
	if Symbol === target
		if target.start_with? '@'
			define_method ali || name do |*args, &block|
				object = instance_variable_get(target)

				Native.send(Native === object ? object : object.to_native, name, *args, &block)
			end
		else
			define_method ali || name do |*args, &block|
				object = __send__(target)

				Native.send(Native === object ? object : object.to_native, name, *args, &block)
			end
		end
	else
		define_method ali || name do |*args, &block|
			Native.send(target, name, *args, &block)
		end
	end

	nil
end