simple-sql 0.1.3 → 0.2.0

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: a64abeaf64f527b10d9381fcecbb47c712184ebc
4
- data.tar.gz: 8ad02d882e3f281c4880af342c7b15c33e189e4f
3
+ metadata.gz: 35cf2798d086495d8716eb22b6ce08bea72a5ab1
4
+ data.tar.gz: 6e332f0a12d3442ee7a28e319cff5b8759a8affb
5
5
  SHA512:
6
- metadata.gz: fec8ff260a4551b5f5e73640291cf1db92792aa108fc73e218825e27890e8275028617c52fa9acde204373155bebad645d894515461bf09dc1f2ccac592efa7e
7
- data.tar.gz: fb5648febf02c8a08e25c31e30602838bdaff934ddd1097d597291b3b09a03d8451218f1a9594a77ac0fe66be9b2366ce1406477e2b1fa3dc72e7ff30f5cf97e
6
+ metadata.gz: 45f2f76f763f1587b4e1d330e00f3d24e87c8f7dca32a08abc9fed7f1076f5d87d42cc1dc06eeb20a2ac2a5c92d3a8312a31642edcb80f7940bfb46cc02d5bb9
7
+ data.tar.gz: d5efb732d0ebf9f8be40cf1dae1c3ba33782b4df0c92be284608fc74818b78b920177186da5fb5ef9ae6f704f11a49d2ae33cf8bd423df834b4a905bab694061
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-sql (0.1.3)
5
- activerecord (~> 4)
4
+ simple-sql (0.2.0)
5
+ pg (~> 0.20)
6
6
  pg_array_parser (~> 0)
7
7
 
8
8
  GEM
@@ -77,6 +77,7 @@ PLATFORMS
77
77
  ruby
78
78
 
79
79
  DEPENDENCIES
80
+ activerecord (~> 4)
80
81
  database_cleaner (~> 1)
81
82
  factory_girl (= 4.8.0)
82
83
  pg (= 0.20)
data/lib/simple/sql.rb CHANGED
@@ -97,7 +97,59 @@ module Simple
97
97
  end
98
98
 
99
99
  def connection
100
- ActiveRecord::Base.connection.raw_connection
100
+ @connector.call
101
+ end
102
+
103
+ def connector=(connector)
104
+ @connector = connector
105
+ end
106
+
107
+ self.connector = lambda {
108
+ connect_to_active_record
109
+ }
110
+
111
+ def connect_to_active_record
112
+ return ActiveRecord::Base.connection.raw_connection if defined?(ActiveRecord)
113
+
114
+ STDERR.puts <<-SQL
115
+ simple-sql works out of the box with ActiveRecord-based postgres connections.
116
+
117
+ To use it without ActiveRecord you must connect to a database via
118
+
119
+ Simple::SQL.connect!("postgresql://username:password@dbhost:port/dbname").
120
+ SQL
121
+
122
+ raise ArgumentError, "simple-sql: missing connection"
123
+ end
124
+
125
+ public
126
+
127
+ def connect!(url)
128
+ require "pg"
129
+
130
+ config = db_config_from_url(url)
131
+ connection = PG::Connection.new(config)
132
+ self.connector = lambda { connection }
133
+ end
134
+
135
+ def db_config_from_url(url)
136
+ require "uri"
137
+
138
+ raise ArgumentError, "Invalid URL #{url.inspect}" unless url.is_a?(String)
139
+ raise ArgumentError, "Invalid URL #{url.inspect}" unless url =~ /^postgres(ql)?s?:\/\//
140
+
141
+ uri = URI.parse(url)
142
+ raise ArgumentError, "Invalid URL #{url}" unless uri.hostname && uri.path
143
+
144
+ config = {
145
+ dbname: uri.path.sub(%r{^/}, ""),
146
+ host: uri.hostname
147
+ }
148
+ config[:port] = uri.port if uri.port
149
+ config[:user] = uri.user if uri.user
150
+ config[:password] = uri.password if uri.password
151
+ config[:sslmode] = uri.scheme == "postgress" || uri.scheme == "postgresqls" ? "require" : "prefer"
152
+ config
101
153
  end
102
154
  end
103
155
  end
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/simple-sql.gemspec CHANGED
@@ -27,12 +27,13 @@ Gem::Specification.new do |gem|
27
27
 
28
28
  gem.required_ruby_version = '~> 2.3'
29
29
 
30
- gem.add_dependency 'activerecord', '~> 4'
31
30
  gem.add_dependency 'pg_array_parser', '~> 0'
31
+ gem.add_dependency 'pg', '~> 0.20'
32
32
 
33
33
  # optional gems (required by some of the parts)
34
34
 
35
35
  # development gems
36
+ gem.add_development_dependency 'activerecord', '~> 4'
36
37
  gem.add_development_dependency 'pg', '0.20'
37
38
  gem.add_development_dependency 'rake', '~> 11'
38
39
  gem.add_development_dependency 'rspec', '~> 3.7'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel
@@ -12,33 +12,47 @@ cert_chain: []
12
12
  date: 2018-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activerecord
15
+ name: pg_array_parser
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '4'
20
+ version: '0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '4'
27
+ version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: pg_array_parser
29
+ name: pg
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
34
+ version: '0.20'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: '0.20'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activerecord
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '4'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '4'
42
56
  - !ruby/object:Gem::Dependency
43
57
  name: pg
44
58
  requirement: !ruby/object:Gem::Requirement