Sorm

Ruby ORM with sdbm as a data storage

Build Status Code Climate Coverage Status Dependency Status Gem Version

SORM

Ruby ORM with sdbm as a data storage

Installation

Add this line to your application’s Gemfile:

gem 'sorm'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sorm

Configuration

First of all, require it in your source code (if you are not using Rails)

require 'sorm'
SORM.configure do |config|
  config.storage_config = {
    database: "/tmp/path_to_your_db"
  }
end

Definition

class Person < SORM::Model
  attribute :first_name
  attribute :last_name
end

Basic usage

p = Person.new(first_name: "Krokodil", last_name: "Gena")
=> #<Person:instance @first_name="Krokodil", @last_name="Gena", @sorm_id=nil>

p.save
=> true

Person.create(first_name: "Krokodil")
=> #<Person:instance @first_name="Krokodil", @last_name=nil, @sorm_id="some-long-generated-id">

Validatation

class Person < SORM::Model
  attribute :name

  validate :name, presence: true
end

Person.new.valid?
=> false

Person.new.errors
=> { name: "Can't be blank" }
class Person < SORM::Model
  attribute :name

  validate :name, uniq: true
end

Person.create(name: "Gena")
=> true
Person.create(name: "Gena")
=> false
Person.new(name: "Gena").valid?
=> false
Person.new(name: "Gena").errors
=> { name: "Should be uniq" }
class Person < SORM::Model
  attribute :name

  validate :name do |record|
    record.name =~ "Gena"
  end
end

Person.create(name: "Gena")
=> true
Person.create(name: "Vasya")
=> false
Person.new(name: "Vasya").valid?
=> false
Person.new(name: "Vasya").errors
=> { name: "Validation block returns false-value" }

Associations

class User < SORM::Model
  has_one :profile, class: "Profile"
end

class Profile < SORM::Model
  belongs_to :user, class: "User"
end

u = User.create
=> #<User:instance @sorm_id="some-user-id">
Profile.create(user_id: u.sorm_id)
=> #<Profile:instance @sorm_id="some-profile-id", @user_id="some-user-id">
u.profile
=> #<Profile:instance @sorm_id="some-profile-id", @user_id="some-user-id">
class Profile < SORM::Model
  has_many :companies, class: "Company"
end

class Company < SORM::Model
  belongs_to :profile, class: "Profile"
end

p = Profile.create
=> #<Profile:instance @sorm_id="some-profile-id">
Company.create(profile_id: p.sorm_id)
=> #<Company:instance @sorm_id="some-company-id", @profile_id="some-profile-id">
Company.create(profile_id: p.sorm_id)
=> #<Company:instance @sorm_id="some-company-id2", @profile_id="some-profile-id">
p.companies
=> [
  #<Company:instance @sorm_id="some-company-id", @profile_id="some-profile-id">,
  #<Company:instance @sorm_id="some-company-id2", @profile_id="some-profile-id">
]

API

# class-methods
Profile.first
=> #<Profile:instance ... >
Profile.last
=> #<Profile:instance ... >
Profile.find("some-sorm-id")
=> #<Profile:instance @sorm_id="some-sorm-id" ... >
Profile.where(name: "Gena")
=> [#<Profile:instance @name="Gena" ...>, #<Profile:instance @name="Gena" ... >]
Profile.count
=> 10

# instance-methods
p = Profile.create(name: "Gena")
=> #<Profile:instance @name="Gena", @sorm_id="some-generated-id">

p.update(name: "Vasya")
=> #<Profile:instance @name="Vasya", @sorm_id="some-generated-id">

Documentation

Your can also check documentation for SORM.

Testing

SORM has 100% coverage for 1.9.2, 1.9.3, 2.0.0 and rbx (you can check Travis) It doesn’t work on jruby (because jruby don’t have sdbm)

Dependencies

SORM has no dependencies. It was written on pure Ruby.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request