Kashia is proud to present you this little tip:

Enchanting an Og Model

Tags: 0.40

This tip aims to define all methods to enchant a model (make it persistant).

Enchanting: Add the needed features like .find, the manager .ogmanager which contains the database connection pool, creates the database and adds a primary key called oid when nothing else is specified.

The first and easiest method:

implicit declaration.

class User
  attr_accessor :name, String
end

When, and only when, one or more symbols are followed by a Class, the User class is regarded as a Og Model.

All implicit declaration keywords:

  • attr_accessor
  • property (alias of attr_accessor)
  • attr_reader
  • attr_writer

Subclassing

class User < Og::Entity
  
end

This class will be enchanted on creation, regardless on what you write in there.

Mixin

class User
  include Og::EntityMixin
end

This is analog to the subclassing, it will enchant your model as well.

Use library

class User
  is Taggable
end

This also makes the User class managed because Taggable depends on a database and sets everything up for you.

The is keyword is provided by Facets and is the same as using include.

Other standard libraries you can include to get the class managed:

  • Taggable
  • Glue::Timestamped
  • Glue::TimestampedOnCreate
  • Glue::Reviseable
  • Glue::Orderable
  • Glue::Locking
  • Glue::NestedSets
  • Glue::Hierarchical