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

I'm trying to get this example running but it fails with uninitialized constant Lotus::Model::Mapping::Coercions (NameError). I'm using ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0] via rbenv.

My Gemfile looks like this:

source 'https://rubygems.org'

gem 'lotus-model'
gem 'sqlite3'

When running it with bundle exec ruby lotus_model.rb I get this error output:

eval):12:in `from_record': uninitialized constant Lotus::Model::Mapping::Coercions (NameError)
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/model/mapping/collection.rb:297:in `block in deserialize'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/dataset/actions.rb:139:in `block in each'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/adapters/sqlite.rb:371:in `block (2 levels) in fetch_rows'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sqlite3-1.3.9/lib/sqlite3/resultset.rb:138:in `each'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/adapters/sqlite.rb:362:in `block in fetch_rows'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:269:in `query'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/adapters/sqlite.rb:179:in `block (2 levels) in _execute'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/database/logging.rb:33:in `log_yield'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/adapters/sqlite.rb:179:in `block in _execute'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/database/connecting.rb:229:in `block in synchronize'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/database/connecting.rb:229:in `synchronize'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/adapters/sqlite.rb:172:in `_execute'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/adapters/sqlite.rb:122:in `execute'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/dataset/actions.rb:906:in `execute'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/adapters/sqlite.rb:356:in `fetch_rows'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/dataset/actions.rb:139:in `each'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/dataset/actions.rb:389:in `map'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/sequel-4.11.0/lib/sequel/dataset/actions.rb:389:in `map'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/model/mapping/collection.rb:296:in `deserialize'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/model/adapters/sql/collection.rb:192:in `to_a'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-utils-0.2.0/lib/lotus/utils/kernel.rb:65:in `Array'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-utils-0.2.0/lib/lotus/utils/kernel.rb:65:in `Array'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/model/adapters/sql/query.rb:75:in `all'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/model/adapters/implementation.rb:98:in `first'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/model/adapters/implementation.rb:98:in `_first'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/model/adapters/implementation.rb:64:in `first'
    from /usr/local/var/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/lotus-model-0.1.1/lib/lotus/repository.rb:455:in `first'
    from lotus_model.rb:88:in `<main>'

@diegosouza
Copy link

@Partyschaum it was just fixed, take a look at the commit.

@jodosha
Copy link
Author

jodosha commented Jun 26, 2014

@Partyschaum @diegosouza Released a new version which includes that fix: https://rubygems.org/gems/lotus-model/versions/0.1.2

@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