opal_stimulus 0.1.0 → 0.1.2
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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +19 -4
- data/lib/install/opal +12 -2
- data/lib/opal_stimulus/stimulus_controller.rb +16 -14
- data/lib/opal_stimulus/version.rb +1 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 300ef0df34590b96644f09bec7f618c308189700c8b91c909bccb4bd7cb48760
|
4
|
+
data.tar.gz: 4d405702dc56dcd9a0fd980c6c834dce308303a4e73f950239ed33c58493bd56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 320be88eef00e84f254a120dd0c3029f6027daacba7aeab96c74af79de26b1150034de44200d299fad77d1817e9f298e1f1111d7c608fb076cf4db9ef571831b
|
7
|
+
data.tar.gz: 548d689d02602637832156e6e88be3ff29224ce12dd5cba5de985d04c1567eabc1ad539120b9f6cd7dee41c687ec86324ac3179c4ffac04ed5cc15f79f5fb524
|
data/CHANGELOG.md
CHANGED
@@ -3,3 +3,11 @@
|
|
3
3
|
## [0.1.0] - 2025-03-27
|
4
4
|
|
5
5
|
- Initial release
|
6
|
+
|
7
|
+
## [0.1.1] - 2025-07-02
|
8
|
+
|
9
|
+
- Generate better stimulus controller name replacing controller class's namespace resolution operators to --
|
10
|
+
|
11
|
+
## [0.1.2] - 2025-07-29
|
12
|
+
|
13
|
+
- Implement Opal Source maps https://opalrb.com/docs/guides/v1.4.1/source_maps.html
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Opal Stimulus for Rails
|
2
2
|
|
3
3
|
**Opal Stimulus** is a Stimulus wrapper made with Opal (a source-to-source Ruby to JavaScript compiler) that allows you to write Stimulus controllers in Ruby instead of JavaScript (It works only with Rails).
|
4
4
|
|
@@ -10,11 +10,17 @@ Add this line to your Gemfile:
|
|
10
10
|
gem 'opal_stimulus'
|
11
11
|
```
|
12
12
|
|
13
|
-
|
13
|
+
Execute:
|
14
14
|
|
15
15
|
```bash
|
16
16
|
bundle install
|
17
|
-
rails
|
17
|
+
rails opal_stimulus:install
|
18
|
+
```
|
19
|
+
|
20
|
+
Start application:
|
21
|
+
|
22
|
+
```bash
|
23
|
+
bin/dev
|
18
24
|
```
|
19
25
|
|
20
26
|
## Basic Example
|
@@ -25,11 +31,13 @@ Here's a Hello World example with OpalStimulus. Compare with the [original JavaS
|
|
25
31
|
|
26
32
|
```ruby
|
27
33
|
# app/opal/controllers/hello_controller.rb
|
34
|
+
# new controllers will be automatically added to app/opal/controllers_requires.rb
|
35
|
+
# (ordered files load is not supported yet)
|
28
36
|
class HelloController < StimulusController
|
29
37
|
self.targets = ["name", "output"]
|
30
38
|
|
31
39
|
def greet
|
32
|
-
output_target.
|
40
|
+
output_target.content = "Hello, #{name_target.value}!"
|
33
41
|
end
|
34
42
|
end
|
35
43
|
```
|
@@ -49,6 +57,13 @@ end
|
|
49
57
|
</div>
|
50
58
|
```
|
51
59
|
|
60
|
+
**Result**
|
61
|
+
|
62
|
+
https://github.com/user-attachments/assets/c51ed28c-13d2-4e06-b882-1cc997e9627b
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
52
67
|
## Contributing
|
53
68
|
|
54
69
|
Bug reports and pull requests are welcome on GitHub at https://github.com/josephschito/opal_stimulus.
|
data/lib/install/opal
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
require "bundler/setup"
|
5
5
|
require "listen"
|
6
6
|
require "opal"
|
7
|
+
require "opal-browser"
|
7
8
|
|
8
9
|
GEM_NAME = "opal_stimulus"
|
9
10
|
|
@@ -14,9 +15,18 @@ Opal.append_path("app/opal")
|
|
14
15
|
build = -> {
|
15
16
|
puts "🔨 Compiling Opal..."
|
16
17
|
|
17
|
-
builder = Opal::Builder.
|
18
|
+
builder = Opal::Builder.new
|
19
|
+
|
20
|
+
result = builder.build("application")
|
21
|
+
|
18
22
|
output_path = "app/assets/builds/opal.js"
|
19
|
-
|
23
|
+
sourcemap_path = "#{output_path}.map"
|
24
|
+
|
25
|
+
js_code = result.to_s
|
26
|
+
source_map_json = result.source_map.to_json
|
27
|
+
|
28
|
+
File.write(output_path, js_code + "\n//# sourceMappingURL=/assets/opal.js.map")
|
29
|
+
File.write(sourcemap_path, source_map_json)
|
20
30
|
|
21
31
|
puts "✅ Compiled to #{output_path}"
|
22
32
|
}
|
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
require "opal"
|
4
4
|
require "native"
|
5
|
+
require "promise"
|
6
|
+
require "browser/setup/full"
|
5
7
|
|
6
8
|
class StimulusController < `Controller`
|
7
9
|
include Native::Wrapper
|
@@ -20,23 +22,19 @@ class StimulusController < `Controller`
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def self.stimulus_name
|
23
|
-
self.name.gsub(/Controller$/, "").gsub(/([a-z])([A-Z])/, '\1-\2').downcase
|
25
|
+
self.name.gsub(/Controller$/, "").gsub(/([a-z])([A-Z])/, '\1-\2').gsub("::", "--").downcase
|
24
26
|
end
|
25
27
|
|
26
28
|
def self.method_added(name)
|
27
29
|
return if DEFAULT_GETTERS.include?(name)
|
28
30
|
|
29
|
-
self.bridge_method(name)
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.register(controller)
|
33
|
-
`Stimulus.register(#{self.stimulus_name}, #{controller})`
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.bridge_method(name)
|
37
31
|
%x{
|
38
32
|
#{self.stimulus_controller}.prototype[name] = function (...args) {
|
39
|
-
|
33
|
+
try {
|
34
|
+
return this['$' + name].apply(this, args);
|
35
|
+
} catch (e) {
|
36
|
+
console.error("Uncaught", e);
|
37
|
+
}
|
40
38
|
}
|
41
39
|
}
|
42
40
|
end
|
@@ -46,11 +44,13 @@ class StimulusController < `Controller`
|
|
46
44
|
|
47
45
|
targets.each do |target|
|
48
46
|
define_method(target + "_target") do
|
49
|
-
`
|
47
|
+
Browser::DOM::Element.new(`this[#{target + "Target"}]`)
|
50
48
|
end
|
51
49
|
|
52
50
|
define_method(target + "_targets") do
|
53
|
-
`
|
51
|
+
`this[#{target + "Targets"}]`.map do |el|
|
52
|
+
Browser::DOM::Element.new(el)
|
53
|
+
end
|
54
54
|
end
|
55
55
|
|
56
56
|
define_method("has_" + target + "_target") do
|
@@ -84,11 +84,13 @@ class StimulusController < `Controller`
|
|
84
84
|
|
85
85
|
outlets.each do |outlet|
|
86
86
|
define_method(outlet + "_outlet") do
|
87
|
-
`
|
87
|
+
Browser::DOM::Element.new(`this[#{outlet + "Outlet"}]`)
|
88
88
|
end
|
89
89
|
|
90
90
|
define_method(outlet + "_outlets") do
|
91
|
-
`
|
91
|
+
`this[#{outlet + "Outlets"}]`.map do |outlet|
|
92
|
+
Browser::DOM::Element.new(outlet)
|
93
|
+
end
|
92
94
|
end
|
93
95
|
|
94
96
|
define_method("has_" + outlet + "_outlet") do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opal_stimulus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Schito
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: opal
|
@@ -37,6 +37,20 @@ dependencies:
|
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: 3.9.0
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: opal-browser
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.3.5
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.3.5
|
40
54
|
description: Opal Stimulus provides a way to write Stimulus controllers in Ruby, leveraging
|
41
55
|
the Opal compiler to convert Ruby code into JavaScript. This allows developers familiar
|
42
56
|
with Ruby to use the Stimulus framework without needing to write JavaScript directly.
|
@@ -83,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
97
|
- !ruby/object:Gem::Version
|
84
98
|
version: '0'
|
85
99
|
requirements: []
|
86
|
-
rubygems_version: 3.6.
|
100
|
+
rubygems_version: 3.6.9
|
87
101
|
specification_version: 4
|
88
102
|
summary: 'Opal Stimulus: Write Stimulus controllers in Ruby'
|
89
103
|
test_files: []
|