gschool_database_connection 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed0df0ad931019d91dfe5740993a7538228616a9
4
+ data.tar.gz: 0c526641b45a2abf7088e6a34c94b58874de5806
5
+ SHA512:
6
+ metadata.gz: 060d0483597792d01492f85507e6c744b31eddc8c2c478788ebc036488417dc56cca1404ed46f23bb2e97b05b08490e65dea33ad2ed3b1aaa98421966a885822
7
+ data.tar.gz: e9f5e4986503a76577a504115be58ee6f324d795700995351377ca94089dfd09a3b7d5e0b0aee4d4d363f6a236d33299137375328dfe7eea09b40fa3f5d05e59
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+
24
+ config/database.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gschool_database_connection.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jeff Taggart
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # GschoolDatabaseConnection
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gschool_database_connection'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gschool_database_connection
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/gschool_database_connection/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,15 @@
1
+ development:
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ database: sql_ar
5
+ pool: 5
6
+ username: gschool
7
+ password:
8
+
9
+ test:
10
+ adapter: postgresql
11
+ encoding: unicode
12
+ database: sql_ar_test
13
+ pool: 5
14
+ username: gschool
15
+ password:
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gschool_database_connection/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gschool_database_connection"
8
+ spec.version = GschoolDatabaseConnection::VERSION
9
+ spec.authors = ["Jeff Taggart"]
10
+ spec.email = ["jeff@jetaggart.com"]
11
+ spec.summary = %q{This is a database connection class for gSchool}
12
+ spec.description = %q{See summary.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord"
22
+ spec.add_dependency "pg", "~> 0.17.1"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", ">= 3.0.0"
27
+ end
@@ -0,0 +1,6 @@
1
+ require "gschool_database_connection/version"
2
+ require "gschool_database_connection/database_connection"
3
+
4
+ module GschoolDatabaseConnection
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,51 @@
1
+ require "yaml"
2
+ require "active_record"
3
+
4
+ module GschoolDatabaseConnection
5
+ class DatabaseConnection
6
+ attr_reader :connection
7
+
8
+ def self.establish(environment = "development", config_file_path="config/database.yml")
9
+ @@_connection ||= new(environment, config_file_path)
10
+ end
11
+
12
+ def self.clear!
13
+ @@_connection and @@_connection.close
14
+ @@_connection = nil
15
+ end
16
+
17
+ def initialize(environment, config_file_path)
18
+ if ENV["DATABASE_URL"]
19
+ @connection = establish_from_uri(ENV["DATABASE_URL"])
20
+ else
21
+ file = File.read(config_file_path)
22
+ config = YAML.load(file)[environment]
23
+ @connection = establish_from_config(config)
24
+ end
25
+ end
26
+
27
+ def sql(sql_string)
28
+ connection.exec(sql_string).to_a
29
+ end
30
+
31
+ def close
32
+ connection.close
33
+ end
34
+
35
+ private_class_method :new
36
+ private
37
+
38
+ def establish_from_config(config)
39
+ ActiveRecord::Base.establish_connection(
40
+ :adapter => config['adapter'],
41
+ :database => config['database'],
42
+ :username => config['username'],
43
+ :password => config['password']
44
+ ).connection.raw_connection
45
+ end
46
+
47
+ def establish_from_uri(uri)
48
+ ActiveRecord::Base.establish_connection(uri).connection.raw_connection
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module GschoolDatabaseConnection
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,102 @@
1
+ require "spec_helper"
2
+
3
+ describe GschoolDatabaseConnection::DatabaseConnection do
4
+ describe("with a DATABASE_URL") do
5
+ it "test description" do
6
+ skip
7
+ test_config = YAML.load(File.read("config/database.yml"))["test"]
8
+
9
+ old_database_url = ENV["DATABASE_URL"]
10
+ ENV["DATABASE_URL"] = "postgres://#{test_config["username"]}:#{test_config["password"]}@localhost/#{test_config["database"]}"
11
+
12
+ expect {
13
+ GschoolDatabaseConnection::DatabaseConnection.clear!
14
+ database_connection = GschoolDatabaseConnection::DatabaseConnection.establish("production", "/not/a/path.yml")
15
+ database_connection.sql("BEGIN")
16
+ database_connection.sql("ROLLBACK")
17
+ GschoolDatabaseConnection::DatabaseConnection.clear!
18
+ }.to_not raise_error
19
+
20
+ ENV["DATABASE_URL"] = old_database_url
21
+ end
22
+ end
23
+
24
+ describe "with a database.yml" do
25
+ let(:database_connection) { GschoolDatabaseConnection::DatabaseConnection.establish("test") }
26
+
27
+ let(:create_table_sql) do
28
+ <<-CREATE_SQL
29
+ CREATE TABLE users_test (
30
+ username varchar(25),
31
+ email varchar(200),
32
+ date_of_birth date
33
+ )
34
+ CREATE_SQL
35
+ end
36
+
37
+ let(:insert_hunter_sql) do
38
+ <<-INSERT_SQL
39
+ INSERT INTO users_test (username, email, date_of_birth)
40
+ VALUES ('hunter', 'hunter@example.com', '2000-01-01')
41
+ INSERT_SQL
42
+ end
43
+
44
+ let(:insert_jeff_sql) do
45
+ <<-INSERT_SQL
46
+ INSERT INTO users_test (username, email, date_of_birth)
47
+ VALUES ('jeff', 'jeff@example.com', '2006-01-01')
48
+ INSERT_SQL
49
+ end
50
+
51
+ before do
52
+ database_connection.sql(create_table_sql)
53
+ database_connection.sql(insert_hunter_sql)
54
+ database_connection.sql(insert_jeff_sql)
55
+ end
56
+
57
+ it "can SELECT" do
58
+ expected_users = [
59
+ {
60
+ "username" => "hunter",
61
+ "email" => "hunter@example.com",
62
+ "date_of_birth" => "2000-01-01"
63
+ },
64
+ {
65
+ "username" => "jeff",
66
+ "email" => "jeff@example.com",
67
+ "date_of_birth" => "2006-01-01"
68
+ }
69
+ ]
70
+
71
+ expect(database_connection.sql("SELECT * from users_test")).to eq(expected_users)
72
+ end
73
+
74
+ it "can filter" do
75
+ hunter_results = database_connection.sql("SELECT * from users_test WHERE username = 'hunter';")
76
+
77
+ expected_results = [
78
+ {
79
+ "username" => "hunter",
80
+ "email" => "hunter@example.com",
81
+ "date_of_birth" => "2000-01-01"
82
+ }
83
+ ]
84
+
85
+ expect(hunter_results).to eq(expected_results)
86
+ end
87
+
88
+ it "can DELETE" do
89
+ database_connection.sql("DELETE FROM users_test where username = 'hunter';")
90
+
91
+ expected_results = [
92
+ {
93
+ "username" => "jeff",
94
+ "email" => "jeff@example.com",
95
+ "date_of_birth" => "2006-01-01"
96
+ }
97
+ ]
98
+
99
+ expect(database_connection.sql("SELECT * from users_test")).to eq(expected_results)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,14 @@
1
+ require "gschool_database_connection"
2
+ ENV["RACK_ENV"] = "test"
3
+
4
+ database_connection = GschoolDatabaseConnection::DatabaseConnection.establish(ENV["RACK_ENV"])
5
+
6
+ RSpec.configure do |config|
7
+ config.before do
8
+ database_connection.sql("BEGIN")
9
+ end
10
+
11
+ config.after do
12
+ database_connection.sql("ROLLBACK")
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gschool_database_connection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Taggart
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.17.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.17.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ description: See summary.
84
+ email:
85
+ - jeff@jetaggart.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - config/database.yml.example
96
+ - gschool_database_connection.gemspec
97
+ - lib/gschool_database_connection.rb
98
+ - lib/gschool_database_connection/database_connection.rb
99
+ - lib/gschool_database_connection/version.rb
100
+ - spec/database_connection_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: This is a database connection class for gSchool
126
+ test_files:
127
+ - spec/database_connection_spec.rb
128
+ - spec/spec_helper.rb
129
+ has_rdoc: