utility_classes 0.1.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: fc3a27fdd8eb9c65a9c7aaa296f0aba4b3ddb93a2333a12fcd063b61d896a546
4
+ data.tar.gz: 3c19c85e940ae919c5c939c5b92ca5bcb2c64801b1cf62dd9c643b9a5f5aff41
5
+ SHA512:
6
+ metadata.gz: 4db6adf30d9895e3fc28170aa760a891dc7515d7ffa6f519df2ca557df87d763bbccbaef8ec77838998bfd49f150cde016e2d3aeb1a2c8a5109a05960dd6e3da
7
+ data.tar.gz: f0efac2fac26eae4bdfefe097f32f0cbf25a4f8d4215df9e5e888bc5b56f5b4fc5b2faa2e2b6e1a4574663251fd46fc380e6fc17291f0ac019d8c853e0008558
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Dick Davis
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,77 @@
1
+ # UtilityClasses
2
+
3
+ Rails gem that facilitates the use of utility CSS frameworks.
4
+
5
+ This project was inspired by the [classy-yaml](https://github.com/Tonksthebear/classy-yaml) project.
6
+
7
+ ## Usage
8
+
9
+ You can easily define a base class list for a given type:
10
+
11
+ `config/utility_classes.yml`
12
+ ```yaml
13
+ heading:
14
+ base: 'text-lg font-semibold text-center'
15
+ ```
16
+
17
+ And then retrieve the class list in your code:
18
+
19
+ ```ruby
20
+ UtilityClasses.for(:heading)
21
+ ```
22
+
23
+ You can optionally define variants for each type:
24
+
25
+ `config/utility_classes.yml`
26
+ ```yaml
27
+ heading:
28
+ base: 'text-lg font-semibold text-center'
29
+ variants:
30
+ grande: 'text-4xl'
31
+ ```
32
+
33
+ And then retrieve the class list by passing the variant key:
34
+
35
+ ```ruby
36
+ UtilityClasses.for(:heading, variant: :grande)
37
+ ```
38
+
39
+ The returned string will contain the base classes with the variant classes appended.
40
+
41
+ The `config/utility_classes.yml` file will be loaded into memory the first time it is called and only be reloaded if changes are made to the file.
42
+
43
+ ## Installation
44
+
45
+ Add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem 'utility_classes'
49
+ ```
50
+
51
+ And then execute:
52
+
53
+ ```bash
54
+ $ bundle
55
+ ```
56
+
57
+ Or install it yourself as:
58
+
59
+ ```bash
60
+ $ gem install utility_classes
61
+ ```
62
+
63
+ Run the install generator to create a config file (`config/utility_classes.yml`):
64
+
65
+ ```bash
66
+ $ bin/rails generate utility_classes:install
67
+ ```
68
+
69
+ Add your classes to the generated config file following the structure of the examples.
70
+
71
+ ## Contributing
72
+
73
+ Contributions are welcome. Feel free to open a PR.
74
+
75
+ ## License
76
+
77
+ 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
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.pattern = "test/**/*_test.rb"
9
+ t.verbose = false
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UtilityClasses
4
+ module Generators
5
+ ##
6
+ # Creates basic installation files for UtilityClasses
7
+ class InstallGenerator < Rails::Generators::Base
8
+ desc 'Create basic installation files for UtilityClasses'
9
+ source_root File.expand_path('templates', __dir__)
10
+
11
+ def copy_config_file
12
+ copy_file 'utility_classes.yml', 'config/utility_classes.yml'
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # The structure of the config file for utility_classes should follow the following pattern:
2
+ #
3
+ # ELEMENT:
4
+ # base: BASE STYLES HERE
5
+ # variants:
6
+ # NAMED VARIANTS HERE
7
+ #
8
+ # For example:
9
+ #
10
+ # btn:
11
+ # base: 'appearance-none cursor-pointer inline-block text-lg font-semibold text-center align-middle no-underline select-none transition-colors border-2 border-solid rounded-md px-4 py-3 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50'
12
+ # variants:
13
+ # solid_blue: 'bg-blue-500 text-white border-blue-500 hover:bg-blue-700'
14
+ # outline_blue: 'bg-transparent text-blue-500 border-blue-500 hover:bg-blue-500 hover:text-white'
15
+ #
16
+ # You do not need to add variants if the element does not require it. For example, this is valid:
17
+ #
18
+ # btn:
19
+ # base: 'appearance-none cursor-pointer inline-block text-lg font-semibold text-center align-middle no-underline select-none transition-colors border-2 border-solid rounded-md px-4 py-3 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50'
20
+ #
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :utility_classes do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UtilityClasses
4
+ module Exceptions
5
+ ##
6
+ # Exception thrown when type key is not found in config.
7
+ class TypeKeyNotFound < StandardError
8
+ attr_reader :type_key
9
+
10
+ def initialize(type_key:)
11
+ @type_key = type_key
12
+ super("Type key #{type_key} not found in config")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UtilityClasses
4
+ module Exceptions
5
+ ##
6
+ # Exception thrown when variant key is not found in config.
7
+ class VariantKeyNotFound < StandardError
8
+ attr_reader :variant_key
9
+
10
+ def initialize(variant_key:)
11
+ @variant_key = variant_key
12
+ super("Variant key #{variant_key} not found in config")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ module UtilityClasses
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ require_relative 'exceptions/type_key_not_found'
6
+ require_relative 'exceptions/variant_key_not_found'
7
+
8
+ module UtilityClasses
9
+ ##
10
+ # Provides access to utility classes stored in config
11
+ class UtilityClassReader
12
+ def initialize
13
+ @styles = data_from_config
14
+ @last_updated = File.ctime(config_file)
15
+ end
16
+
17
+ def for(type, variant: nil)
18
+ reload_config! if config_changed?
19
+
20
+ classes = styles.dig(type, :base)
21
+ raise UtilityClasses::Exceptions::TypeKeyNotFound.new(type_key: type) unless classes
22
+
23
+ if variant
24
+ variant_classes = styles.dig(type, :variants, variant)
25
+ raise UtilityClasses::Exceptions::VariantKeyNotFound.new(variant_key: variant) unless variant_classes
26
+ end
27
+
28
+ variant_classes ? "#{classes} #{variant_classes}" : classes
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :styles, :last_updated
34
+
35
+ def data_from_config
36
+ YAML.safe_load(File.read(config_file)).deep_symbolize_keys
37
+ end
38
+
39
+ def config_file
40
+ Rails.root.join('config/utility_classes.yml')
41
+ end
42
+
43
+ def reload_config!
44
+ @styles = data_from_config
45
+ @last_updated = File.ctime(config_file)
46
+ end
47
+
48
+ def config_changed?
49
+ return false unless Rails.env.development?
50
+
51
+ last_updated != File.ctime(config_file)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UtilityClasses
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+ require 'active_support/dependencies'
5
+ require 'utility_classes/version'
6
+ require 'utility_classes/railtie'
7
+
8
+ ##
9
+ # Namespace for UtilityClasses gem
10
+ module UtilityClasses
11
+ autoload :UtilityClassReader, 'utility_classes/utility_class_reader'
12
+ autoload :TypeKeyNotFound, 'utility_classes/exceptions/type_key_not_found'
13
+ autoload :VariantKeyNotFound, 'utility_classes/exceptions/variant_key_not_found'
14
+
15
+ mattr_accessor :reader
16
+ def self.reader
17
+ @reader ||= UtilityClassReader.new
18
+ end
19
+
20
+ def self.for(type, variant: nil)
21
+ reader.for(type, variant: variant)
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utility_classes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dick Davis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-05 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.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.0
27
+ description: Rails gem that facilitates the use of utility CSS frameworks.
28
+ email:
29
+ - dick@hey.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/generators/utility_classes/install_generator.rb
38
+ - lib/generators/utility_classes/templates/utility_classes.yml
39
+ - lib/tasks/utility_classes_tasks.rake
40
+ - lib/utility_classes.rb
41
+ - lib/utility_classes/exceptions/type_key_not_found.rb
42
+ - lib/utility_classes/exceptions/variant_key_not_found.rb
43
+ - lib/utility_classes/railtie.rb
44
+ - lib/utility_classes/utility_class_reader.rb
45
+ - lib/utility_classes/version.rb
46
+ homepage: https://github.com/d3d1rty/utility_classes
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/d3d1rty/utility_classes
51
+ source_code_uri: https://github.com/d3d1rty/utility_classes
52
+ changelog_uri: https://github.com/d3d1rty/utility_classes
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.2.15
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Rails gem that facilitates the use of utility CSS frameworks.
72
+ test_files: []