perpetuity-postgres 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45cd2bfa1345abc08e97eda5e929c3ef1c4b7c23
4
- data.tar.gz: 2cab12a23a5f25a0f114b76494f283793f7213be
3
+ metadata.gz: 1674c0daefbd7dcfaad9351b6e653f22b0ade171
4
+ data.tar.gz: b2b4e9059b515c61f3bfa0bcfda1f460c167cc51
5
5
  SHA512:
6
- metadata.gz: 79578054a8adc0164fd5e5197f6b52f0ed337c8af13b6910dfef79060dedea6c6e08f8310c9ad26b01fc70e756896996820ea695bf2ee9a70a1a16037fba2ba8
7
- data.tar.gz: a0d72ba46bff515cc6d7c222fd68e63ea780aee5fcf2f534a4c9f8963f1afa54f15739c5335aa8e15591d43fe8f52e8a2e1d76d351c421e1e85ba119901b4c44
6
+ metadata.gz: f142288f17ca1ac7ad7b6a9c3f8e874e64276cb2a40d08b6f03c6733d1357763a2d97c95f56a3df3ffcea98106530bceb60f721277c32a81daffbe7baeb6ef77
7
+ data.tar.gz: b624021e54ec0dc4632eb51b5ed56b56fe965fe63f7d559333da4b4f5260dc55b5907bccca3b0efbd3fe7070130e16d01cf0bf7409fdbe4f3eac8500a321609b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Version 0.0.3
2
+
3
+ - Require the `bigdecimal` library to avoid undefined constant error: BigDecimals are converted to the Postgres `numeric` data type. In 1.9, it needs to be required explicitly. — [Avdi Grimm](https://github.com/avdi)
4
+ - Properly return the connection to the connection pool if an exception is raised in the query.
5
+
1
6
  ## Version 0.0.2
2
7
 
3
8
  - Make DB creation call out to remote DB to create it instead of calling on localhost.
data/README.md CHANGED
@@ -21,7 +21,7 @@ Or install it yourself as:
21
21
  To configure Perpetuity to use your PostgreSQL database, you can use the same parameters as you would with the MongoDB adapter, except substitute `:postgres` in for `:mongodb`:
22
22
 
23
23
  ```ruby
24
- require 'perpetuity-postgres' # Unnecessary if using Rails
24
+ require 'perpetuity/postgres' # Unnecessary if using Rails
25
25
  Perpetuity.data_source :postgres, 'my_perpetuity_database'
26
26
  ```
27
27
 
@@ -17,10 +17,10 @@ module Perpetuity
17
17
  def lend_connection
18
18
  if block_given?
19
19
  connection = connections.pop
20
- result = yield connection
21
- connections << connection
22
- result
20
+ yield connection
23
21
  end
22
+ ensure
23
+ connections << connection
24
24
  end
25
25
 
26
26
  def execute sql
@@ -1,31 +1,39 @@
1
- require 'perpetuity/postgres/numeric_value'
2
1
  require 'perpetuity/postgres/json_string_value'
3
- require 'perpetuity/postgres/json_hash'
2
+ require 'perpetuity/postgres/sql_value'
4
3
 
5
4
  module Perpetuity
6
5
  class Postgres
7
6
  class JSONArray
8
- def initialize value
7
+ def initialize value, position=:outer
9
8
  @value = value
9
+ @position = position
10
10
  end
11
11
 
12
12
  def to_s
13
- "'[#{serialize_elements}]'"
13
+ if @position == :outer
14
+ "'#{to_inner_array}'"
15
+ else
16
+ to_inner_array
17
+ end
18
+ end
19
+
20
+ def to_inner_array
21
+ "[#{serialize_elements}]"
14
22
  end
15
23
 
16
24
  def serialize_elements
17
25
  @value.map do |element|
18
- if element.is_a? Numeric
19
- NumericValue.new(element)
20
- elsif element.is_a? String
26
+ if element.is_a? String
21
27
  JSONStringValue.new(element)
22
- elsif element.is_a? Hash
23
- JSONHash.new(element, :inner)
24
- elsif element.is_a? JSONHash
25
- JSONHash.new(element.to_hash, :inner)
28
+ else
29
+ SQLValue.new(element)
26
30
  end
27
31
  end.join(',')
28
32
  end
33
+
34
+ def to_a
35
+ @value
36
+ end
29
37
  end
30
38
  end
31
39
  end
@@ -1,5 +1,5 @@
1
+ require 'perpetuity/postgres/sql_value'
1
2
  require 'perpetuity/postgres/json_string_value'
2
- require 'perpetuity/postgres/numeric_value'
3
3
 
4
4
  module Perpetuity
5
5
  class Postgres
@@ -26,20 +26,14 @@ module Perpetuity
26
26
  string = ''
27
27
  string << JSONStringValue.new(key) << ':'
28
28
 
29
- string << if value.is_a? Numeric
30
- NumericValue.new(value)
31
- elsif value.is_a? String
32
- JSONStringValue.new(value)
33
- elsif value.is_a? Hash
34
- JSONHash.new(value, :inner)
35
- elsif value.is_a? Class
29
+ string << if [String, Class].include? value.class
36
30
  JSONStringValue.new(value.to_s)
37
31
  elsif [true, false].include? value
38
32
  value.to_s
39
33
  elsif value.nil?
40
34
  'null'
41
35
  else
42
- value
36
+ SQLValue.new(value).to_s
43
37
  end
44
38
  end.join(',')
45
39
  end
@@ -41,6 +41,10 @@ METHOD
41
41
  def to_s
42
42
  name.to_s
43
43
  end
44
+
45
+ def to_db
46
+ (self != nil).to_db
47
+ end
44
48
  end
45
49
  end
46
50
  end
@@ -34,7 +34,7 @@ module Perpetuity
34
34
  end
35
35
 
36
36
  def any?
37
- values.any?
37
+ values.flatten.any?
38
38
  end
39
39
 
40
40
  def each
@@ -109,6 +109,8 @@ module Perpetuity
109
109
  SQLValue.new(value)
110
110
  elsif value.is_a? Array
111
111
  serialize_array(object)
112
+ elsif value.is_a? Hash
113
+ serialize_hash(object)
112
114
  elsif !object.embedded?
113
115
  serialize_reference(value)
114
116
  elsif object.embedded?
@@ -131,6 +133,10 @@ module Perpetuity
131
133
  JSONArray.new(array)
132
134
  end
133
135
 
136
+ def serialize_hash object
137
+ JSONHash.new(object)
138
+ end
139
+
134
140
  def serialize_to_hash value
135
141
  Hash[
136
142
  mapper.attribute_set.map do |attribute|
@@ -3,6 +3,8 @@ require 'perpetuity/postgres/timestamp_value'
3
3
  require 'perpetuity/postgres/numeric_value'
4
4
  require 'perpetuity/postgres/null_value'
5
5
  require 'perpetuity/postgres/boolean_value'
6
+ require 'perpetuity/postgres/json_hash'
7
+ require 'perpetuity/postgres/json_array'
6
8
 
7
9
  module Perpetuity
8
10
  class Postgres
@@ -17,6 +19,10 @@ module Perpetuity
17
19
  TimestampValue.new(value)
18
20
  when Fixnum, Float
19
21
  NumericValue.new(value)
22
+ when Hash, JSONHash
23
+ JSONHash.new(value.to_hash, :inner)
24
+ when Array, JSONArray
25
+ JSONArray.new(value.to_a, :inner)
20
26
  when nil
21
27
  NullValue.new
22
28
  when true, false
@@ -1,3 +1,5 @@
1
+ require "bigdecimal"
2
+
1
3
  module Perpetuity
2
4
  class Postgres
3
5
  class Table
@@ -1,5 +1,5 @@
1
1
  module Perpetuity
2
2
  class Postgres
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -23,6 +23,10 @@ module Perpetuity
23
23
  JSONArray.new([JSONHash.new(a: 1)]).to_s.should == %q{'[{"a":1}]'}
24
24
  end
25
25
 
26
+ it 'serializes arrays of arrays' do
27
+ JSONArray.new([[1], ['foo']]).to_s.should == %q{'[[1],["foo"]]'}
28
+ end
29
+
26
30
  it 'serializes elements of arrays' do
27
31
  JSONArray.new([1,'a']).to_s.should == %q{'[1,"a"]'}
28
32
  end
@@ -31,6 +31,11 @@ module Perpetuity
31
31
  JSONHash.new({a: 1, b: 'c'}).to_s.should == %q{'{"a":1,"b":"c"}'}
32
32
  end
33
33
 
34
+ it 'serializes a hash with array values' do
35
+ JSONHash.new({foo: ['bar', 'baz', 'quux']}).to_s.should ==
36
+ %q{'{"foo":["bar","baz","quux"]}'}
37
+ end
38
+
34
39
  it 'converts back to a hash' do
35
40
  JSONHash.new({a: 1}).to_hash.should == { a: 1 }
36
41
  end
@@ -51,6 +51,10 @@ module Perpetuity
51
51
  it 'checks for nil' do
52
52
  attribute.nil?.should be_a QueryExpression
53
53
  end
54
+
55
+ it 'checks for truthiness' do
56
+ attribute.to_db.should == 'attribute_name IS NOT NULL'
57
+ end
54
58
  end
55
59
  end
56
60
  end
@@ -46,6 +46,8 @@ module Perpetuity
46
46
 
47
47
  it 'checks whether there are any objects' do
48
48
  serialized.any?.should be_true
49
+ serialized.values.clear << []
50
+ serialized.any?.should be_false
49
51
  end
50
52
 
51
53
  it 'iterates like a hash' do
@@ -84,6 +84,10 @@ module Perpetuity
84
84
  it 'serializes an array as JSON' do
85
85
  serializer.serialize_attribute([1, 'foo']).should == %q{'[1,"foo"]'}
86
86
  end
87
+
88
+ it 'serializes a hash as JSON' do
89
+ serializer.serialize_attribute(a: 1, foo: ['bar']).should == %q{'{"a":1,"foo":["bar"]}'}
90
+ end
87
91
  end
88
92
 
89
93
  describe 'unserialization AKA deserialization' do
@@ -29,6 +29,23 @@ module Perpetuity
29
29
  SQLValue.new(false).should == "FALSE"
30
30
  end
31
31
 
32
+ it 'converts hashes' do
33
+ SQLValue.new({ a: 1, b: 'foo'}).should == %q({"a":1,"b":"foo"})
34
+ end
35
+
36
+ it 'converts arrays' do
37
+ SQLValue.new([1, 'foo', { a: 1 }]).should == %q([1,"foo",{"a":1}])
38
+ end
39
+
40
+ it 'converts JSONHashes' do
41
+ SQLValue.new(JSONHash.new(a: 1)).should == %q({"a":1})
42
+ end
43
+
44
+ it 'converts JSONArrays' do
45
+ SQLValue.new(JSONArray.new([1, 'foo', [1, 'foo']])).should ==
46
+ %q([1,"foo",[1,"foo"]])
47
+ end
48
+
32
49
  it 'converts Time objects' do
33
50
  time = Time.new(2013, 1, 2, 3, 4, 5.1234567, '+05:30')
34
51
  SQLValue.new(time).should == "'2013-01-02 03:04:05.123456+0530'::timestamptz"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perpetuity-postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Gaskins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-30 00:00:00.000000000 Z
11
+ date: 2014-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler