Skip to content

Instantly share code, notes, and snippets.

@jodosha
Last active October 13, 2016 10:33
Show Gist options
  • Save jodosha/11211048 to your computer and use it in GitHub Desktop.
Save jodosha/11211048 to your computer and use it in GitHub Desktop.
Full working example of Lotus::Model
require 'bundler/setup'
require 'lotus/model'
require 'lotus/model/adapters/sql_adapter'
require 'sqlite3'
connection_uri = "sqlite://#{ __dir__ }/test.db"
database = Sequel.connect(connection_uri)
database.create_table! :articles do
primary_key :id
String :title
Integer :comments_count, default: 0
Boolean :published, default: false
end
class Article
include Lotus::Entity
self.attributes = :title, :comments_count, :published # id is implicit
def published?
!!published
end
def publish!
@published = true
end
end
class ArticleRepository
include Lotus::Repository
def self.published
query do
where(published: true)
end
end
def self.drafts
exclude published
end
def self.rank
published.desc(:comments_count)
end
def self.best_article_ever
rank.limit(1)
end
def self.comments_average
query.average(:comments_count)
end
end
Lotus::Model.configure do
adapter name: :application, type: :sql, uri: connection_uri
mapping do
collection :authors do # this must match an existing table name
entity Author
repository AuthorRepository
attribute :id, Integer
attribute :name, String
end
collection :articles do
entity Article
repository ArticleRepository
attribute :id, Integer
attribute :title, String
attribute :comments_count, Integer
attribute :published, Boolean
end
end
end
Lotus::Model.load!
articles = [
Article.new(title: 'Announcing Lotus', comments_count: 123, published: true),
Article.new(title: 'Introducing Lotus::Router', comments_count: 63, published: true),
Article.new(title: 'Introducing Lotus::Controller', comments_count: 82, published: true),
Article.new(title: 'Introducing Lotus::Model')
]
articles.each do |article|
ArticleRepository.create(article)
end
puts "========"
puts "QUERYING"
puts "========\n"
puts "\nfirst, last"
puts ArticleRepository.first.inspect
puts ArticleRepository.last.inspect
puts "\npublished"
puts ArticleRepository.published
puts "\ndrafts"
puts ArticleRepository.drafts
puts "\nmost commented articles"
puts ArticleRepository.rank
puts "\nbest article ever"
puts ArticleRepository.best_article_ever
puts "\ncomments average"
puts ArticleRepository.comments_average
puts
puts "=========="
puts "PERSISTING"
puts "==========\n"
article = articles.last
puts "\npublishing an article\n\n"
puts "title: #{ article.title }"
puts "published: #{ article.published? }\n\n"
puts "publishing.."
article.publish!
puts "published: #{ article.published? }\n" # not yet persisted
puts "persisting.."
ArticleRepository.update(article)
puts "drafts count: #{ ArticleRepository.drafts.count } "
@Partyschaum
Copy link

👍

@liamdawson
Copy link

I just went to copy/paste, and this code still references Lotus! 😨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment