kombu 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: 555b72c094087f10c29ed89696f91d92ac68516aa256fb78b08a1ed7acf8c986
4
+ data.tar.gz: 8c23db5a65333c5a6024b572a3f4c06e73151494006e7d0fd69d92cfa251cf87
5
+ SHA512:
6
+ metadata.gz: 0f3575fd1fbe1d603eb952a2242f4eaffa60fa2b2e4491e3909e9cdca21ad5ea2d27f90d25619b95ad32195b3157fffe32c295cf0483e1cc2e0c4b5e35b46b05
7
+ data.tar.gz: 5bc0893ee1a32b87e43f4c22a4a4c32beba1c0fb002eeca9d86348e4bcd08b6b73640c894e5835ddcdd41bc2962118ab215bffca58f7ea4ba3184123763388a3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 madogiwa
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,147 @@
1
+ # Kombu
2
+
3
+ Kombu provides the ability to render the specified component directly from the controller.
4
+
5
+ This gem may replace any javascript component except layouts in the `views` directory.
6
+
7
+ This gem was inspired by the following projects.
8
+
9
+ - [React-Rails](https://github.com/reactjs/react-rails)
10
+ - [Inertia.js Rails Adapter](https://github.com/inertiajs/inertia-rails)
11
+
12
+ ## Usage
13
+
14
+ ### Setup
15
+
16
+ Please configure the following settings according to your application.
17
+
18
+ ```ruby
19
+ Rails.application.configure do
20
+ # NOTE: (OPTIONAL) id of the element (div) to mount (default: `vue-root`)
21
+ # config.kombu.default_mount_element_id = 'vue-root'
22
+
23
+ # NOTE: (REQUIIRED) Specify a proc that generates a tag that reads a javascript entry.
24
+ # See `lib/kombu/renderable.rb` for instance variables provided by kombu that can be used within proc.
25
+ config.kombu.javascript_entry_tag_proc = -> { helpers.javascript_pack_tag(@entry, defer: true) }
26
+
27
+ # NOTE: (REQUIIRED) Specify a proc that generates a tag that reads a css entry.
28
+ # See `lib/kombu/renderable.rb` for instance variables provided by kombu that can be used within proc.
29
+ config.kombu.stylesheet_entry_tag_proc = -> { helpers.stylesheet_pack_tag(@entry) }
30
+
31
+ # NOTE: (OPTIONAL) template of the view to render that contains the component. (default: See below)
32
+ # config.kombu.default_entry_view_template = <<~ERB
33
+ # <div id="<%= @kombu_mount_element_id %>">
34
+ # <%= kombu_component_tag %>
35
+ # </div>
36
+ # <% content_for :stylesheet do %>
37
+ # <%= kombu_stylesheet_entry_tag %>
38
+ # <% end %>
39
+ # <% content_for :javascript do %>
40
+ # <%= kombu_javascript_entry_tag %>
41
+ # <% end %>
42
+ # ERB
43
+ end
44
+ ```
45
+
46
+ ### Use `Kombu::Renderable`
47
+
48
+ If you include `Kombu::Renderable` in any controller, you can use `kombu_render_component` which can render component directly.
49
+
50
+ ```ruby
51
+ class ArticlesController < ApplicationController
52
+ include Kombu::Renderable
53
+
54
+ def index
55
+ @title = 'Articles'
56
+ @articles = [{ id: 1, title: 'artile1', body: 'body1' }, { id: 2, title: 'artile2', body: 'body2' }]
57
+ kombu_render_component('article-index-page', attributes: { 'title': @title, ':articles': @articles.to_json })
58
+ # NOTE: The following html is rendered.
59
+ # <div id="vue-root"><artile-index-page title="Articles" :articles="[{"id":1,"title":"artile1","body":"body1"},{"id":2,"title":"artile2","body":"body2"}]"></div>
60
+ </artile-index-page>
61
+ end
62
+ end
63
+ ```
64
+
65
+ ### (Optional) Rename method name.
66
+
67
+ If you do not like the method name, you can change it to any name you like.
68
+
69
+ ```ruby
70
+ module ComponentRenderable
71
+ extend ActiveSupport::Concern
72
+ include Kombu::Renderable
73
+
74
+ included do
75
+ alias render_component kombu_render_component
76
+ end
77
+ end
78
+ ```
79
+
80
+ ```ruby
81
+ class ArticlesController < ApplicationController
82
+ include ComponentRenderable
83
+
84
+ def index
85
+ @title = 'Articles'
86
+ @articles = [{ id: 1, title: 'artile1', body: 'body1' }, { id: 2, title: 'artile2', body: 'body2' }]
87
+ render_component('article-index-page', attributes: { 'title': @title, ':articles': @articles.to_json })
88
+ end
89
+ end
90
+ ```
91
+
92
+ ### Testing (RSpec only)
93
+
94
+ Kombu provides a matcher to validate values passed to `kombu_render_component`.
95
+ You can use `kombu_component_rendered` by enabling it in `RSpec.configure`.
96
+
97
+ ```ruby
98
+ require 'kombu/test/rspec/matchers/component_renderd_matcher'
99
+
100
+ RSpec.configure do |config|
101
+ config.include Kombu::RSpec::Matchers::ComponentRenderedMatcher, type: :request
102
+ end
103
+ ```
104
+
105
+ ```ruby
106
+ describe 'GET /articles', type: :request do
107
+ before { get articles_path }
108
+
109
+ it 'Specific arguments must be passed to render_component.' do
110
+ title = 'Articles'
111
+ articles = [{ id: 1, title: 'artile1', body: 'body1' }, { id: 2, title: 'artile2', body: 'body2' }]
112
+ expect(controller).to kombu_component_rendered('article-index-page', attributes: { 'title': title, ':articles': articles.to_json })
113
+ end
114
+ end
115
+ ```
116
+
117
+ ## How do it work
118
+
119
+ See `lib/kombu/renderable.rb`.
120
+
121
+ ## Installation
122
+
123
+ Add this line to your application's Gemfile:
124
+
125
+ ```ruby
126
+ gem "kombu"
127
+ ```
128
+
129
+ And then execute:
130
+
131
+ ```bash
132
+ $ bundle install
133
+ ```
134
+
135
+ Or install it yourself as:
136
+
137
+ ```bash
138
+ $ gem install kombu
139
+ ```
140
+
141
+ ## Contributing
142
+
143
+ Contribution directions go here.
144
+
145
+ ## License
146
+
147
+ 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,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/ordered_options"
5
+
6
+ class Configration < ActiveSupport::OrderedOptions
7
+ def custom_payload(&block)
8
+ self.custom_payload_method = block
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kombu
4
+ class Error < StandardError; end
5
+
6
+ class RenderError < Error; end
7
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+ require_relative "configration"
5
+
6
+ module Kombu
7
+ class Railtie < ::Rails::Railtie
8
+ MOUNT_ELEMENT_ID = "vue-root"
9
+ ENTRY_VIEW_TEMPLATE = <<~ERB
10
+ <div id="<%= @kombu_mount_element_id %>">
11
+ <%= kombu_component_tag %>
12
+ </div>
13
+ <% content_for :stylesheet do %>
14
+ <%= kombu_stylesheet_entry_tag %>
15
+ <% end %>
16
+ <% content_for :javascript do %>
17
+ <%= kombu_javascript_entry_tag %>
18
+ <% end %>
19
+ ERB
20
+
21
+ config.kombu = Configration.new
22
+ config.kombu.default_mount_element_id = MOUNT_ELEMENT_ID
23
+ config.kombu.default_entry_view_template = ENTRY_VIEW_TEMPLATE
24
+ end
25
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errors"
4
+
5
+ module Kombu
6
+ module Renderable
7
+ include ActionView::Helpers::TagHelper
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ helper_method :kombu_component_tag, :kombu_javascript_entry_tag, :kombu_stylesheet_entry_tag
12
+ end
13
+
14
+ def kombu_render_component(component_name, entry_name: nil, mount_element_id: nil, attributes: {})
15
+ @kombu_component = component_name
16
+ @kombu_attributes = attributes
17
+ @kombu_entry = entry_name.presence || kombu_default_entry
18
+ @kombu_mount_element_id = mount_element_id.presence || kombu_default_mount_element_id
19
+ render kombu_render_option
20
+ rescue => error
21
+ raise Kombu::RenderError, error.message
22
+ end
23
+
24
+ def kombu_component_tag
25
+ tag(@kombu_component, @kombu_attributes)
26
+ end
27
+
28
+ def kombu_javascript_entry_tag
29
+ raise_kombu_render_error_not_configured("javascript_entry_tag_proc") if kombu_javascript_entry_tag_proc.nil?
30
+ instance_exec(&kombu_javascript_entry_tag_proc)
31
+ end
32
+
33
+ def kombu_stylesheet_entry_tag
34
+ raise_kombu_render_error_not_configured("stylesheet_entry_tag_proc") if kombu_stylesheet_entry_tag_proc.nil?
35
+ instance_exec(&kombu_stylesheet_entry_tag_proc)
36
+ end
37
+
38
+ private
39
+
40
+ def kombu_default_entry
41
+ File.join(controller_name, action_name)
42
+ end
43
+
44
+ def kombu_render_option
45
+ {inline: kombu_template, layout: true}
46
+ end
47
+
48
+ def kombu_template
49
+ kombu_app_config.default_entry_view_template
50
+ end
51
+
52
+ def kombu_javascript_entry_tag_proc
53
+ kombu_app_config.javascript_entry_tag_proc
54
+ end
55
+
56
+ def kombu_stylesheet_entry_tag_proc
57
+ kombu_app_config.stylesheet_entry_tag_proc
58
+ end
59
+
60
+ def kombu_default_mount_element_id
61
+ kombu_app_config.default_mount_element_id
62
+ end
63
+
64
+ def kombu_app_config
65
+ @kombu_app_config ||= Rails.application.config.kombu
66
+ end
67
+
68
+ def raise_kombu_render_error_not_configured(config_name)
69
+ raise Kombu::RenderError,
70
+ "config.kombu.#{config_name} is not configured. Please set it during application initialization."
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kombu
4
+ module RSpec
5
+ module Matchers
6
+ module ComponentRenderedMatcher
7
+ def kombu_component_rendered(component, entry: nil, mount_element_id: nil, attributes: {})
8
+ Matcher.new(component, entry: entry, mount_element_id: mount_element_id, attributes: attributes)
9
+ end
10
+
11
+ class Matcher
12
+ def initialize(component, entry: nil, mount_element_id: nil, attributes: {})
13
+ @expected_component = component
14
+ @expected_entry = entry
15
+ @expected_mount_element_id = mount_element_id
16
+ @expected_attributes = attributes
17
+ end
18
+
19
+ def matches?(controller)
20
+ set_actual_avaliables(controller)
21
+ component_match? && entry_match? && mount_element_id_match? && attributes_match?
22
+ end
23
+
24
+ def failure_message
25
+ return failure_component_message unless component_match?
26
+ return failure_attributes_message unless attributes_match?
27
+ return failure_entry_message unless entry_match?
28
+ return failure_mount_element_id_message unless mount_element_id_match?
29
+ end
30
+
31
+ private
32
+
33
+ def failure_component_message
34
+ "Expected component #{@expected_component} to match #{@actual_component}."
35
+ end
36
+
37
+ def failure_attributes_message
38
+ expected = pretty_json(@expected_attributes.to_json)
39
+ actual = pretty_json(@actual_attributes.to_json)
40
+ "Expected component attributes #{expected} to match #{actual}."
41
+ end
42
+
43
+ def failure_entry_message
44
+ "Expected entry #{@expected_entry} to match #{@actual_entry}."
45
+ end
46
+
47
+ def failure_mount_element_id_message
48
+ "Expected mount element id #{@expected_mount_element_id} to match #{@actual_mount_element_id}."
49
+ end
50
+
51
+ def component_match?
52
+ @expected_component == @actual_component
53
+ end
54
+
55
+ def attributes_match?
56
+ @expected_attributes == @actual_attributes
57
+ end
58
+
59
+ def entry_match?
60
+ return true if @expected_entry.nil?
61
+ @expected_entry == @actual_entry
62
+ end
63
+
64
+ def mount_element_id_match?
65
+ return true if @expected_mount_element_id.nil?
66
+ @expected_mount_element_id == @actual_mount_element_id
67
+ end
68
+
69
+ def set_actual_avaliables(actual) # rubocop:disable Naming/AccessorMethodName
70
+ @actual_component = actual.instance_variable_get(:@kombu_component)
71
+ @actual_attributes = actual.instance_variable_get(:@kombu_attributes)
72
+ @actual_entry = actual.instance_variable_get(:@kombu_entry)
73
+ @actual_mount_element_id = actual.instance_variable_get(:@kombu_mount_element_id)
74
+ end
75
+
76
+ def pretty_json(str)
77
+ JSON.pretty_generate(JSON.parse(str))
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module Kombu
2
+ VERSION = "0.1.0"
3
+ end
data/lib/kombu.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "kombu/version"
2
+ require "kombu/railtie"
3
+
4
+ module Kombu
5
+ end
6
+
7
+ require_relative "kombu/renderable"
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kombu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - madogiwa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ description:
28
+ email:
29
+ - madogiwa0124@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/kombu.rb
38
+ - lib/kombu/configration.rb
39
+ - lib/kombu/errors.rb
40
+ - lib/kombu/railtie.rb
41
+ - lib/kombu/renderable.rb
42
+ - lib/kombu/test/rspec/matchers/component_rendered_matcher.rb
43
+ - lib/kombu/version.rb
44
+ homepage: https://github.com/madogiwa0124/kombu
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/madogiwa0124/kombu
49
+ source_code_uri: https://github.com/madogiwa0124/kombu
50
+ post_install_message:
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: '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.4.1
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Kombu provides the ability to render the specified component directly from
69
+ the controller.
70
+ test_files: []