Permalink
Browse files

Set up Article entity, repository and mapping

  • Loading branch information...
avdgaag committed Mar 24, 2015
1 parent 64f0e7e commit 7cd9b2197c589c975da86c47be5afd195683c7e8
View
@@ -93,12 +93,12 @@ class Application < Lotus::Application
# 'postgres://localhost/bookshelf'
# 'mysql://localhost/bookshelf'
#
# adapter type: :file_system, uri: ENV['WEB_DATABASE_URL']
adapter type: :sql, uri: ENV['DEMO_DATABASE_URL']
# Configure a database mapping
# See: http://www.rubydoc.info/gems/lotus-model#Data_Mapper
#
# mapping 'config/mapping'
mapping "#{__dir__}/../../lib/config/mapping"
##
# TEMPLATES
View
@@ -1,7 +1,9 @@
# collection :users do
# entity User
# repository UserRepository
#
# attribute :id, Integer
# attribute :name, String
# end
collection :articles do
entity Demo::Article
repository Demo::ArticleRepository
attribute :id, Integer
attribute :title, String
attribute :body, String
attribute :created_at, Time
end
@@ -0,0 +1,6 @@
module Demo
class Article
include Lotus::Entity
attributes :id, :title, :body, :created_at
end
end
@@ -0,0 +1,5 @@
module Demo
class ArticleRepository
include Lotus::Repository
end
end
@@ -0,0 +1,23 @@
require_relative '../../spec_helper'
module Demo
describe Article do
it 'is a Lotus::Entity' do
assert_kind_of Lotus::Entity, Article.new
end
it 'can set attribute through the initializer' do
timestamp = Time.now
article = Article.new(
id: 1,
title: 'New article',
body: 'Lorem ipsum',
created_at: timestamp
)
assert_equal 1, article.id
assert_equal 'New article', article.title
assert_equal 'Lorem ipsum', article.body
assert_equal timestamp, article.created_at
end
end
end
@@ -0,0 +1,24 @@
require_relative '../../spec_helper'
module Demo
describe ArticleRepository do
before do
ArticleRepository.clear
end
it 'is a Lotus::Repository' do
assert_kind_of Lotus::Repository, ArticleRepository.new
end
it 'can load all articles into an array of Article entities' do
3.times do |i|
ArticleRepository.create(
Article.new(title: "Article #{i}", body: 'Lorem Ipsum')
)
end
articles = ArticleRepository.all
assert_equal 3, articles.size
assert_kind_of Article, articles.first
end
end
end

0 comments on commit 7cd9b21

Please sign in to comment.