Skip to content

Instantly share code, notes, and snippets.

View jeremyevans's full-sized avatar

Jeremy Evans jeremyevans

View GitHub Profile
@jeremyevans
jeremyevans / sequel_model_without_a_table.rb
Created October 10, 2012 21:17 — forked from jescalante/sequel_model_without_a_table.rb
Trying to use a sequel model without a table
# in sequel/plugins/foo.rb
module Sequel::Plugin::Foo
def self.apply(model)
model.plugin :timestamps, :create => :created_on, :update => :updated_on
model.plugin :validation_helpers
model.many_to_one :user
end
module InstanceMethods
# Note that it's probably a bad idea to override initialize unless you
Calculating -------------------------------------
AR new 1110 i/100ms
AR new args 479 i/100ms
AR create 57 i/100ms
AR find 131 i/100ms
-------------------------------------------------
AR new 13570.5 (±5.4%) i/s - 67710 in 5.003359s
AR new args 5271.7 (±6.5%) i/s - 26345 in 5.018283s
AR create 576.4 (±6.2%) i/s - 2907 in 5.062781s
AR find 1357.2 (±4.2%) i/s - 6812 in 5.027665s
one: {:name=>"Acme Enterprises, Inc.", :commplanname=>"All Transactions", :policynum=>"21IL909GL341234", :class=>"WORK", :billingtype=>"D", :term=>"A", :territory=>nil, :location=>1, :effective=>2008-10-05 00:00:00 -0500, :expires=>2009-10-04 00:00:00 -0500, :cancelled=>false, :decverified=>nil, :totprem=>#<BigDecimal:1d2da07,'0.1492E4',4(12)>, :policyfee=>#<BigDecimal:1e80cc2,'0.0',1(4)>, :adminfee=>#<BigDecimal:19db869,'0.0',1(4)>, :tax=>#<BigDecimal:1b5f860,'0.0',1(4)>, :otherfee=>#<BigDecimal:1c5822c,'0.0',1(4)>}
two: Acme Enterprises, Inc.
@jeremyevans
jeremyevans / record.rb
Created March 31, 2012 19:20 — forked from leucos/record.rb
An attempt with Sequel's composition plugin
# In-memory test database
# The schema has been simplified for the discussion (yes, it should have some domain_id somewhere !)
#
# Values in the content field for a SOA, are, in order :
# ns email serial refresh retry expiry minimum
DB = Sequel.sqlite
DB.run "CREATE TABLE records (name VARCHAR(255) PRIMARY KEY NOT NULL, type VARCHAR(5), content VARCHAR(255))"
DB.run "INSERT INTO records VALUES ('example.com', 'SOA', 'ns.example.com root.example.com 2012333335 28800 86400 3600000 86400')"
# Just an assert function for basic testing
@jeremyevans
jeremyevans / non_persistent_test.rb
Created March 1, 2012 16:39 — forked from nelsnelson/non_persistent_test.rb
Testing non-persistent fields on persisted identity-mapped entity instances in JRuby and Sequel through jdbc-postgres
#! /usr/bin/env jruby
require 'rubygems'
require 'sequel'
DB = Sequel.sqlite
DB.create_table? :test do
primary_key :id
foreign_key :parent_id, :test, :on_delete => :set_null
module Sequel
module Plugins
module OracleSequence
def self.configure(model, seq=nil)
model.sequence = seq || model.dataset.opts[:sequence]
model.dataset = model.dataset.sequence(nil) if model.dataset.opts[:sequence]
end
module ClassMethods
attr_accessor :sequence
end
#! /usr/bin/env ruby
require 'rubygems'
require 'sequel'
# Setup schema correctly
TestDatabase.down
TestDatabase.up
TestSetup.down
<%
BlogPost.select(:headline, 'substring(body from "^.*?</p>")'.as(:body_preview)).
order(:post_date.desc).all do |post|
%>
<div>
<h2><%=post.headline%></h2>
<%==post[:body_preview]%>
</div>
<% end %>
filter(:name => ["company_importer_created", "manual_txn_created"]).
filter("day_num >= ?", day_num).
filter("year_num >= ?", year_num).
filter(:engagement_type_str => "Initial").
class User < Sequel::Model
one_to_many :posts
many_to_many :tags, :right_key=>:id, :right_primary_key=>:post_id, :join_table=>:posts
end