Cero
Cero contains building blocks for constructing ruby applications.
Architecture
An Entitiy is little more than a dumb data structure. The only thing unique about it is that an Entity must have an identity field - usually an :id attribute. This is used for comparisons and persistance.
A Repo is a class that can be used to mapulate collections of Entities. It knows nothing about persistance. Its interface contains the following methods:
- create(entity)
- find(id)
- update(entity)
- delete(entity)
- save(entity)
- all
- first
- last
- count
- query(&block)
- clear
A RepoQuery is an interface for constucting complex queries against data in a Repo.
An Collection is a structural mapping that hooks all parts of the System together. It assigns an Adapter to an Entity and a Repo, and sets up fields for type coercians and the peristance layer. For instance
Cero::Collections.setup do
collection :users do
entity User
adapter :sql
repo UserRepo
fields do
identity :uuid, String
attribute :username, String
attribute :email, String
end
end
collection :user_stats do
entity UserStat
adapter :redis
repo UserStatRepo
fields do
identity :id, Integer
attribute :user_id, Integer
attribute :last_login_at, DateTime
attribute :site_visits_count, Integer
attribute :karma, Float
end
end
end
Usage
class User
include Cero::Entity
attribute :username, String
attribute :email, String
attribute :confirmed, Boolean
end
class UserRepo
include Cero::Repo
def self.find_all_confirmed
query do
where(confirmed: true)
end
end
end
user = User.new(email: "[email protected]", username: "bob")
UserRepo.create(user)
puts user.id # => 1
UserRepo.find(1) # => returns <User @email="[email protected]" @username="bob">
UserRepo.find_all_having_confirmed_of(true)
end
Contributing
- Fork it ( https://github.com/[my-github-username]/cero/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request