This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Figured out by Craig on the macruby-devel mailing list | |
framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/DictionaryServices.framework' | |
word = 'History' | |
word_len = DCSGetTermRangeInString(nil, word, 0); | |
result = DCSCopyTextDefinition(nil, word, word_len) | |
puts "Definition for: #{word}" | |
puts result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Search < ActiveRecord::Base | |
# We want to reference various models | |
belongs_to :searchable, :polymorphic => true | |
# Wish we could eliminate n + 1 query problems, | |
# but we can't include polymorphic models when | |
# using scopes to search in Rails 3 | |
# default_scope :include => :searchable | |
# Search.new('query') to search for 'query' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'texticle/searchable' | |
class Book | |
# :title, String | |
# :author, String | |
extend Searchable(:title) | |
end | |
Book.create :title => "Poignant Guide to Ruby", :author => "_why" |