Ruby ORM with sdbm as a data storage
Ruby ORM with sdbm as a data storage
Add this line to your application’s Gemfile:
gem 'sorm'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sorm
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
class Person < SORM::Model
attribute :first_name
attribute :last_name
end
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">
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" }
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">
]
# 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">
Your can also check documentation for SORM.
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)
SORM has no dependencies. It was written on pure Ruby.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)