Skip to content

Instantly share code, notes, and snippets.

View ecin's full-sized avatar
🐶
Woof!

ecin ecin

🐶
Woof!
View GitHub Profile
Running 683 tests...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................OK!
bin/mspec ci --background --agent
rubinius 2.0.0dev (1.8.7 68a5acb5 yyyy-mm-dd JI) [x86_64-apple-darwin10.8.0]
................................................................................................................................................................................................................
class Address
# :street, String
# :city, String
# :state, String
# :country, String
scope :local, lambda { |query| search_by_street_or_city(query, query) }
scope :national, lambda {|query| search_by_state_or_country(query, query) }
end
require 'texticle/searchable'
class Book
# :title, String
# :author, String
extend Searchable(:title)
end
Book.create :title => "Poignant Guide to Ruby", :author => "_why"
def method_missing(name, *terms)
if match = name.to_s.match(/search_by_(?<columns>[_a-zA-Z]\w*)/)
string_columns = self.columns.select {|column| column.type == :string }.map(&:name)
columns = match[:columns].split('_and_')
super unless columns.all? {|column| string_columns.include?(column) }
query = columns.inject({}) do |query, column|
query.merge column => terms.shift
end
search(query)
end
@ecin
ecin / store_credit.rb
Created April 16, 2011 05:04
Going through Google Code Jam problem sets...
module Reader
def read(lines_per_problem = 1)
Enumerator.new do |yielder|
gets.chomp.to_i.times do
yielder.yield *[Array.new(lines_per_problem) { gets.chomp }]
end
end
end
end
<html>
<head>
<title>My First Webpage</title>
<style>
h1.insetType {
font-family: Rockwell, Georgia, "Times New Roman", Times, serif;
font-size: 50px;
color: #0D4383;
text-shadow: rgba(0,0,0,0.5) -1px 0, rgba(0,0,0,0.3) 0 -1px, rgba(255,255,255,0.5) 0 1px, rgba(0,0,0,0.3) -1px -2px;
}
@ecin
ecin / jammit-pre-commit.rb
Created December 6, 2010 22:05
Bundle assets when they change.
#!/usr/bin/env ruby
require 'yaml'
top_dir = `git rev-parse --show-toplevel`.chomp
changes = `git diff --cached --name-only`.chomp.split
assets = YAML.load_file "#{top_dir}/config/assets.yml"
assets = assets['javascripts'].values + assets['stylesheets'].values
assets.flatten!
@ecin
ecin / freckle_reminder.rb
Created December 6, 2010 19:52
I keep committing without specifying how long the commit took, so...
#!/usr/bin/env ruby
commit_msg = File.open(ARGV[0]).read
exit(0) if commit_msg[/f:\d+/]
warn "Missing Freckle time duration in commit message! Aborting commit..."
exit(1)
@ecin
ecin / search.rb
Created November 10, 2010 03:46
For tumblr post.
class Search < ActiveRecord::Base
belongs_to :searchable, :polymorphic => true
# This will actually work now, no more
# n + 1 query problems, yay!
default_scope :include => :searchable
def hash
searchable_id.hash + searchable_type.hash
end
@ecin
ecin / create_searches.rb
Created October 28, 2010 15:04
Migration for Searches view for use with Texticle and search.rb
class CreateSearches < ActiveRecord::Migration
def self.up
ActiveRecord::Base.connection.execute <<-SQL
CREATE VIEW searches AS
SELECT authors.id AS searchable_id, authors.name AS term,
CAST ('Author' AS varchar) AS searchable_type
FROM authors
UNION
SELECT books.id AS searchable_id, books.title AS term,
CAST ('Book' AS varchar) AS searchable_type