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 'dm-core' | |
require 'dm-migrations' | |
class Foo | |
include DataMapper::Resource | |
property :id, Serial | |
has n, :bars | |
end | |
class Bar |
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 m | |
yield :a | |
end | |
def n | |
yield :a, 1 | |
end | |
m { |x| p x } | |
n { |x| p x } |
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
sasha:rubinius2.0 brian$ irb | |
ruby-1.9.2-p180 :001 > def m | |
ruby-1.9.2-p180 :002?> yield [1,2,3] | |
ruby-1.9.2-p180 :003?> end | |
=> nil | |
ruby-1.9.2-p180 :004 > m { |a, b| p a, b } | |
1 | |
2 | |
=> [1, 2] | |
ruby-1.9.2-p180 :005 > m { |a| p a } |
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 m | |
yield :a | |
end | |
def n | |
yield :a, 1 | |
end | |
m { |x| p x } | |
n { |x| p x } |
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
ast = "def foo(a) p a end; foo(1)".to_ast | |
root = Rubinius::AST::Script.new ast | |
root.file = "(heckle)" | |
compiler = Rubinius::Compiler.new :bytecode, :compiled_method | |
compiler.generator.input root | |
cm = compiler.run | |
script = cm.create_script | |
Rubinius.run_script script.compiled_method |
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
# gem install datamapper dm-sqlite-adapter sunspot sunspot_rails activesupport | |
require 'rubygems' | |
require 'datamapper' | |
require 'sunspot' | |
require 'sunspot/rails' | |
require 'active_support/all' | |
DataMapper::Logger.new($stdout, :debug) | |
DataMapper.setup(:default, 'sqlite::memory:') |
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
# The goal is to not use the regular Symbol#to_proc | |
# (which is to_proc method in the Symbol class) | |
# but rather use an Abstract Syntax Tree transformation to | |
# transform map(&:to_s) into map {|x| x.to_s} | |
# This can be achieved by using the flexible compiler architecture | |
# in Rubinius and is presented below. | |
# | |
# First we undefine the regular Symbol#to_proc | |
class Symbol |
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
-- Retrieve descendants | |
-- ==================== | |
-- retrieve descendants of #4 | |
SELECT c.* | |
FROM Comments AS c | |
JOIN TreePaths AS t ON c.comment_id = t.descendant | |
WHERE t.ancestor = 4; | |
-- Retrieve ancestors |
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 QueryTraceLogger | |
def initialize(logger) | |
@logger = logger | |
end | |
def method_missing(method_name, *args) | |
if method_name == :debug | |
if args.first =~ /SQL/ | |
Rails.logger.debug "" | |
Rails.backtrace_cleaner.clean(caller).each do |line| |
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
# Yields the given models wrapped in Loader::Table objects that can be | |
# used to write SQL. The block should return an array with the SQL as | |
# the first element, and variable substitutions as the rest. | |
# | |
# Example | |
# using_sql(Article) {|a| | |
# ["SELECT #{a.*} FROM #{a} WHERE #{a.id} = ?", 1] | |
# } | |
def using_sql(*models) | |
opts = models.extract_options! |