Skip to content

Instantly share code, notes, and snippets.

View janko's full-sized avatar

Janko Marohnić janko

View GitHub Profile
require "sequel"
DB = Sequel.postgres("arel")
DB.create_table!(:movies) { primary_key :id }
class Movie < Sequel::Model
end
# Asterisk (I agree this one isn't ideal)
Movie.select{count{}.*} # SELECT count(*) FROM "movies"
@janko
janko / 1-activerecord.rb
Last active June 13, 2023 20:00
INSERTing 50,000 records into a database in ActiveRecord, Arel, SQL, activerecord-import and Sequel.
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Migration.class_eval do
create_table(:records) do |t|
t.string :column
end
end
data = 50_000.times.map { |i| Hash[column: "Column #{i}"] }
@janko
janko / 01-safe-download.rb
Last active January 9, 2023 11:40
A safe way in Ruby to download a file to disk using open-uri (with/without comments)
require "open-uri"
require "net/http"
Error = Class.new(StandardError)
DOWNLOAD_ERRORS = [
SocketError,
OpenURI::HTTPError,
RuntimeError,
URI::InvalidURIError,
@janko
janko / arel.rb
Created July 28, 2015 00:09
Insert statement in Arel
require "active_record"
ActiveRecord::Base.establish_connection("postgres:///db")
insert = Arel::Nodes::InsertStatement.new
insert.relation = Arel::Table.new(:movies)
insert.columns = hash.keys.map { |k| Arel::Table.new(:movies)[k] }
insert.values = Arel::Nodes::Values.new(hash.values, insert.columns)
ActiveRecord::Base.connection.execute(insert.to_sql)
@janko
janko / 01-requirements.md
Last active March 6, 2023 11:15
ActiveRecord vs Sequel (require time)
$ brew install cloc
$ bundle install
@janko
janko / 01.md
Created May 27, 2015 23:07
The Roda Ruby framework (my presentation on our local Ruby meetups)

Roda

  • Web framework (on top of Rack)

  • Forked from Cuba

  • Core + Plugins

    • Core: 440 LOC
  • Total: 3200 LOC
@janko
janko / presentation.rb
Created May 27, 2015 23:05
Advanced Regex features in Ruby (my presentation from our local Ruby meetups)
############
# GROUPING #
############
def Given(*) end
Given(/There (is|are) some links?/) { } # ERROR
Given(/There (?:is|are) some links?/) { }
@janko
janko / 01-presentation.md
Last active June 6, 2019 23:58
PostgreSQL full-text search capabilites (my presentation from our local Ruby meetup)

Full-text search

  • Keywords

  • Typos

  • Stemming

  • Stopword ignore

@janko
janko / 01.rb
Created May 27, 2015 22:54
Capabilities of the "mail" gem, without ActionMailer (my presentation from our local Ruby meetups)
require "mail"
Mail.defaults do
delivery_method :smtp,
address: "smtp.gmail.com",
port: 587,
user_name: ENV["GMAIL_EMAIL"],
password: ENV["GMAIL_PASSWORD"],
authentication: "plain",
end
@janko
janko / 01-activerecord.rb
Created May 27, 2015 22:50
PostgreSQL JSON querying in Sequel (my presentation from our local Ruby meetup)
require "active_record"
ActiveRecord::Base.establish_connection('postgres:///testing')
ActiveRecord::Migration.verbose = false
ActiveRecord::Migration.class_eval do
create_table :played_quizzes, force: true do |t|
t.integer :player_ids, array: true
t.json :quiz_snapshot
end