Project

wormy

0.0
No commit activity in last 3 years
No release in over 3 years
A lightweight Object-Relational Mapping (ORM) library for Ruby. Allows you to keep code DRY and easily perform database operations in an object-oriented manner.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

WORMY 🐛

A lightweight Object-Relational Mapping (ORM) library for Ruby. Allows you to keep code DRY and easily perform database operations in an object-oriented manner.

gem install wormy

Demo

  1. From the demo directory of this repo, open pry or irb in the console
  2. load 'hp_demo.rb'
  3. Use hp_demo.rb and the API section below as a reference to play around with the data

How to Use WORMY

  • Navigate to the folder in your directory where you would like your .db database file to be saved.
  • If you have an existing database.rb file you need to rewrite, run rm database.db
  • Run cat '{YOUR_SQL_FILE_NAME}' | sqlite3 'database.db' (replacing {YOUR_SQL_FILE_NAME} with your own .sql file)
  • Then, in your project, open a connection with DBConnection.open('database.db')

Libraries

  • SQLite3
  • ActiveSupport::Inflector

API

Associations between models are defined by simple class methods, like so:

class Pet < WORMY::Base
  belongs_to :owner,
    class_name: "Wizard"

  has_one_through :house, :owner, :house

  finalize!
end

Querying and updating the database is made easy with WORMY::Base's methods like:

  • ::all
  • ::count
  • ::destroy_all
  • ::find
  • ::first
  • ::last
  • ::where
  • #create
  • #save
  • #destroy

Perform custom model validations by adding a call to validates in your subclass definition:

class House < WORMY::Base
  has_many :wizards
  has_many_through :pets, :wizards, :pets
  validates :house_name

  def house_name
    ["Gryffindor", "Slytherin", "Ravenclaw", "Hufflepuff"].include?(self.name)
  end

  finalize!
end

About WORMY

WORMY opens a connection to a provided database file by instantiating a singleton of SQLite3::Database via DBConnection. DBConnection uses native SQLite3::Database methods (execute, execute2, last_insert_row_id) to allow WORMY to perform complex SQL queries using heredocs. The Searchable and Associatable modules extend WORMY::Base to provide an intuitive API.

WORMY emphasizes convention over configuration by setting sensible defaults for associations, but also allows for easy overrides if desired.