squirm 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -48,11 +48,16 @@ compatible ORM based on stored procedures.
48
48
 
49
49
  ## Using it with Rails
50
50
 
51
- Squirm can use Active Record's connection pool rather than its own, all you have
52
- to do is pass in the pool to `Squirm.connect`:
51
+ Squirm comes with built-in support to make it work seamlessly with Active Record:
53
52
 
54
- # in config/initializers/squirm.rb
55
- Squirm.connect pool: ActiveRecord::Base.connection_pool
53
+ class Person < ActiveRecord::Base
54
+ procedure :say_hello
55
+ end
56
+
57
+ p = Person.find(23)
58
+ p.say_hello
59
+
60
+ More documentation coming soon.
56
61
 
57
62
  ## Author
58
63
 
@@ -70,4 +70,6 @@ module Kernel
70
70
  def Squirm(&block)
71
71
  Squirm.instance_eval(&block)
72
72
  end
73
- end
73
+ end
74
+
75
+ require "squirm/rails" if defined? Rails
@@ -0,0 +1,54 @@
1
+ module Squirm
2
+
3
+ # Support for working with procedures inside Active Record models. This exists
4
+ # primarily to ensure that stored procedure calls are done inside the same
5
+ # connection used by the AR model, to avoid transaction opacity issues that
6
+ # could arise if AR and Squirm are used different connections.
7
+ module ActiveRecord
8
+ class Procedure < ::Squirm::Procedure
9
+ attr_accessor :connector
10
+
11
+ def call(*args, &block)
12
+ Squirm.use(connector.call) do
13
+ super
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.included(model_class)
19
+ model_class.extend ClassMethods
20
+ end
21
+
22
+ module ClassMethods
23
+ def procedure(name, options = {}, &block)
24
+ self.class_eval(<<-EOM, __FILE__, __LINE__ + 1)
25
+ @@__squirm ||= {}
26
+ @@__squirm[:#{name}] = Squirm::ActiveRecord::Procedure.new("#{name}")
27
+ @@__squirm[:#{name}].connector = ->{connection}
28
+ @@__squirm[:#{name}].load
29
+ def #{options[:as] or name}(options = {})
30
+ options[:id] ||= id if @@__squirm[:#{name}].arguments.hash.has_key?(:id)
31
+ @@__squirm[:#{name}].call(options)
32
+ end
33
+ EOM
34
+ end
35
+ end
36
+ end
37
+
38
+ class Railtie < Rails::Railtie
39
+ initializer "squirm.setup" do
40
+ Squirm.connect pool: ::ActiveRecord::Base.connection_pool
41
+ ::ActiveRecord::Base.send :include, Squirm::ActiveRecord
42
+ end
43
+
44
+ rake_tasks do
45
+ load File.expand_path("../rails/squirm.rake", __FILE__)
46
+ end
47
+
48
+ generators do
49
+ require File.expand_path("../rails/generator", __FILE__)
50
+ end
51
+
52
+ end
53
+ end
54
+
@@ -0,0 +1,11 @@
1
+ require "rails/generators"
2
+
3
+ module Squirm
4
+ class Install < ::Rails::Generators::Base
5
+ source_root File.dirname(__FILE__)
6
+ def install_functions
7
+ copy_file "stored_procedures.sql", "db/stored_procedures.sql"
8
+ copy_file "unit_test.rb", "test/unit/stored_procedures_test.rb"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ namespace :db do
2
+ namespace :schema do
3
+ task :load do
4
+ functions = File.read(Rails.root.join("db", "stored_procedures.sql"))
5
+ ActiveRecord::Base.connection.execute functions
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ /*
2
+ This is the stored_procedures.sql file used by Squirm. Define your Postgres
3
+ stored procedures in this file and they will be loaded at the end of any calls
4
+ to the db:schema:load Rake task.
5
+ */
6
+
7
+ CREATE OR REPLACE FUNCTION hello_world() RETURNS TEXT AS $$
8
+ BEGIN
9
+ RETURN 'hello world!';
10
+ END;
11
+ $$ LANGUAGE 'PLPGSQL'
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class StoredProceduresTest < ActiveSupport::TestCase
4
+
5
+ test "hello world should emit a greeting" do
6
+ procedure = Squirm.procedure "hello_world"
7
+ assert_equal "hello world!", procedure.call
8
+ end
9
+
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Squirm
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squirm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-22 00:00:00.000000000 Z
12
+ date: 2011-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70112879161580 !ruby/object:Gem::Requirement
16
+ requirement: &70228168578720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.6'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70112879161580
24
+ version_requirements: *70228168578720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: pg
27
- requirement: &70112879182280 !ruby/object:Gem::Requirement
27
+ requirement: &70228168571760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 0.11.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70112879182280
35
+ version_requirements: *70228168571760
36
36
  description: ! '"Squirm is an anti-ORM for database-loving programmers"'
37
37
  email:
38
38
  - norman@njclarke.com
@@ -50,6 +50,11 @@ files:
50
50
  - lib/squirm/pool.rb
51
51
  - lib/squirm/procedure.rb
52
52
  - lib/squirm/procedure.sql
53
+ - lib/squirm/rails.rb
54
+ - lib/squirm/rails/generator.rb
55
+ - lib/squirm/rails/squirm.rake
56
+ - lib/squirm/rails/stored_procedures.sql
57
+ - lib/squirm/rails/unit_test.rb
53
58
  - lib/squirm/version.rb
54
59
  - spec/core_spec.rb
55
60
  - spec/helper.rb