Created
September 26, 2015 02:19
-
-
Save sskirby/1b7c5a8510b392d21d89 to your computer and use it in GitHub Desktop.
Proposed API for Vorpal v0.0.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'vorpal' | |
module TreeRepository | |
extend self | |
# The engine is basically the same as the old repository (some methods have been renamed, see below) | |
# but it is not intended to be used directly. | |
engine = Vorpal.define do | |
map Tree do | |
attributes :name | |
belongs_to :gardener, owned: false | |
has_many :branches | |
end | |
map Gardener, to: Gardener | |
map Branch do | |
attributes :length, :diameter | |
belongs_to :tree | |
end | |
end | |
# Now you can also create a 'mapper' which is like the old repository, but you don't have | |
# to specify the Aggregate Root all the time. | |
@mapper = engine.mapper_for(Tree) | |
def find(tree_id) | |
# #load and #load_all have been replaced with #load_one and #load_many. | |
# The new methods also take AR:Base objects instead of ids. | |
@mapper.load_one(@mapper.db_class.where(id: tree_id).first) | |
end | |
# Like #find, but using a special AREL object with Vorpal #load_* methods. | |
def find_again(tree_id) | |
@mapper.query.where(id: tree_id).load_one | |
end | |
def save(tree) | |
# persist can take a single object or an Array. | |
@mapper.persist(tree) | |
end | |
def destroy(tree) | |
# destroy can take a single object or an Array. | |
@mapper.destroy(tree) | |
end | |
def destroy_by_id(tree_id) | |
# destroy_by_id can take a single id or an Array. | |
@mapper.destroy_by_id(tree_id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment