Skip to content
yet another slugging gem
Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
.editorconfig
.gitignore
.rspec
.travis.yml
Gemfile
LICENSE
Rakefile
changelog.md
readme.md
slugity.gemspec

readme.md

Description

Yet another slugging gem, convert a string to a slug with the option for custom mappings.

Build Status Code Climate


Installation

gem install slugity

or with bundler

gem 'slugity'

Ruby Support

Slugity is tested against and supports:

  • 2.1.x
  • 2.0.0
  • 1.9.3
  • rubinius 2.2

Use

To use Slugity, require it and then include the module where needed. So:

require 'slugity'
include Slugity

slugity("one + two = three")
# => "one-plus-two-equals-three"

To include the to_slug method on the String class

require 'slugity/extend_string'

"one + two = three".to_slug
# => "one-plus-two-equals-three"

There is also a Slugity::Convert module that has class methods for each if you don't want to polute any modules with extra methods, so:

require 'slugity'
Slugity::Convert.slug 'one + two'
# => 'one-plus-two'

Custom Matchers

The default matcher set is currently small, but with custom matchers you can extend it or write a new set from the ground up.

View the full set here or view a sample of the matched symbols.

default: {
  /\s|\// => '-',
  /\~|\~/ => '-',
  /\_/ => '_',
  /\&/ => 'and',
  /\+/ => 'plus',
  /\=/ => 'equals',
  /℡/ => 'tel',
  /™/ => 'tm',
  /℠/ => 'sm',
  /²/ => '2',
  /³/ => '3'
}

Lets say we want to use the default matchers, but we want exclamation marks to become 'omg'

Slugity::Matchers.add :omg, true, {
  /\!/ => "omg"
}

We created a new matcher named :omg, told it to extend from the default matcher, and then added a matcher for the exclamation mark. To use it, we then tell the slugity or to_slug method to use our new matcher.

slugity( "woot!", :omg )
# => "wootomg"

"woot!".to_slug(:omg)
# => "wootomg"

Non-Slug Matching

Slugity is also good for manipulating strings that aren't destined to be slugs, such as strings in json. To do this though we'll need to use a different method included with Slugity, stringity. It operates just as slugity except that it doesn't escape unrecognized characters.

string = %q{ a "quote" \ or so }
stringity(string, :json_string)
# => "a \"quote\" \\ or so"

Examples

require 'slugity'
include Slugity

slugity "hello world"
# => hello-world

slugity "one & one"
# => one-and-one
require 'slugity/extend_string'

"hello world".to_slug
# => hello-world

"one & one".to_slug
# => one-and-one

Testing

$ bundle install
# => installs the testing gems

$ rake
# => runs the specs

Contributing

If there is any thing you'd like to contribute or fix, please:

  • Fork the repo
  • Add tests for any new functionality
  • Make your changes
  • Verify all existing tests work properly
  • Make a pull request

License

The slugity gem is Copyright 2013 Steven Sloan, distributed under the MIT License.

You can’t perform that action at this time.