classy-yaml 0.5.0

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: 268a5b342320d4be502ad562a4740625645394eba3d927b97c3bf2919b611a5a
4
+ data.tar.gz: 4db1bc8ffbffed59fd5c60741614ac7e294be7e975a1b85fda472d0ab2319634
5
+ SHA512:
6
+ metadata.gz: b54b1fdb7ecaaccf4d9da488111ef2b40d54bf536d683667a7af12ad30bb3c5e0d372823612f41b6737f311be0fd231642fdfbd71c1331572649b67ce8a5f54c
7
+ data.tar.gz: d715ccf782b6c54ee93eb7acb1cb804428ae9b2fbcb646088c4c9ee41a3ba0aa2663938d22867f6e33c2ffa556758f0715885601ceee7f91260291aef7984210
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021
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,97 @@
1
+ # Classy::Yaml
2
+ This was created to provide convenient utility class grouping for environments without a bundler (or any situation where you might not want to add custom css classes). YAML files are a perfect structure for these situations.
3
+
4
+ ## Usage
5
+ After installing the gem, the helper method `yass` will be available from anywhere. It look for the definitions in the yaml file `config/utility_classes.yml` (note: you must add this yourself).
6
+
7
+ The YAML structure should follow this format:
8
+
9
+ ```
10
+ btn:
11
+ blue: "text-blue-200 bg-blue-500"
12
+ yellow: "text-yellow-200 bg-blue-500"
13
+ ```
14
+
15
+ Then, you can add these classes to any element by calling `yass(btn: :blue)` or `yass(btn: :yellow)`.
16
+
17
+ You may wonder though, what about shared classes you want all nested definitions to inherit? There is a special syntax for this type of inheritance:
18
+
19
+ ```
20
+ btn:
21
+ base: "px-3 py-2"
22
+ blue: "text-blue-200 bg-blue-500"
23
+ yellow: "text-yellow-200 bg-blue-500"
24
+ ```
25
+
26
+ Now, calling `yass(btn: :blue)` or `yass(btn: :yellow)` will ALSO pull in the classes from `btn: :base`.
27
+
28
+
29
+ ### ViewComponent
30
+ There is a special helper built for ViewComponent and sidecar assets. In your `example_component.rb`, add the line `include Classy::Yaml::ComponentHelpers`. This helper will tell `yass` to check if there is a `example_component.yml` file, and first use that for definitions. If the definitions aren't found in the `example_component.yml`, then it will fallback to `config/utility_classes.yml`.
31
+
32
+ So, it may look like this
33
+
34
+ ```
35
+ # config/utility_classes.yml
36
+ btn:
37
+ base: "px-3 py-2"
38
+ blue: "text-blue-200 bg-blue-500"
39
+ yellow: "text-yellow-200 bg-blue-500"
40
+
41
+
42
+ # app/components/example_component/example_component.yml
43
+ btn:
44
+ purple: "text-purple-200 bg-blue-500"
45
+
46
+
47
+ # app/components/example_component/example_component.html.erb
48
+
49
+ <button class="<%=sass(btn: :purple)%>">Classy Button</button>
50
+
51
+ # will add the classes "px-3 py-2 text-purple-200 bg-blue-500"
52
+ ```
53
+
54
+ As you can see, this will merge definitions found in the ExampleComponent's sidecar asset with the main application's definitions. Not only will it merge, but it will overwrite them as well, allowing for true customization on a per-component basis:
55
+ ```
56
+ # config/utility_classes.yml
57
+ btn:
58
+ base: "px-3 py-2"
59
+ blue: "text-blue-200 bg-blue-500"
60
+ yellow: "text-yellow-200 bg-blue-500"
61
+
62
+
63
+ # app/components/example_component/example_component.yml
64
+ btn:
65
+ blue: "text-blue-50"
66
+
67
+
68
+ # app/components/example_component/example_component.html.erb
69
+
70
+ <button class="<%=sass(btn: :purple)%>">Classy Button</button>
71
+
72
+ # will add the classes "px-3 py-2 text-blue-50"
73
+ ```
74
+
75
+
76
+ ## Installation
77
+ Add this line to your application's Gemfile:
78
+
79
+ ```ruby
80
+ gem 'classy-yaml'
81
+ ```
82
+
83
+ And then execute:
84
+ ```bash
85
+ $ bundle
86
+ ```
87
+
88
+ Or install it yourself as:
89
+ ```bash
90
+ $ gem install classy-yaml
91
+ ```
92
+
93
+ ## Contributing
94
+ This is my first attempt at an actual gem usable for all. Any and all suggestions are absolutely welcome. I'm not the cleanest programmer, so code quality suggestions are welcome too 👍 I extracted this logic from my private work and I felt it was useful enought to be worth publishing
95
+
96
+ ## License
97
+ 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,13 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,13 @@
1
+ module Classy
2
+ module Yaml
3
+ module ComponentHelpers
4
+ def yass(*args)
5
+ component_name = caller.first.split("/").last.split(".").first
6
+ calling_path = caller.first.split("/")[0...-1].join("/")
7
+ classy_file = calling_path + "/" + component_name + ".yml"
8
+
9
+ helpers.yass(args, classy_files: [classy_file] )
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Classy
2
+ module Yaml
3
+ class Engine < ::Rails::Engine
4
+ paths["lib/classy/yaml"]
5
+ config.to_prepare do
6
+ ApplicationController.helper(Classy::Yaml::Helpers)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,84 @@
1
+ module Classy
2
+ module Yaml
3
+ module Helpers
4
+ def yass(*args)
5
+ classy_yamls = []
6
+ classy_yamls << YAML.load_file(Rails.root.join("config/utility_classes.yml")) if File.exists?(Rails.root.join("config/utility_classes.yml"))
7
+
8
+ classy_files_hash = args.find { |arg| arg.is_a?(Hash) && arg.keys.include?(:classy_files) }
9
+ if classy_files_hash.present?
10
+ classy_files_hash[:classy_files].each do |file|
11
+ if File.exists?(file) && YAML.load_file(file)
12
+ file = YAML.load_file(file)
13
+ classy_yamls << file if file
14
+ end
15
+ end
16
+ end
17
+
18
+ return if classy_yamls.blank?
19
+
20
+ keys, classes = flatten_args(values: args)
21
+ classes += fetch_classes(keys, classy_yamls: classy_yamls)
22
+
23
+ return classes.flatten.join(" ")
24
+ end
25
+
26
+ private
27
+
28
+ def flatten_args(root: [], values: [], keys: [], added_classes: [])
29
+ values.each do|value|
30
+ if value.is_a?(Hash)
31
+ if value.has_key? :add
32
+ added_classes << value[:add]
33
+ value.except! :add
34
+ end
35
+
36
+ value.keys.each do |key|
37
+ values << (root + [key.to_s])
38
+ flatten_args(root: root + [key.to_s], values: [value[key]], keys: keys, added_classes: added_classes)
39
+ end
40
+ else
41
+ if value.is_a?(Array)
42
+ flatten_args(root: root, values: value, keys: keys, added_classes: added_classes)
43
+ else
44
+ keys << (root + [value.to_s])
45
+ end
46
+ end
47
+ end
48
+
49
+ return keys, added_classes
50
+ end
51
+
52
+ def fetch_classes(keys, classy_yamls: [])
53
+ classes = []
54
+
55
+ keys.map do |key|
56
+ base_classes = nil
57
+ fetched_classes = nil
58
+
59
+ classy_yamls.reverse_each do |classy_yaml|
60
+ base_classes ||= if classy_yaml.send(:dig, *key).is_a?(Hash)
61
+ classy_yaml.send(:dig, *(key + ['base'])).try(:split, " ")
62
+ else
63
+ classy_yaml.send(:dig, *(key[0...-1] + ['base'])).try(:split, " ")
64
+ end
65
+
66
+ fetched_classes ||= unless classy_yaml.send(:dig, *key).is_a?(Hash)
67
+ classy_yaml.send(:dig, *key).try(:split, " ")
68
+ end
69
+
70
+ base_classes = nil if base_classes.blank?
71
+ fetched_classes = nil if fetched_classes.blank?
72
+ end
73
+
74
+ classes << base_classes
75
+ classes << fetched_classes
76
+ end
77
+
78
+ classes.reject!(&:blank?)
79
+ return classes.flatten.uniq
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ module Classy
2
+ module Yaml
3
+ VERSION = '0.5.0'
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require "classy/yaml/version"
2
+ require "classy/yaml/engine"
3
+
4
+ module Classy
5
+ module Yaml
6
+ autoload :Helpers, "classy/yaml/helpers"
7
+ autoload :ComponentHelpers, "classy/yaml/component_helpers"
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :classy_yaml do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: classy-yaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Tonksthebear
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-11 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: 6.1.4
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.1.4.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.4
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.1.4.1
33
+ description: Rails helper to allow yaml configuration of utility css classes
34
+ email:
35
+ - ''
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - MIT-LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/classy/yaml.rb
44
+ - lib/classy/yaml/component_helpers.rb
45
+ - lib/classy/yaml/engine.rb
46
+ - lib/classy/yaml/helpers.rb
47
+ - lib/classy/yaml/version.rb
48
+ - lib/tasks/classy/yaml_tasks.rake
49
+ homepage: https://github.com/Tonksthebear/classy-yaml
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ allowed_push_host: https://rubygems.org
54
+ homepage_uri: https://github.com/Tonksthebear/classy-yaml
55
+ source_code_uri: https://github.com/Tonksthebear/classy-yaml
56
+ changelog_uri: https://github.com/Tonksthebear/classy-yaml/releases
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Rails helper to allow yaml configuration of utility css classes
76
+ test_files: []