cedar-component 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1c4605f9e4246750dc6fd877f5dd5f0bb67f347fcd55091af204082c226d117
4
+ data.tar.gz: 25e4e9904e2b1b80489bbeb4f58ce4cad4750305f16310d99fdea30325fe5105
5
+ SHA512:
6
+ metadata.gz: c43bb2ae1b938b7ef3af7dc0ec7aa1c6b7e06e3642c83ceaaf643ad4cbc1c8a82a9943428eae01e7afac166b9595fea4890f644488d3d5311e73d1d77613d6ce
7
+ data.tar.gz: 1367da1b4b18ef6acd578e20a701e08a3d58f511f72749202664b3abf19189603feb4ba8e49c26d32eaa6c67b921bb5fe6cfd8186560751b8f45e5c33acc9d08
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.2
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.4
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+ /log/*.log
11
+ /test/dummy/db/*.sqlite3
12
+ /test/dummy/db/*.sqlite3-*
13
+ /test/dummy/log/*.log
14
+ /test/dummy/storage/
15
+ /test/dummy/tmp/
16
+ .byebug_history
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
data/.versionrc.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "skip": {
3
+ "tag": true
4
+ },
5
+ "NAME": "Cedar",
6
+ "VERSION": "0.1.0"
7
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
+
5
+ ### [0.1.2](https://github.com/joshleblanc/cedar/compare/v0.1.1...v0.1.2) (2021-04-16)
6
+
7
+ ### [0.1.1](https://github.com/joshleblanc/cedar/compare/v0.1.0...v0.1.1) (2021-04-02)
8
+
9
+
10
+ ### Bug Fixes
11
+
12
+ * error handling for undefined helpers was confusing ([916cd35](https://github.com/joshleblanc/cedar/commit/916cd3556f80a311fc3917c74bd61e9fb0bf4115))
13
+
14
+ # Changelog
15
+
16
+ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at joshleblanc94@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cedar.gemspec
6
+ gemspec
7
+
8
+ gem "rails"
9
+ gem "rubocop", "~> 0.80"
10
+ gem "view_component"
11
+
12
+ group :development do
13
+ gem 'sqlite3'
14
+ end
15
+
16
+ # To use a debugger
17
+ # gem 'byebug', group: [:development, :test]
18
+
19
+ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
data/Gemfile.lock ADDED
@@ -0,0 +1,176 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cedar-component (0.1.2)
5
+ arbre
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.1.3.1)
11
+ actionpack (= 6.1.3.1)
12
+ activesupport (= 6.1.3.1)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (6.1.3.1)
16
+ actionpack (= 6.1.3.1)
17
+ activejob (= 6.1.3.1)
18
+ activerecord (= 6.1.3.1)
19
+ activestorage (= 6.1.3.1)
20
+ activesupport (= 6.1.3.1)
21
+ mail (>= 2.7.1)
22
+ actionmailer (6.1.3.1)
23
+ actionpack (= 6.1.3.1)
24
+ actionview (= 6.1.3.1)
25
+ activejob (= 6.1.3.1)
26
+ activesupport (= 6.1.3.1)
27
+ mail (~> 2.5, >= 2.5.4)
28
+ rails-dom-testing (~> 2.0)
29
+ actionpack (6.1.3.1)
30
+ actionview (= 6.1.3.1)
31
+ activesupport (= 6.1.3.1)
32
+ rack (~> 2.0, >= 2.0.9)
33
+ rack-test (>= 0.6.3)
34
+ rails-dom-testing (~> 2.0)
35
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
36
+ actiontext (6.1.3.1)
37
+ actionpack (= 6.1.3.1)
38
+ activerecord (= 6.1.3.1)
39
+ activestorage (= 6.1.3.1)
40
+ activesupport (= 6.1.3.1)
41
+ nokogiri (>= 1.8.5)
42
+ actionview (6.1.3.1)
43
+ activesupport (= 6.1.3.1)
44
+ builder (~> 3.1)
45
+ erubi (~> 1.4)
46
+ rails-dom-testing (~> 2.0)
47
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
48
+ activejob (6.1.3.1)
49
+ activesupport (= 6.1.3.1)
50
+ globalid (>= 0.3.6)
51
+ activemodel (6.1.3.1)
52
+ activesupport (= 6.1.3.1)
53
+ activerecord (6.1.3.1)
54
+ activemodel (= 6.1.3.1)
55
+ activesupport (= 6.1.3.1)
56
+ activestorage (6.1.3.1)
57
+ actionpack (= 6.1.3.1)
58
+ activejob (= 6.1.3.1)
59
+ activerecord (= 6.1.3.1)
60
+ activesupport (= 6.1.3.1)
61
+ marcel (~> 1.0.0)
62
+ mini_mime (~> 1.0.2)
63
+ activesupport (6.1.3.1)
64
+ concurrent-ruby (~> 1.0, >= 1.0.2)
65
+ i18n (>= 1.6, < 2)
66
+ minitest (>= 5.1)
67
+ tzinfo (~> 2.0)
68
+ zeitwerk (~> 2.3)
69
+ arbre (1.4.0)
70
+ activesupport (>= 3.0.0, < 6.2)
71
+ ruby2_keywords (>= 0.0.2, < 1.0)
72
+ ast (2.4.2)
73
+ builder (3.2.4)
74
+ concurrent-ruby (1.1.8)
75
+ crass (1.0.6)
76
+ erubi (1.10.0)
77
+ globalid (0.4.2)
78
+ activesupport (>= 4.2.0)
79
+ i18n (1.8.10)
80
+ concurrent-ruby (~> 1.0)
81
+ loofah (2.9.0)
82
+ crass (~> 1.0.2)
83
+ nokogiri (>= 1.5.9)
84
+ mail (2.7.1)
85
+ mini_mime (>= 0.1.1)
86
+ marcel (1.0.1)
87
+ method_source (1.0.0)
88
+ mini_mime (1.0.3)
89
+ minitest (5.14.4)
90
+ nio4r (2.5.7)
91
+ nokogiri (1.11.2-x64-mingw32)
92
+ racc (~> 1.4)
93
+ parallel (1.20.1)
94
+ parser (3.0.0.0)
95
+ ast (~> 2.4.1)
96
+ racc (1.5.2)
97
+ rack (2.2.3)
98
+ rack-test (1.1.0)
99
+ rack (>= 1.0, < 3)
100
+ rails (6.1.3.1)
101
+ actioncable (= 6.1.3.1)
102
+ actionmailbox (= 6.1.3.1)
103
+ actionmailer (= 6.1.3.1)
104
+ actionpack (= 6.1.3.1)
105
+ actiontext (= 6.1.3.1)
106
+ actionview (= 6.1.3.1)
107
+ activejob (= 6.1.3.1)
108
+ activemodel (= 6.1.3.1)
109
+ activerecord (= 6.1.3.1)
110
+ activestorage (= 6.1.3.1)
111
+ activesupport (= 6.1.3.1)
112
+ bundler (>= 1.15.0)
113
+ railties (= 6.1.3.1)
114
+ sprockets-rails (>= 2.0.0)
115
+ rails-dom-testing (2.0.3)
116
+ activesupport (>= 4.2.0)
117
+ nokogiri (>= 1.6)
118
+ rails-html-sanitizer (1.3.0)
119
+ loofah (~> 2.3)
120
+ railties (6.1.3.1)
121
+ actionpack (= 6.1.3.1)
122
+ activesupport (= 6.1.3.1)
123
+ method_source
124
+ rake (>= 0.8.7)
125
+ thor (~> 1.0)
126
+ rainbow (3.0.0)
127
+ rake (13.0.3)
128
+ regexp_parser (2.1.1)
129
+ rexml (3.2.4)
130
+ rubocop (0.93.1)
131
+ parallel (~> 1.10)
132
+ parser (>= 2.7.1.5)
133
+ rainbow (>= 2.2.2, < 4.0)
134
+ regexp_parser (>= 1.8)
135
+ rexml
136
+ rubocop-ast (>= 0.6.0)
137
+ ruby-progressbar (~> 1.7)
138
+ unicode-display_width (>= 1.4.0, < 2.0)
139
+ rubocop-ast (1.4.1)
140
+ parser (>= 2.7.1.5)
141
+ ruby-progressbar (1.11.0)
142
+ ruby2_keywords (0.0.4)
143
+ sprockets (4.0.2)
144
+ concurrent-ruby (~> 1.0)
145
+ rack (> 1, < 3)
146
+ sprockets-rails (3.2.2)
147
+ actionpack (>= 4.0)
148
+ activesupport (>= 4.0)
149
+ sprockets (>= 3.0.0)
150
+ sqlite3 (1.4.2)
151
+ thor (1.1.0)
152
+ tzinfo (2.0.4)
153
+ concurrent-ruby (~> 1.0)
154
+ tzinfo-data (1.2021.1)
155
+ tzinfo (>= 1.0.0)
156
+ unicode-display_width (1.7.0)
157
+ view_component (2.28.0)
158
+ activesupport (>= 5.0.0, < 7.0)
159
+ websocket-driver (0.7.3)
160
+ websocket-extensions (>= 0.1.0)
161
+ websocket-extensions (0.1.5)
162
+ zeitwerk (2.4.2)
163
+
164
+ PLATFORMS
165
+ x64-mingw32
166
+
167
+ DEPENDENCIES
168
+ cedar-component!
169
+ rails
170
+ rubocop (~> 0.80)
171
+ sqlite3
172
+ tzinfo-data
173
+ view_component
174
+
175
+ BUNDLED WITH
176
+ 2.2.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Joshua LeBlanc
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,111 @@
1
+ # Cedar
2
+
3
+ Cedar allows you to render view_component without a sidecar template. It leverages ActiveAdmin's arbre to provide a pure ruby DSL for creating markup.
4
+ In addition Cedar provides convenience methods for rendering components and component collections.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'cedar-component'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install cedar
21
+
22
+ ## Usage
23
+
24
+ To use Cedar, you need to extend your components with `Cedar::Component`. The most straight forward way to do this is right
25
+ in your base `ApplicationComponent` class.
26
+
27
+ ```ruby
28
+ class ApplicationComponent < ViewComponent::Base
29
+ extend Cedar::Component
30
+ end
31
+ ```
32
+
33
+ `Cedar` provides 3 key pieces of functionality:
34
+
35
+ # Rendering an arbre tree
36
+
37
+ `render` is now available to you as a class level method, which renders the contained `arbre` tree.
38
+
39
+ ```ruby
40
+ class FoobarComponent < ApplicationComponent
41
+ render do
42
+ div do
43
+ para "Wow this is kind of neat"
44
+ end
45
+ end
46
+ end
47
+ ```
48
+
49
+ # Rendering components
50
+
51
+ We provide a shortcut for rendering components themselves. If you have a component called `MyCoolButtonComponent`, you can render this by simply calling `my_cool_button` within your arbre tree.
52
+
53
+ ```ruby
54
+ class FoobarComponent < ApplicationComponent
55
+ class MyCoolButtonComponent < ApplicationComponent
56
+ def initialize(label:)
57
+ @label = label
58
+ end
59
+
60
+ render { button_tag "Click me!", class: "btn btn-primary" }
61
+ end
62
+
63
+ render do
64
+ div do
65
+ para "We're going to render our cool button below"
66
+ my_cool_button label: "Click me!"
67
+ end
68
+ end
69
+ end
70
+ ```
71
+
72
+ ## Rendering collections
73
+
74
+ Suffixing your component tag with `_collection` will render a collection of your components.
75
+
76
+ ```ruby
77
+ class FoobarComponent < ApplicationComponent
78
+ class MyCoolButtonComponent < ApplicationComponent
79
+ with_collection_param :label
80
+ def initialize(label:)
81
+ @label = label
82
+ end
83
+ render { button_tag "Click me!", class: "btn btn-primary" }
84
+ end
85
+
86
+ render do
87
+ my_cool_button_collection %w[One Two Three Four]
88
+ end
89
+ end
90
+ ```
91
+
92
+ ## ActionView helpers
93
+
94
+ If you need to access the underlying rails helpers instead of the arbre helpers, use the `helpers` object.
95
+ For example, if you were making a compatible wrapper around `label`:
96
+
97
+ ```ruby
98
+ class LabelComponenent < ApplicationComponent
99
+ def initialize(object_name, method, text = nil, options = {})
100
+ @object_name = object_name
101
+ @method = method
102
+ @text = text
103
+ @options = options
104
+ @options[:class] ||= "label"
105
+ end
106
+
107
+ render do
108
+ helpers.label @object_name, @method, @text, @options
109
+ end
110
+ end
111
+ ```
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "cedar"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cedar.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/cedar/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cedar-component"
7
+ spec.version = Cedar::VERSION
8
+ spec.authors = ["Joshua LeBlanc"]
9
+ spec.email = ["jleblanc@hey.com"]
10
+
11
+ spec.summary = "Provides inline rendering to view components"
12
+ spec.description = "Extends view components to allow rendering inline arbre templates"
13
+ spec.homepage = "https://github.com/joshleblanc/cedar"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/joshleblanc/cedar"
21
+ spec.metadata["changelog_uri"] = "https://github.com/joshleblanc/cedar"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+ spec.add_dependency "arbre"
35
+
36
+ # For more information and examples about making a new gem, checkout our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
data/lib/cedar.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'arbre'
4
+ require_relative "cedar/version"
5
+ require_relative "cedar/component_warden"
6
+ require_relative "cedar/component_wardens"
7
+ require_relative "cedar/context"
8
+ require_relative "cedar/component"
9
+
10
+ module Cedar
11
+ class Error < StandardError; end
12
+ # Your code goes here...
13
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cedar
4
+
5
+ ##
6
+ # InlineRender provides a `render` dsl to your components
7
+ # which will render any arbre tree within.
8
+ #
9
+ # It will also convert snake case component names to component instances
10
+ # for example, this would be equivalent to `ButtonComponent.new(label: "Test")`
11
+ #
12
+ # ```ruby
13
+ # render do
14
+ # div do
15
+ # button label: "Test"
16
+ # end
17
+ # end
18
+ # ```
19
+ module Component
20
+ include Arbre::HTML
21
+
22
+ # The empty first arg is just to trick rubymine
23
+ # when using `render` inside the arbre context
24
+ def render(&blk)
25
+ define_method :call do
26
+ ctx = Context.new(self)
27
+ ctx.instance_eval(&blk).to_s
28
+ end
29
+ end
30
+
31
+ def find_component(name)
32
+ if const_defined?(name)
33
+ const_get(name)
34
+ else
35
+ if superclass.respond_to?(:find_component)
36
+ superclass.find_component(name)
37
+ else
38
+ adjusted_name = name.chomp("Component").underscore
39
+ raise(NameError, "undefined local variable or method `#{adjusted_name}` for #{self}")
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cedar
4
+ ##
5
+ # A ComponentWarden is responsible for locating potential components
6
+ # given a snake_case representation of that component.
7
+ #
8
+ # If a component exists, the warden is also responsible for calling the correct
9
+ # instantiation method, depending if the component is a collection or not
10
+ class ComponentWarden
11
+ def initialize(component, string)
12
+ @component = component
13
+ @raw_input = string
14
+
15
+ @collection = @raw_input.end_with? "_collection"
16
+ @input = @raw_input.to_s.chomp("_collection")
17
+ end
18
+
19
+ def collection?
20
+ @collection
21
+ end
22
+
23
+ def component_name
24
+ "#{@input}_component".camelize
25
+ end
26
+
27
+ def component_class
28
+ @component_class ||= @component.class.find_component(component_name)
29
+ end
30
+
31
+ def exists?
32
+ component_class.present?
33
+ end
34
+
35
+ def new(*args, &block)
36
+ return nil unless exists?
37
+
38
+ if collection?
39
+ component_class.with_collection(*args, &block)
40
+ else
41
+ component_class.new(*args, &block)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cedar
4
+ ##
5
+ # Thin wrapper around a hash of component wardens
6
+ # such that the default is always an instance of ComponentWarden
7
+ class ComponentWardens
8
+ def initialize(component)
9
+ @wardens = Hash.new do |hash, key|
10
+ hash[key] = ComponentWarden.new(component, key)
11
+ end
12
+ end
13
+
14
+ def [](key)
15
+ @wardens[key]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cedar
4
+ ##
5
+ # Inject a component's instance variables into arbre's default context
6
+ # as well as provide implicit handling of snake-cased component names as
7
+ # tags
8
+ class Context < Arbre::Context
9
+
10
+ def initialize(component)
11
+ super({}, component)
12
+
13
+ @component = component
14
+ @component_wardens = ComponentWardens.new(@component)
15
+
16
+ # Copy all of the instance variables from the component to the context,
17
+ # so we have access to them when rendering
18
+ @component.instance_variables.each do |iv|
19
+ instance_variable_set(iv, @component.instance_variable_get(iv))
20
+ end
21
+ end
22
+
23
+ def respond_to_missing?(method, include_all)
24
+ @component.respond_to?(method) || @component_wardens[method].exists? || super
25
+ end
26
+
27
+ def method_missing(method, *args, &content_block)
28
+ if @component.respond_to?(method)
29
+ @component.send(method, *args, &content_block)
30
+ elsif @component_wardens[method].exists?
31
+ render @component_wardens[method].new(*args, &content_block)
32
+ else
33
+ super
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cedar
4
+ VERSION = "0.1.2"
5
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cedar-component
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Joshua LeBlanc
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: arbre
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Extends view components to allow rendering inline arbre templates
28
+ email:
29
+ - jleblanc@hey.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".github/workflows/main.yml"
35
+ - ".gitignore"
36
+ - ".rubocop.yml"
37
+ - ".versionrc.json"
38
+ - CHANGELOG.md
39
+ - CODE_OF_CONDUCT.md
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/console
46
+ - bin/setup
47
+ - cedar.gemspec
48
+ - lib/cedar.rb
49
+ - lib/cedar/component.rb
50
+ - lib/cedar/component_warden.rb
51
+ - lib/cedar/component_wardens.rb
52
+ - lib/cedar/context.rb
53
+ - lib/cedar/version.rb
54
+ homepage: https://github.com/joshleblanc/cedar
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ allowed_push_host: https://rubygems.org
59
+ homepage_uri: https://github.com/joshleblanc/cedar
60
+ source_code_uri: https://github.com/joshleblanc/cedar
61
+ changelog_uri: https://github.com/joshleblanc/cedar
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.3.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.1.4
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Provides inline rendering to view components
81
+ test_files: []