activerecord-configurations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4252f9c1907733834ec22c952d74dd28e9e320ed274202cd20e4e672a9fd0746
4
+ data.tar.gz: 267708e01b05d2c1a1face1c19c6ae6f74ff726cd330f3cf87f104c6fee4a84a
5
+ SHA512:
6
+ metadata.gz: 59c4d07bc30ac0b1cd120aea051694b2cc81533ec8c7575606815b548397e4fb17cb3f53cbe7ff314018198ae6d844363200431a46d6d6e948a85dce239cae2d
7
+ data.tar.gz: 6a9997690b9ecdbe6a0095b5f310cc5eab970fcf8d4307295a32d2b0d4653f3a056627e3ca4e3cd0b0092239facc110c5cc23d00b432832e766fca46385e59d0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --color
2
+ --format documentation
3
+ --backtrace
4
+ --warnings
5
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.2
5
+ env: COVERAGE=true
6
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-migrations.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'sqlite3'
8
+ gem 'pry'
9
+ end
10
+
11
+ group :test do
12
+ gem 'simplecov'
13
+ gem 'coveralls', require: false
14
+ end
@@ -0,0 +1,63 @@
1
+ # ActiveRecord::Configurations
2
+
3
+ This gem replaces the default ActiveRecord configurations system which typically involves a YAML file.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/ioquatix/activerecord-configurations.svg)](http://travis-ci.org/ioquatix/activerecord-configurations)
6
+ [![Code Climate](https://codeclimate.com/github/ioquatix/activerecord-configurations.svg)](https://codeclimate.com/github/ioquatix/activerecord-configurations)
7
+ [![Coverage Status](https://coveralls.io/repos/ioquatix/activerecord-configurations/badge.svg)](https://coveralls.io/r/ioquatix/activerecord-configurations)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'activerecord-configurations'
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Add something like this to `db/environment.rb`
20
+
21
+ ```ruby
22
+ require 'active_record/configurations'
23
+
24
+
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
34
+
35
+ ## See Also
36
+
37
+ - [ActiveRecord::Rack](https://github.com/ioquatix/activerecord-rack)
38
+ - [ActiveRecord::Migrations](https://github.com/ioquatix/activerecord-migrations)
39
+
40
+ ## License
41
+
42
+ Released under the MIT license.
43
+
44
+ Copyright, 2018, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining a copy
47
+ of this software and associated documentation files (the "Software"), to deal
48
+ in the Software without restriction, including without limitation the rights
49
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
50
+ copies of the Software, and to permit persons to whom the Software is
51
+ furnished to do so, subject to the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be included in
54
+ all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
62
+ THE SOFTWARE.
63
+
@@ -0,0 +1,20 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ begin
6
+ require('simplecov/version')
7
+ task.rspec_opts = %w{--require simplecov} if ENV['COVERAGE']
8
+ rescue LoadError
9
+ end
10
+ end
11
+
12
+ task :default => :spec
13
+
14
+ task :console do
15
+ require 'pry'
16
+
17
+ require_relative 'lib/active_record/configurations'
18
+
19
+ Pry.start
20
+ end
@@ -0,0 +1,28 @@
1
+
2
+ require_relative 'lib/active_record/configurations/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "activerecord-configurations"
6
+ spec.version = ActiveRecord::Configurations::VERSION
7
+ spec.authors = ["Samuel Williams"]
8
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
+
10
+ spec.summary = %q{Simplified multi-DSN configuration for ActiveRecord 5+}
11
+ spec.homepage = "https://github.com/ioquatix/activerecord-configurations"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", "~> 5.0"
22
+
23
+ spec.add_dependency "build-environment", "~> 1.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,117 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative "configurations/version"
22
+
23
+ require 'build/environment'
24
+
25
+ module ActiveRecord
26
+ module Configurations
27
+ def self.extended(child)
28
+ child.instance_variable_set(:@environments, {})
29
+ child.instance_variable_set(:@configurations, {})
30
+
31
+ # This is needed to explicitly override the behaviour provided by ActiveRecord::Core.
32
+ def child.configurations
33
+ instance_variable_get(:@configurations)
34
+ end
35
+
36
+ def child.configurations= value
37
+ instance_variable_set(:@configurations, value)
38
+ end
39
+
40
+ # Remove the shitty "default"
41
+ child.configurations.delete("default_env")
42
+
43
+ # Add one that makes sense:
44
+ child.configure(:default) do
45
+ # The provided uri overrides other options.
46
+ dsn ->{ENV[prefix.upcase + "_DSN"]}
47
+ database ->{prefix + "_#{name}"}
48
+
49
+ host 'localhost'
50
+ prefix 'database'
51
+ end
52
+ end
53
+
54
+ attr :environments
55
+ attr :configurations
56
+
57
+ def resolve_database_dsn(string, environment)
58
+ uri = URI.parse(string)
59
+
60
+ case uri.scheme
61
+ when 'postgres'
62
+ environment[:adapter] = 'postgres'
63
+ when 'mysql'
64
+ environment[:adapter] = 'mysql2'
65
+ else
66
+ raise ArgumentError.new("Unsupported database access: #{uri.scheme.inspect}")
67
+ end
68
+
69
+ if username = uri.user
70
+ environment[:username] = username
71
+ end
72
+
73
+ if password = uri.password
74
+ environment[:password] = password
75
+ end
76
+
77
+ if host = uri.host
78
+ environment[:host] = host
79
+ end
80
+
81
+ if port = uri.port
82
+ environment[:port] = port
83
+ end
84
+
85
+ if path = uri.path
86
+ # The path should always be relative:
87
+ environment[:database] = path.sub(/^\//, '')
88
+ end
89
+ end
90
+
91
+ def lookup_environment(name)
92
+ return nil unless name
93
+
94
+ if environment = self.environments[name]
95
+ environment
96
+ # elsif self.superclass
97
+ # self.superclass.lookup_environment(name)
98
+ end
99
+ end
100
+
101
+ def configure(name, parent: :default, &block)
102
+ parent = self.lookup_environment(parent)
103
+
104
+ environment = Build::Environment.new(parent, {name: name.to_s}, &block)
105
+
106
+ self.environments[name] = environment
107
+
108
+ configuration = environment.to_hash
109
+
110
+ if dsn = configuration[:dsn]
111
+ resolve_database_dsn(dsn, configuration)
112
+ end
113
+
114
+ self.configurations[name.to_s] = configuration.stringify_keys
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module ActiveRecord
22
+ module Configurations
23
+ VERSION = "0.1.0"
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-configurations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-27 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: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: build-environment
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
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.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.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'
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'
83
+ description:
84
+ email:
85
+ - samuel.williams@oriontransfer.co.nz
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - activerecord-configurations.gemspec
97
+ - lib/active_record/configurations.rb
98
+ - lib/active_record/configurations/version.rb
99
+ homepage: https://github.com/ioquatix/activerecord-configurations
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.7.6
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Simplified multi-DSN configuration for ActiveRecord 5+
123
+ test_files: []