activerecord-postgresql-extensions 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDVjMzkzNjNhYzc2MzQxNDkyZTA3MWQzM2FlZGZlZmQzYzkxMDkxNA==
4
+ OGY1YjMzZWU1ZmNjN2VmZDU5ODQyM2Q0ZDg3N2JjNjkwZTFkMTkwYQ==
5
5
  data.tar.gz: !binary |-
6
- NjMzOTZjOTFkOGE3NTQyMmU1ODg2NzJhNmQ3MzQ3NjgwNTRkNzZhNQ==
6
+ ZGMxNWRjNjEwNzVhZjU1MzMxYTQ4ZTk0NWU2MjljYzViMDM5YzdjNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzEzN2MzMjg4YjYzZWI5NjIyNWJmNmNjNjc1NmZiMjNkYjZmODM5MjUyN2Iw
10
- NTJjYzBkYzMxZTZjYTk3OTE2ODZhZDZmZjc5YmUxNzY2YmIxMDdjZjRiMzk1
11
- YjRjZjk0NTkxMDdjN2Q5MTRkYmM3YmMxMWU4MDhkZGFhMWMxMDA=
9
+ NWRkMmZiNWViMzE4ZjBhNDEzOWFlNGJkOTIxZjg1MjljM2IyOWVmM2QzODA2
10
+ OGQzOWY2YjRhY2UzMzE5MzBlN2YxYzM3MjZkMTUzYzc4NDM3Zjc3NzliZWZm
11
+ MTc5NDU2NGE2MjY2YjUxYzhiYTI5MTVlNmZiNDFjMmZkOWFlM2U=
12
12
  data.tar.gz: !binary |-
13
- ZTUyOGIwZTc5MjAwMDRlZGE4ZTI0MzI5OTk1YTM1NjM3MTVkZGJlYjA2MTA0
14
- MzczY2U4NTMwNDdhZTU2MmJhNTJmMjBlYjFmZDU1NzExMDI2ZjRhZmVhNzAz
15
- YWNjYmE1NWU1MzkxMDk3ZDMwYzBiMzNkZGNhZjUyMTg5OTAzN2I=
13
+ YjQyYWU2ZDM3MjQ0NmFhNDVmOWI3N2U0MWZkZDM5MTRiNTRhZmFlM2M4N2Jj
14
+ YjdkZTUyODk5ZmFlYmJhNzVhMjdjZDc5YjdmY2QyODMxZmY2ZmIwZTBiNmE5
15
+ YmZkMjE4NmZhNjAzNWI4MjIwOThkNjYzMzg3ZjQyMTQ1ODQ3NTE=
@@ -89,14 +89,22 @@ module ActiveRecord
89
89
  # convoluted as evidenced by the plethora of options we're
90
90
  # handling here.
91
91
  #
92
- # ==== Example
92
+ # ==== Examples
93
+ #
94
+ # # With a string for the body:
95
+ # create_function('tester_function', 'integer',
96
+ # 'integer', 'sql',
97
+ # 'select $1;'
98
+ # :behavior => :immutable, :set => { :search_path => :from_current }, :force => true
99
+ # )
93
100
  #
101
+ # # With a block:
94
102
  # create_function('tester_function', 'integer',
95
103
  # 'integer', 'sql', :behavior => :immutable, :set => { :search_path => :from_current }, :force => true) do
96
104
  # "select $1;"
97
105
  # end
98
106
  #
99
- # # Produces:
107
+ # # Both produce:
100
108
  # #
101
109
  # # CREATE OR REPLACE FUNCTION "tester_function"(integer) RETURNS integer AS $$
102
110
  # # select $1;
@@ -104,9 +112,20 @@ module ActiveRecord
104
112
  # # LANGUAGE "sql"
105
113
  # # IMMUTABLE
106
114
  # # SET "search_path" FROM CURRENT;
107
- def create_function(name, args, returns, language, options = {})
108
- body = yield.to_s
109
- execute PostgreSQLFunctionDefinition.new(self, name, args, returns, language, body, options).to_s
115
+ def create_function(name, arguments, returns, language, *args)
116
+ options = args.extract_options!
117
+
118
+ body = if args.first.present?
119
+ if block_given?
120
+ raise ArgumentError.new("Can't have both a function body argument as well as a block in create_function")
121
+ end
122
+
123
+ args.first
124
+ elsif block_given?
125
+ yield.to_s
126
+ end
127
+
128
+ execute PostgreSQLFunctionDefinition.new(self, name, arguments, returns, language, body, options).to_s
110
129
  end
111
130
 
112
131
  # Drops a function.
@@ -1,7 +1,7 @@
1
1
 
2
2
  module ActiveRecord
3
3
  module PostgreSQLExtensions
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
6
6
  end
7
7
 
@@ -46,6 +46,45 @@ class FunctionsTests < PostgreSQLExtensionsTestCase
46
46
  assert_equal(expected, statements)
47
47
  end
48
48
 
49
+ def test_create_function_with_body_argument
50
+ ARBC.create_function(:test, :integer, :integer, :sql, "select 10;")
51
+
52
+ ARBC.create_function(:test, :integer, :integer, :sql, "return 10;", {
53
+ :force => true,
54
+ :delimiter => '$__$',
55
+ :behavior => :immutable,
56
+ :on_null_input => :strict,
57
+ :cost => 1,
58
+ :rows => 10,
59
+ :set => {
60
+ 'TIME ZONE' => 'America/Halifax'
61
+ }
62
+ })
63
+
64
+ expected = []
65
+
66
+ expected << strip_heredoc(<<-SQL)
67
+ CREATE FUNCTION "test"(integer) RETURNS integer AS $$
68
+ select 10;
69
+ $$
70
+ LANGUAGE "sql";
71
+ SQL
72
+
73
+ expected << strip_heredoc(<<-SQL)
74
+ CREATE OR REPLACE FUNCTION "test"(integer) RETURNS integer AS $__$
75
+ return 10;
76
+ $__$
77
+ LANGUAGE "sql"
78
+ IMMUTABLE
79
+ STRICT
80
+ COST 1
81
+ ROWS 10
82
+ SET TIME ZONE "America/Halifax";
83
+ SQL
84
+
85
+ assert_equal(expected, statements)
86
+ end
87
+
49
88
  def test_drop_function
50
89
  ARBC.drop_function(:test, :integer)
51
90
  ARBC.drop_function(:test, :integer, :if_exists => true, :cascade => true)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgresql-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - J Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-04 00:00:00.000000000 Z
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  type: :runtime