classnames 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +8 -0
  3. data/README.md +37 -0
  4. data/lib/classnames.rb +37 -0
  5. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3ebb80be48cbe732432a7c13cf1701ad657c986
4
+ data.tar.gz: d3f8dfc3a45bd3fd6b8b681d880af9464ab0e87e
5
+ SHA512:
6
+ metadata.gz: dd4aac17656e307f076bf6174340108f117f56ecfde255b95909bc0366e96981c3e22a51223e09fc5744e67b864e8003f3ece7ca3fab300f6ca391a074012c51
7
+ data.tar.gz: 01b5d5778d401b899723c7ff0cc74659aadf942077ba577695295ed11eaaeb51a26c6a366898ac38e89d6fc09ffb90e7730fb8ded10c6f639ae852134100982f
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2016 Mikkel Malmberg
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # classnames
2
+
3
+ This is a port of JavaScript's [classnames](https://www.npmjs.com/package/classnames) for Ruby. The API is basically the same except with Ruby's types.
4
+
5
+ ### Example usage
6
+
7
+ ```ruby
8
+ module ApplicationHelper
9
+ include ClassNames
10
+
11
+ def example
12
+ str = classnames(
13
+ 'takes',
14
+ ['any', 'kind'],
15
+ { of: true, not_me: false },
16
+ ->{ 'arguments' },
17
+ nil, # and ignores ...
18
+ false, # ... falsey values
19
+ )
20
+ # => "takes any kind of arguments"
21
+
22
+ content_tag(:div, class: str) { 'So hot' }
23
+ end
24
+ end
25
+ ```
26
+
27
+ ### Installation
28
+
29
+ Add `classnames` to your `Gemfile`:
30
+
31
+ ```rb
32
+ gem 'classnames', '~> 1.0.0'
33
+ ```
34
+
35
+ ### License
36
+
37
+ MIT
@@ -0,0 +1,37 @@
1
+ module ClassNames
2
+ def classnames(*args)
3
+ args = Array(args)
4
+
5
+ args.reduce([]) do |arr, arg|
6
+ if value = format(arg)
7
+ arr << value
8
+ else
9
+ arr
10
+ end
11
+ end.flatten.join(' ')
12
+ end
13
+
14
+ private
15
+
16
+ def format arg
17
+ if arg.is_a?(Hash)
18
+ format_hash(arg)
19
+ elsif arg.respond_to?(:call)
20
+ arg.call
21
+ else
22
+ arg
23
+ end
24
+ end
25
+
26
+ def format_hash hsh
27
+ hsh.reduce([]) do |result, kv|
28
+ key, value = kv
29
+
30
+ if value
31
+ result.concat([key])
32
+ else
33
+ result
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: classnames
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikkel Malmberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - mikkel@brnbw.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/classnames.rb
23
+ homepage: https://github.com/mikker/classnames
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.5.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A copy of js' classnames
47
+ test_files: []