This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.rb
76 lines (56 loc) · 1.5 KB
/
example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# irb -r locomotive/models -I ./lib
require 'pry'
require_relative 'lib/locomotive/adapters/memory_adapter'
adapter = Locomotive::Adapters::MemoryAdapter
locale = :en
class Article
include Locomotive::Entity
attributes :title, :author, :comments
end
class Author
include Locomotive::Entity
attributes :name
end
class Comment
include Locomotive::Entity
attributes :title
end
class ArticlesRepository
include Locomotive::Repository
end
class AuthorsRepository
include Locomotive::Repository
end
class CommentsRepository
include Locomotive::Repository
end
mapper = Locomotive::Mapper.new(adapter) do
collection :articles do
entity Article
repository ArticlesRepository
attribute :title, localized: true
attribute :author, association: :authors
attribute :comments, association: :comments
end
collection :authors do
entity Author
repository AuthorsRepository
attribute :name
end
collection :comments do
entity Comment
repository CommentsRepository
attribute :title
end
end
articles_repository = Locomotive::Models[:articles]
comments_repository = Locomotive::Models[:comments]
author = Author.new(name: 'John')
authors_repository.create author
comment = Comment.new(title: 'New Comment')
comments_repository.create comment
article = Article.new(title: {en:"Title #{rand(100_000)}"}, author: author, comments: [comment])
articles_repository.create article
my_article = articles_repository.find my_article.id
my_article.author
my_article.comments