opal_proxy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 13a2623ddda8f5c4e6892bc3c96f4cca256c45bb7fa18831c75a38e738bb7cd1
4
+ data.tar.gz: 2a54e5826508f77937177d5354054ac6f48408977ee9ffe3eeb78926f2bc462c
5
+ SHA512:
6
+ metadata.gz: 502c1850edd96983c5f1816aea4e365d9468bc18a558f2c57348126c52456a7847b7a5be8ac782008d5d195d795021e9b045a228944603631e2c84e3cca224d2
7
+ data.tar.gz: 4326f0e789b89a6279b68d1eba455b9133b4a0e9c7192de0c1ad9b21ef749acb7493213a820aadf6555782267bd04e3a93ad5ed35bd433c8e3d6a732f4fca106
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-07-24
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Joseph Schito
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # OpalProxy
2
+
3
+ Opal Proxy provides a dynamic interface to JavaScript objects in Opal,
4
+ allowing seamless property access, method calls, and Promise handling using idiomatic Ruby syntax.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your Gemfile:
9
+
10
+ ```ruby
11
+ gem 'opal_proxy'
12
+ ```
13
+
14
+ Execute:
15
+
16
+ ```bash
17
+ bundle install
18
+ ```
19
+
20
+
21
+ ## Document example
22
+
23
+ ```ruby
24
+ require "js/proxy"
25
+
26
+ class Document < JSProxy
27
+ def initialize
28
+ super($$.document)
29
+ end
30
+ end
31
+
32
+ document = Document.new
33
+ headers = document.querySelectorAll("h1") # or query_selector_all
34
+ headers.each do |h1|
35
+ h1.text_content = "Opal is great!" # or textContent
36
+ end
37
+
38
+ document.body.style.background_color = "lightblue"
39
+ document.body.style.font_family = "Arial, sans-serif"
40
+ document.body.style.color = "darkblue"
41
+ ```
42
+
43
+ ## Window example
44
+
45
+ ```ruby
46
+ require "js/proxy"
47
+ # ... including document
48
+
49
+ class Window < JSProxy
50
+ def initialize
51
+ super($$.window)
52
+ end
53
+ end
54
+
55
+ window = Window.new
56
+ window.alert "Hello world!"
57
+ window.set_timeout(-> {
58
+ puts "1. Timeout test OK (1s delay)"
59
+ }, 1000)
60
+ window.fetch("https://jsonplaceholder.typicode.com/todos/1")
61
+ .then do |response|
62
+ response.json().then do |data|
63
+ puts "5. Fetched: #{data["title"]}"
64
+ document.get_element_by_id("output").inner_html += "<p>5. Fetched: #{data["title"]}</p>"
65
+ end
66
+ end
67
+ ```
68
+
69
+ ## JQuery example
70
+
71
+ ```ruby
72
+ require "js/proxy"
73
+ # ... including document
74
+
75
+ doc = Document.new
76
+ jquery_script = doc.createElement('script')
77
+ jquery_script.src = "https://code.jquery.com/jquery-3.7.1.min.js"
78
+ doc.head.appendChild(jquery_script)
79
+ jquery_script.onload = -> {
80
+ class JQuery < JS::Proxy
81
+ def initialize(node)
82
+ super(`$(node)`)
83
+ end
84
+ end
85
+
86
+ document = JQuery.new($$.document)
87
+ document.ready do
88
+ paragraph = doc.createElement('p')
89
+ paragraph.text_content = "If you click on me, I will disappear."
90
+ doc.body.appendChild(paragraph)
91
+ JQuery.new("p").click(&:hide)
92
+ end
93
+ }
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/josephschito/opal_proxy.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
data/lib/js/proxy.rb ADDED
@@ -0,0 +1,177 @@
1
+ require "opal"
2
+ require "native"
3
+
4
+ module JS
5
+ module Helpers
6
+ def wrap_result(result)
7
+ if `result && typeof result.then === 'function'`
8
+ Promise.new(result)
9
+ elsif `typeof result === 'object' && result !== null`
10
+ Proxy.new(result)
11
+ else
12
+ result
13
+ end
14
+ end
15
+
16
+ def native_methods
17
+ @native_methods ||= %x{
18
+ let obj = #{to_n};
19
+ const props = new Set();
20
+
21
+ while (obj !== null) {
22
+ for (const key of Reflect.ownKeys(obj)) {
23
+ const stringKey = key.toString()
24
+ const rubyName = #{to_rb_name(`stringKey`)}
25
+
26
+ if (typeof key !== 'symbol' && stringKey !== rubyName ) { props.add(rubyName); }
27
+ props.add(stringKey);
28
+ }
29
+ obj = Object.getPrototypeOf(obj);
30
+ }
31
+
32
+ return Array.from(props);
33
+ }
34
+ end
35
+ end
36
+
37
+ class Proxy
38
+ include Enumerable
39
+ include Helpers
40
+
41
+ attr_accessor :native
42
+
43
+ IRREGULARS = %w(html url uri)
44
+
45
+ def initialize(native)
46
+ @native = Native(native)
47
+ end
48
+
49
+ def method_missing(name, *args, &block)
50
+ js_name = to_js_name(name)
51
+
52
+ unless existing_property?(js_name) || js_name.end_with?("=")
53
+ raise NoMethodError, "undefined method `#{name}` for #{self}"
54
+ end
55
+
56
+ if js_name.end_with?("=")
57
+ prop = js_name[0..-2]
58
+ native[prop] = args.first
59
+ else
60
+ val = native[js_name]
61
+
62
+ if `typeof val === 'function'`
63
+ js_args = args.dup
64
+
65
+ if block
66
+ js_callback = %x{
67
+ function() {
68
+ let args = Array.prototype.slice.call(arguments);
69
+ return #{block.call(self.class.new(`this`), *args)};
70
+ }
71
+ }
72
+ js_args << js_callback
73
+ end
74
+
75
+ result = `val.apply(#{to_n}, #{js_args.to_n})`
76
+ wrap_result(result)
77
+ elsif `typeof val === 'object' && val !== null`
78
+ wrap_result(val)
79
+ else
80
+ val
81
+ end
82
+ end
83
+ end
84
+
85
+ def to_str
86
+ `#{to_n}.toString()`
87
+ end
88
+
89
+ def respond_to_missing?(name, include_private = false)
90
+ true
91
+ end
92
+
93
+ def each
94
+ return enum_for(:each) unless respond_to?(:length)
95
+
96
+ length = self.length
97
+ (0...length).each do |i|
98
+ yield self[i]
99
+ end
100
+ end
101
+
102
+ def [](index)
103
+ val = native[index]
104
+ wrap_result(val)
105
+ end
106
+
107
+ def to_n
108
+ native.to_n
109
+ end
110
+
111
+ def length
112
+ native.length
113
+ end
114
+
115
+ private
116
+
117
+ def to_js_name(name)
118
+ name.to_s.split('_').map.with_index do |part, index|
119
+ if IRREGULARS.include? part.gsub("=", "").downcase
120
+ part.upcase
121
+ else
122
+ index.zero? ? part : part.capitalize
123
+ end
124
+ end.join
125
+ end
126
+
127
+ def to_rb_name(name)
128
+ name
129
+ .to_s
130
+ .gsub(/([A-Z]+)/) { "_#{$1.downcase}" }
131
+ .sub(/^_/, '')
132
+ end
133
+
134
+ def existing_property?(property)
135
+ `#{property} in #{to_n}`
136
+ end
137
+ end
138
+
139
+ class Promise < Proxy
140
+ include Helpers
141
+
142
+ def then(&block)
143
+ js_callback = %x{
144
+ function(value) {
145
+ var ruby_result = #{block.call(wrap_result(`value`))};
146
+ if (ruby_result && typeof ruby_result.then === 'function') {
147
+ return ruby_result;
148
+ } else if (ruby_result && typeof ruby_result.to_n === 'function') {
149
+ return ruby_result.to_n();
150
+ } else {
151
+ return ruby_result;
152
+ }
153
+ }
154
+ }
155
+
156
+ self.native = `#{to_n}.then(#{js_callback})`
157
+ end
158
+
159
+ def catch(&block)
160
+ js_callback = %x{
161
+ function(error) {
162
+ var ruby_result = #{block.call(wrap_result(`error`))};
163
+
164
+ if (ruby_result && typeof ruby_result.then === 'function') {
165
+ return ruby_result;
166
+ } else if (ruby_result && typeof ruby_result.to_n === 'function') {
167
+ return ruby_result.to_n();
168
+ } else {
169
+ return ruby_result;
170
+ }
171
+ }
172
+ }
173
+
174
+ self.native = `#{to_n}.catch(#{js_callback})`
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpalProxy
4
+ VERSION = "0.1.0"
5
+ end
data/lib/opal_proxy.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ if RUBY_ENGINE == 'opal'
4
+ require_relative "js/proxy"
5
+ else
6
+ require "opal"
7
+ require_relative "opal_proxy/version"
8
+
9
+ Opal.append_path File.expand_path('lib', __dir__)
10
+ end
11
+
12
+ module OpalProxy
13
+ end
@@ -0,0 +1,4 @@
1
+ module OpalProxy
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opal_proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Schito
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: opal
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 1.8.2
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 1.8.2
26
+ description: Opal Proxy provides a dynamic interface to JavaScript objects in Opal,
27
+ allowing seamless property access, method calls, and Promise handling using idiomatic
28
+ Ruby syntax.
29
+ email:
30
+ - joseph.schito@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/js/proxy.rb
41
+ - lib/opal_proxy.rb
42
+ - lib/opal_proxy/version.rb
43
+ - sig/opal_proxy.rbs
44
+ homepage: https://github.com/josephschito/opal_proxy
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/josephschito/opal_proxy
49
+ source_code_uri: https://github.com/josephschito/opal_proxy
50
+ changelog_uri: https://github.com/josephschito/opal_proxy/blob/main/CHANGELOG.md
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 3.1.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.6.9
66
+ specification_version: 4
67
+ summary: Dynamic Ruby-style wrapper for JavaScript objects in Opal.
68
+ test_files: []