Skip to content

Commit

Permalink
add clsx class, railstie and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SethHorsley committed Mar 3, 2024
1 parent 614c8a6 commit 170e66c
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 108 deletions.
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,42 @@
## [0.1.0] - 2024-03-02

- Initial release

These examples should be working

Strings (variadic)
```ruby
clsx("foo", true && "bar", "baz")
#=> "foo bar baz"
```

Objects
```ruby
clsx({ foo:true, bar:false, baz:is_true? })
#=> "foo baz"
```

Objects (variadic)
```ruby
clsx({ foo:true }, { bar:false }, null, { "--foobar":"hello" })
#=> "foo --foobar"
```

Arrays
```ruby
clsx(["foo", 0, false, "bar"])
#=> "foo bar"
```

Arrays (variadic)
```ruby
clsx(["foo"], ["", 0, false, "bar"], [["baz", [["hello"], "there"]]])
#=> "foo bar baz hello there"
```

Kitchen sink (with nesting)
```ruby
clsx("foo", [1 && "bar", { baz:false, bat:null }, ["hello", ["world"]]], "cya")
#=> "foo bar hello world cya"
```

84 changes: 0 additions & 84 deletions CODE_OF_CONDUCT.md

This file was deleted.

22 changes: 22 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
PATH
remote: .
specs:
clsx (0.1.0)

GEM
remote: https://rubygems.org/
specs:
minitest (5.22.2)
rake (13.1.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
clsx!
minitest (~> 5.16)
rake (~> 13.0)

BUNDLED WITH
2.5.3
57 changes: 46 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
# Clsx
## A gem for constructing HTML class strings conditionally

TODO: Delete this and the text below, and describe your gem

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/clsx`. To experiment with that code, run `bin/console` for an interactive prompt.
Ruby utility for constructing HTML class strings conditionally with the provided syntax, you can define a module named Clsx that includes a method to handle each case: strings, objects (hashes), arrays, and a combination of these with nested structures. This method will recursively process each argument, filter out falsy values, and concatenate the truthy values into a single string.

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.

Install the gem and add to the application's Gemfile by executing:

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

If bundler is not being used to manage dependencies, install the gem by executing:
$ bundle add "clsx"

$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG

## Usage
Strings (variadic)
```ruby
clsx("foo", true && "bar", "baz")
#=> "foo bar baz"
```

Objects
```ruby
clsx({ foo:true, bar:false, baz:is_true? })
#=> "foo baz"
```

Objects (variadic)
```ruby
clsx({ foo:true }, { bar:false }, null, { "--foobar":"hello" })
#=> "foo --foobar"
```

Arrays
```ruby
clsx(["foo", 0, false, "bar"])
#=> "foo bar"
```

Arrays (variadic)
```ruby
clsx(["foo"], ["", 0, false, "bar"], [["baz", [["hello"], "there"]]])
#=> "foo bar baz hello there"
```

Kitchen sink (with nesting)
```ruby
clsx("foo", [1 && "bar", { baz:false, bat:null }, ["hello", ["world"]]], "cya")
#=> "foo bar hello world cya"
```

You can also use clsx in rails views like this:
```ruby
<div class="<%= clsx({ 'border': true, "border-t": false, "border-b":true }) %>">
<!-- ... -->
</div>
```

TODO: Write usage instructions here

## Development

Expand All @@ -28,7 +63,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/clsx. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/clsx/blob/main/CODE_OF_CONDUCT.md).
Bug reports and pull requests are welcome. Just fork this repo make the changes you want and submit a pull request back to this repo :)

## License

Expand Down
12 changes: 5 additions & 7 deletions clsx.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ Gem::Specification.new do |spec|
spec.authors = ["Seth Horsley"]
spec.email = ["isethi@me.com"]

spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = "A gem for constructing HTML class strings conditionally"
spec.description = "Ruby utility for constructing HTML class strings conditionally with the provided syntax, you can define a module named Clsx that includes a method to handle each case: strings, objects (hashes), arrays, and a combination of these with nested structures. This method will recursively process each argument, filter out falsy values, and concatenate the truthy values into a single string."
spec.homepage = "https://github.com/iseth/ruby-clsx"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"

spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down
21 changes: 20 additions & 1 deletion lib/clsx.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
# frozen_string_literal: true

require_relative "clsx/version"
require "clsx/railtie" if defined?(Rails)

module Clsx
class Error < StandardError; end
# Your code goes here...

def self.clsx(*args)
args.flat_map { |arg| parse(arg) }.compact.join(" ")
end

private

def self.parse(arg)
case arg
when String
arg unless arg.empty?
when Hash
arg.map { |k, v| k if v }.compact
when Array
arg.flat_map { |a| parse(a) }
else
nil
end
end
end
18 changes: 18 additions & 0 deletions lib/clsx/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "clsx"
require "rails"

module Clsx
class Railtie < Rails::Railtie
initializer "clsx.action_view" do
ActiveSupport.on_load :action_view do
ActionView::Base.send :include, Helper
end
end
end

module Helper
def clsx(*args)
Clsx.clsx(*args)
end
end
end
26 changes: 21 additions & 5 deletions test/test_clsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@

require "test_helper"

class TestClsx < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::Clsx::VERSION
class ClsxTest < Minitest::Test
def test_strings
assert_equal "foo bar baz", Clsx.clsx("foo", true && "bar", "baz")
end

def test_it_does_something_useful
assert false
def test_objects
assert_equal "foo baz", Clsx.clsx({ foo: true, bar: false, baz: true })
end

def test_objects_variadic
assert_equal "foo --foobar", Clsx.clsx({ foo: true }, { bar: false }, nil, { "--foobar" => "hello" })
end

def test_arrays
assert_equal "foo bar", Clsx.clsx(["foo", 0, false, "bar"])
end

def test_arrays_variadic
assert_equal "foo bar baz hello there", Clsx.clsx(["foo"], ["", 0, false, "bar"], [["baz", [["hello"], "there"]]])
end

def test_kitchen_sink
assert_equal "foo bar hello world cya", Clsx.clsx("foo", [1 && "bar", { baz: false, bat: nil }, ["hello", ["world"]]], "cya")
end
end

0 comments on commit 170e66c

Please sign in to comment.