Skip to content

Instantly share code, notes, and snippets.

Artist.select(:id, :name).naked.all
# => [{:id=>1, :name=>"mrbrdo"}]
@mrbrdo
mrbrdo / sequel_eager.rb
Last active August 29, 2015 14:10
sequel eager loading with conditions
artists = Artist.eager(songs: ->(q) { q.where{year > 1990} }).all
# example to illustrate the results
artists.each do |artist|
puts "Artist: #{artist.name}"
puts "Songs since 1990: " + artist.songs.map(&:title).join(", ")
end
def x: Int = { println("x()"); 1 }
def a(byName1: => Int, byName2: => Int): Unit = {
println("a()")
println(byName1 + byName2)
}
a(x * 2, x * 3)
// output (note when x is actually called):
# /config/application.rb
config.middleware.insert_before ActionDispatch::ParamsParser, "SelectiveStack"
# /config/initializers/omniauth.rb
::OmniAuthConfig = Proc.new do
provider :github, # ...
end
# /app/middleware
class SelectiveStack
BlogsDatasource.new(Blog.all)
.select(:id, posts: { scope: Post.all, select: [:id, :author_name] }).results
# (0.1ms) SELECT blogs.id FROM "blogs"
# (0.2ms) SELECT posts.id, posts.blog_id, (posts.author_first_name || ' ' || posts.author_last_name) as author_name FROM "posts" WHERE (posts.blog_id IN (1))
PostsDatasource.new(Post.all)
.select(:id, :title, :author, :author_name).results
# (0.1ms) SELECT posts.id, posts.title, posts.author_first_name, posts.author_last_name, (posts.author_first_name || ' ' || posts.author_last_name) as author_name FROM "posts"
➜ ams_vs_jbuilder git:(master) ✗ rake benchmarks:single_object_with_render
Calculating -------------------------------------
jbuilder 12 i/100ms
active_model_serializers
50 i/100ms
thin_serializer 53 i/100ms
-------------------------------------------------
jbuilder 132.1 (±6.8%) i/s - 660 in 5.028041s
active_model_serializers
524.8 (±13.7%) i/s - 2600 in 5.088049s
class Object
def test1 # instance method!
end
end
Object.test1 # OK
class SomeClass
def test2
end
struct mrb_jmpbuf buf;
MRB_TRY(&buf) {
mrb->jmp = &buf;
}
MRB_CATCH(&buf) {
// handle error
}
MRB_END_EXC(&buf);
struct mrb_jmpbuf buf;
int stoff = mrb->c->stack - mrb->c->stbase;
int cioff = mrb->c->ci - mrb->c->cibase;
MRB_TRY(&buf) {
mrb->jmp = &buf;
}
MRB_CATCH(&buf) {
// if rescued from method that was called from this method
// and didn't have its own rescue
$ gem list | grep sequel
sequel (4.10.0)
$ irb
jruby-1.7.12 :001 > require 'sequel'
=> true
jruby-1.7.12 :002 > Sequel.connect("jdbc:postgresql://localhost/mydb")
=> #<Sequel::JDBC::Database: "jdbc:postgresql://localhost/mydb">
jruby-1.7.12 :003 > class ContentMetric < Sequel::Model
jruby-1.7.12 :004?> end