Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First commit

  • Loading branch information...
commit e48f0620b06ae5cd038ea7d2e88e33dfbec6e097 0 parents
Josh Graham and Durran Jordan authored Pairing Workstation Sears committed
27 .watchr
@@ -0,0 +1,27 @@
+# ~/.vim/ftdetect/watchr.vim
+#
+# This should have only the following line in it:
+#
+# autocmd BufNewFile,BufRead *.watchr setf ruby
+#
+# This will enable vim to recognize this file as ruby code should you wish to
+# edit it.
+def run(cmd)
+ puts cmd
+ system cmd
+end
+
+def spec(file)
+ run "spec -O spec/spec.opts #{file}"
+end
+
+watch("spec/.*/*_spec\.rb") do |match|
+ p match[0]
+ spec(match[0])
+end
+
+watch("lib/(.*/.*)\.rb") do |match|
+ p match[1]
+ spec("spec/#{match[1]}_spec.rb")
+end
+
0  README
No changes.
11 Rakefile
@@ -0,0 +1,11 @@
+# Add your own tasks in files placed in lib/tasks ending in .rake,
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
+
+require(File.join(File.dirname(__FILE__), 'config', 'boot'))
+
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+Rake::Task[:default].clear
+task :default => [:spec]
45 lib/example_squeal.rb
@@ -0,0 +1,45 @@
+require 'squealer'
+
+import_db('pharmmd_production')
+export_db('pharmmd_reporting_export')
+
+Squealer.import("patients").find({}).each do |patient|
+ target(:patient, patient.id) do |_target| # insert or update on patient where id is primary key column name
+ _target.assign(:name) { patient[first_name] + " " + patient[last_name].upcase }
+ _target.assign(:dob) { patient[date_of_birth] }
+ _target.assign(:latest_drug) { patient.medications.last.name } # dubious
+
+ patient.medications.each do |med|
+ target(:medication, med.id) do |_target|
+ _target.assign(:patient_id) { patient.id }
+ _target.assign(:name) { med.name }
+ end
+
+ med.prescriptions.each do |rx|
+ target(:prescription, rx.id) do |_target|
+ _target.assign(:patient_id) { patient.id }
+ _target.assign(:medication_id) { med.id }
+ _target.assign(:dispense_date) { rx.dispense_date }
+ end
+ end
+ end
+
+ end
+ end
+end
+
+Organization.collection.find({}).each do |organization|
+ if organization.disabled
+ Patient.collection.find({ :organization_id => organization.id }) do |patient|
+ target(:patient, patient.id) do
+ assign(:disabled) { true }
+ end
+ end
+
+ target :organization do
+ end
+
+ else
+ # something else
+ end
+end
1  lib/squealer.rb
@@ -0,0 +1 @@
+require 'squealer/target'
57 lib/squealer/target.rb
@@ -0,0 +1,57 @@
+class Target
+ def initialize(database_connection, table_name, row_id, &block)
+ throw "Block must be given to target (otherwise, there's no work to do)" unless block_given?
+
+ @table_name = table_name.to_s
+ @row_id = row_id
+ @column_names = []
+ @column_values = []
+ @sql = ''
+
+ target(&block)
+ end
+
+ def sql
+ @sql
+ end
+
+ def target(&block)
+ yield(self)
+
+ @sql = "INSERT #{@table_name}"
+ @sql << " (#{pk_name}#{column_names}) VALUES (#{@row_id}#{column_values})"
+ @sql << " ON DUPLICATE KEY UPDATE #{columns}"
+
+ # execute @sql
+ end
+
+ def assign(column_name, &block)
+ @column_names << column_name
+ @column_values << block.call
+ end
+
+ private
+
+ def pk_name
+ 'id'
+ end
+
+ def column_names
+ return if @column_names.size == 0
+ ",#{@column_names.join(',')}"
+ end
+
+ def column_values
+ return if @column_names.size == 0
+ result = ","
+ @column_values.each {|v| result << "'#{v}'," }
+ result.chop
+ end
+
+ def columns
+ return if @column_names.size == 0
+ result = ""
+ @column_names.each_with_index {|k,i| result << "#{k}='#{@column_values[i]}'," }
+ result.chop
+ end
+end
3  spec/spec.opts
@@ -0,0 +1,3 @@
+--colour
+--format nested
+--drb
7 spec/spec_helper.rb
@@ -0,0 +1,7 @@
+require 'rubygems'
+require 'spork'
+
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+require 'squealer'
92 spec/squealer/target_spec.rb
@@ -0,0 +1,92 @@
+require 'spec_helper'
+
+describe Target do
+
+ describe "#target" do
+
+ let(:export_dbc) { nil }
+ let(:table_name) { :test_table }
+ let(:row_id) { 0 }
+ let(:target) { Target.new(export_dbc, table_name, row_id) { nil } }
+
+ it "targets the table" do
+ target.sql.should =~ /^INSERT #{table_name} /
+ end
+
+ it "uses an INSERT ... ON DUPLICATE KEY UPDATE statement" do
+ target.sql.should =~ /^INSERT .* ON DUPLICATE KEY UPDATE /
+ end
+
+ it "includes the primary key name in the INSERT" do
+ target.sql.should =~ / \(id\) VALUES/
+ end
+
+ it "includes the primary key value in the INSERT" do
+ target.sql.should =~ / VALUES \(#{row_id}\) /
+ end
+
+ it "yields inner blocks" do
+ block_done = false
+ target.target { block_done = true }
+ block_done.should be_true
+ end
+
+ it "yields inner blocks first" do
+ Target.new(export_dbc, table_name, row_id) { |target| target.sql.should be_empty }
+ end
+
+ it "yields inner blocks first and they can assign to this target" do
+ target = Target.new(export_dbc, table_name, row_id) { |target| target.assign(:colA) { 42 } }
+ target.sql.should =~ /colA/
+ target.sql.should =~ /42/
+ end
+
+ context "with 2 columns" do
+
+ let(:value_1) { 42 }
+ let(:target) do
+ Target.new(export_dbc, table_name, row_id) { |target| target.assign(:colA) { value_1 } }
+ end
+
+ it "includes the column name in the INSERT" do
+ target.sql.should =~ /\(id,colA\) VALUES/
+ end
+
+ it "includes the column value in the INSERT" do
+ target.sql.should =~ /VALUES \(#{row_id},'#{value_1}'\)/
+ end
+
+ it "includes the column name and value in the UPDATE" do
+ target.sql.should =~ /UPDATE colA='#{value_1}'/
+ end
+
+ end
+
+ context "with 3 columns" do
+
+ let(:value_1) { 42 }
+ let(:value_2) { 'foobar' }
+ let(:target) do
+ Target.new(export_dbc, table_name, row_id) do |target|
+ target.assign(:colA) { value_1 }
+ target.assign(:colB) { value_2 }
+ end
+ end
+
+ it "includes the column names in the INSERT" do
+ target.sql.should =~ /\(id,colA,colB\) VALUES/
+ end
+
+ it "includes the column values in the INSERT" do
+ target.sql.should =~ /VALUES \(#{row_id},'#{value_1}','#{value_2}'\)/
+ end
+
+ it "includes the column names and values in the UPDATE" do
+ target.sql.should =~ /UPDATE colA='#{value_1}',colB='#{value_2}'/
+ end
+
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.