Skip to content

Instantly share code, notes, and snippets.

@denyago
Created August 22, 2012 17:37
Show Gist options
  • Save denyago/3427814 to your computer and use it in GitHub Desktop.
Save denyago/3427814 to your computer and use it in GitHub Desktop.
source :rubygems
gem 'pg'
gem 'activesupport'
gem "debugger"
# DM2 staff
gem 'dm-mapper', git: "https://github.com/solnic/dm-mapper.git"
gem 'veritas', git: 'https://github.com/solnic/veritas.git'
gem 'veritas-sql-generator', git: 'https://github.com/solnic/veritas-sql-generator.git', branch: "string-concat-function-support"
gem 'veritas-optimizer', git: 'https://github.com/dkubb/veritas-optimizer.git'
gem 'veritas-do-adapter', git: 'https://github.com/dkubb/veritas-do-adapter.git'
gem 'virtus', git: 'https://github.com/solnic/virtus.git'
gem 'do_postgres'
require 'bundler/setup'
Bundler.require :default
require 'veritas'
require 'veritas/optimizer'
require 'veritas-do-adapter'
require 'virtus'
require 'do_postgres'
require 'dm-mapper'
require 'debugger'
url = "postgres://rails:rails@localhost:5432/ar_dm2"
DataMapper.setup(:postgres, url)
DataObjects.logger.set_log('do.log', :debug)
connection = DataObjects::Connection.new(url)
connection.create_command(<<-SQL).execute_non_query
DROP TABLE IF EXISTS "public"."users";
DROP SEQUENCE IF EXISTS "users_id_seq";
CREATE SEQUENCE "users_id_seq" INCREMENT 1 START 1 MAXVALUE 9223372036854775807 MINVALUE 1 CACHE 1;
ALTER TABLE "users_id_seq" OWNER TO "rails";
CREATE TABLE "public"."users" (
"id" int4 NOT NULL DEFAULT nextval('users_id_seq'::regclass),
"name" varchar(255) NOT NULL
)
WITH (OIDS=FALSE);
ALTER TABLE "public"."users" OWNER TO "rails";
insert into "public"."users" ( "name") values ( 'octocat');
insert into "public"."users" ( "name") values ( 'dhh');
SQL
class User
attr_accessor :likes, :id, :name
def initialize(attributes={})
@id = attributes[:id]
@name = attributes[:name]
@likes = attributes[:likes]
end
end
class Mapper < DataMapper::Mapper::VeritasMapper
map :id, to: :id, :type => Integer
map :name, to: :name, :type => String
model User
relation_name :users
repository :postgres
end
DataMapper.finalize
profiles_json = [
{"profile"=>{"user_id"=>1, "likes"=>"meat, kats"}},
{"profile"=>{"user_id"=>2, "likes"=>"ruby, mac"}},
{"profile"=>{"user_id"=>3, "likes"=>"be invisible"}}
]
profiles_header = [
[:user_id, Integer],
[:likes, String ]
]
profiles_tuple = profiles_json.map do |profile|
[ profile['profile']['user_id'], profile['profile']['likes']]
end
profiles_relation = Veritas::Relation::Base.new('profiles', profiles_header, profiles_tuple)
puts "Example from gist"
adapter = Veritas::Adapter::DataObjects.new(url)
users_header = [ [ :id, Integer ], [ :name, String ] ]
user_alt_relation = Veritas::Relation::Gateway.new(
adapter, Veritas::Relation::Base.new('users', users_header))
new_alt_relation = user_alt_relation.join(profiles_relation) do |r|
r.id.eq(r.user_id)
end
new_alt_relation.each do |user|
puts "User #{user[:id]} named #{user[:name]} likes #{user[:likes]}"
end
puts "Use Ruby object"
user_relation = DataMapper[User]
new_relation = user_relation.join(profiles_relation) do |r|
puts "Joining id=#{r.id} and user_id=#{r.user_id}"
r.id.eq(r.user_id)
end
new_relation.each do |user|
puts "User #{user.id} named #{user.name} likes #{user.likes}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment