YAML Model
A NoSQL data model with data stored as YAML
A pure ruby data modelling library which uses a YAML file for it's backend store.
This library is used for all the data on this very website. All the project information, tags, screenshot IDs etc are stored in a yaml-model backend.
A fully functional wiki demo using YAML Model and Sinatra has it's full source code here.
Similar Projects:
to_xml, Fixed Width Structures, Remarkably
Installation
gem install yaml-model
Usage
A simple example
require 'yaml-model'
YAML_Model.filename = 'my-database.yaml'
Person = Class.new( YAML_Model )
class Post < YAML_Model
type :person, Person
type :at, Time do |value|
raise "Cannot be in the future!" if ( value - 10 > Time.now )
end
type :subject, String
type :public, [ TrueClass, FalseClass ]
init :person, :at, :subject, :public
end
class Person < YAML_Model
type :name, String do |name|
raise "Invalid format for name" unless name =~ /^[A-Z][a-z]+$/
end
init :name
has :posts, Post
end
bob = Person.create( "Bob" )
mary = Person.create( "Mary" )
Post.create( bob, Time.now, "The art of Ruby", true )
Post.create( bob, Time.now, "Beautiful Ruby", true )
Post.create( bob, Time.now, "Secretly I love Mary", false )
Post.create( mary, Time.now, "Bob is awesome!", true )
Post.all.select{|n|n.public}.each do |post|
puts "#{post.person.name}: #{post.subject}"
end
The resulting file will contain the following:
---
:next_oid: 7
:data:
Person:
- &id001 !ruby/object:Person
name: Bob
id: 1
- &id002 !ruby/object:Person
name: Mary
id: 2
Post:
- !ruby/object:Post
person: *id001
at: 2011-01-27 17:26:52.999533 +02:00
subject: The art of Ruby
public: true
id: 3
- !ruby/object:Post
person: *id001
at: 2011-01-27 17:26:53.000594 +02:00
subject: Beautiful Ruby
public: true
id: 4
- !ruby/object:Post
person: *id001
at: 2011-01-27 17:26:53.001604 +02:00
subject: Secretly I love Mary
public: false
id: 5
- !ruby/object:Post
person: *id002
at: 2011-01-27 17:26:53.002546 +02:00
subject: Bob is awesome!
public: true
Source Code
GitHub
git clone git://github.com/clivecrous/yaml-model
cd yaml-model