Text::Gen
A Ruby gem for building random text strings from rules defined as Ruby hashes. Rules consist of Builders containing Items composed of Segments. Generated text can be modified with filters like pluralize, capitalize, and titleize.
Installation
Add this line to your application's Gemfile:
gem 'text-gen'
And then execute:
bundle install
Or install it yourself as:
gem install text-gen
Usage
require 'text/gen'
# Define builders with items and filters
random_fruit = {
"items" => [{ "segments" => Text::Gen::Segment::Parser.parse("[d4] [fruit]") }],
"filters" => [{ "type" => "pluralize" }, { "type" => "titleize" }]
}
fruit = {
"items" => %w[apple banana orange pear kiwi].map { |f| { "segments" => Text::Gen::Segment::Parser.parse(f) } }
}
# Create a lookup function
lookup = lambda { |key| key == "random-fruit" ? random_fruit : fruit }
# Run the generator
runner = Text::Gen::Runner.new(key: "random-fruit", lookup: lookup)
result = runner.run.first
puts result.text # => "3 Bananas"
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/palmergs/text-gen. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Text::Gen project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.