Skip to content

Instantly share code, notes, and snippets.

require 'dm-core'
require 'dm-migrations'
class Foo
include DataMapper::Resource
property :id, Serial
has n, :bars
end
class Bar
def m
yield :a
end
def n
yield :a, 1
end
m { |x| p x }
n { |x| p x }
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 }
def m
yield :a
end
def n
yield :a, 1
end
m { |x| p x }
n { |x| p x }
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
@dkubb
dkubb / dm_sunspot.rb
Created June 16, 2011 18:57
DataMapper and Sunspot Integration Spike
# 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:')
@hosiawak
hosiawak / irb_session.rb
Created June 12, 2011 12:53
Rubinius AST transform by sublassing Melbourne
# 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
@emmanuel
emmanuel / file.sql
Created June 2, 2011 07:54
Closure Table operations SQL fragments
-- 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
@xaviershay
xaviershay / query_trace_logger.rb
Created May 27, 2011 04:18
Ghetto query tracing for DataMapper. Put it in an initializer.
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|
@xaviershay
xaviershay / loader.rb
Created May 17, 2011 07:43
DataMapper hacks to generate raw SQL
# 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!