Connie

Little compact library to synthesise data that does not load big files in memory.

Heavily based on Forgery it uses the same word sources. It’s built to be a bit more customisable and allows you to define new strategies and styles both using ruby modules or big text lists.

Install

On the console:

>> gem install connie

In your Gemfile

gem 'connie'

Usage

Connie offers four exciting functionalities:

  • Dictionaries

  • Shorthand to pick a random element from an array

  • Interpolating strings

Dictionaries

A dictionary is a vast source of words for a subject, Connie has 4 dictionaries built in:

  • Names

  • Geo

  • Net

  • Creative

  • Shopping

For example inspecting the names dictionary:

Connie[:names].inspect # Shorthand to access a dictionary
<Connie::Dictionary - Connie::Names - company last female suffix title male first gender>

You receive a quick overview of what this dictionary can generate. To have the dictionary generate something simply:

Connie[:names].first

Shorthands

Connie provides a couple of shorthands methods to randomise your data:

Connie([:one, :two, :three]) #=> returns randomly selected element of the array
Connie.letter                #=> returns a random lowercase letter
Connie.letter :uppercase     #=> returns a random uppercse letter
Connie?                      #=> returns true or false. Randomly

Interpolations

Interpolations happen in a dictionary but allow to reach others through scoping

>> Connie[:names].interpolate "Mr. :male :last: :geo.city, :geo.state_short"
#=> "Mr. Mark Taylor: Pleasant Hill, NY"

A shorthand method i is provided for convenience and interpolation is also available through the global method:

Connie(':names.male :names.last')

Extending

You can extend Dictionaries in two ways:

Word Lists

This will ensure that calling the word type a line is randomly picked from word list file

You can provide word list files inside folder registered with connie:

  • by putting the word_name file into the dictionary_name folder

  • by creating a dictionary_name.word_name

Methods in the DictionaryName module

Anywhere in your code by adding instance methods to the Connie::DictionaryName module

It’s handy to aggregate different words from the lists and present the in different ways:

# e.g. Extending the names dictionary to return a full name
module Connie
  module Names
    # Returns a full name
    def full
      "#{first} #{last}"
    end    
  end
end

To add a folder to Connie’s sources just do something like this in your code:

Connie.dictionaries_path << 'my/cool/dictionaries'

What next

It’d be nice to have an structured dictionary or a weighted one, where some options can occur more often than others (Eg not as many ‘Count’ as there are ‘Dr.’ or ‘Mr.’)

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Leandro Pedroni. See LICENSE for details.