activerecord_yamless 0.1.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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +10 -0
- data/LICENSE +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/activerecord_yamless.gemspec +18 -0
- data/lib/activerecord_yamless.rb +10 -0
- data/lib/activerecord_yamless/configuration_extension.rb +15 -0
- data/lib/activerecord_yamless/database_configuration.rb +71 -0
- data/lib/activerecord_yamless/method_name_helper.rb +50 -0
- data/lib/activerecord_yamless/railtie.rb +9 -0
- data/lib/activerecord_yamless/version.rb +3 -0
- data/spec/configuration_extension_spec.rb +12 -0
- data/spec/database_configuration_spec.rb +73 -0
- data/spec/helpers/my_application.rb +10 -0
- data/spec/method_name_helper_spec.rb +42 -0
- data/spec/spec_helper.rb +9 -0
- data/tasks/database_extend.rake +9 -0
- metadata +86 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 menostos
|
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,31 @@
|
|
1
|
+
# ActiverecordYamless
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'activerecord_yamless'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install activerecord_yamless
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
application.rb
|
20
|
+
|
21
|
+
config.database_configuration.base = {
|
22
|
+
adapter: "sqlite3",
|
23
|
+
pool: 5,
|
24
|
+
timeout: 5000
|
25
|
+
}
|
26
|
+
|
27
|
+
environments/production.rb
|
28
|
+
|
29
|
+
config.database_configuration.production = {
|
30
|
+
database: "db/production.sqlite3"
|
31
|
+
}
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/activerecord_yamless/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["menostos"]
|
6
|
+
gem.email = ["menostos@gmail.com"]
|
7
|
+
gem.description = %q{This gem removes the need to configuration the database using yaml}
|
8
|
+
gem.summary = %q{The Gem adds some functionality to the rails configuration allowing users to configure their database within environment files}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "activerecord_yamless"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.add_development_dependency 'rspec', '~> 2.5'
|
17
|
+
gem.version = ActiverecordYamless::VERSION
|
18
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'activerecord_yamless/version'
|
3
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
4
|
+
require 'activerecord_yamless/configuration_extension'
|
5
|
+
require 'activerecord_yamless/database_configuration'
|
6
|
+
require 'activerecord_yamless/method_name_helper'
|
7
|
+
require 'activerecord_yamless/railtie' if defined?(Rails)
|
8
|
+
|
9
|
+
module ActiverecordYamless
|
10
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Rails::Application::Configuration
|
2
|
+
|
3
|
+
# returns the database configuration. if it hasn't been defined
|
4
|
+
# it'll create a new instance of ActiverecordYamless::DatabaseConfiguration.new
|
5
|
+
def database_configuration
|
6
|
+
@database_configuration ||= ActiverecordYamless::DatabaseConfiguration.new
|
7
|
+
end
|
8
|
+
|
9
|
+
# adds an environment configuration to the database configuration.
|
10
|
+
# it'll be merged with the default configuration
|
11
|
+
def database_configuration=(value)
|
12
|
+
db_config = self.database_configuration
|
13
|
+
db_config.update_with_defaults(Rails.env, value)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module ActiverecordYamless
|
2
|
+
|
3
|
+
# the class holds the database configuration
|
4
|
+
class DatabaseConfiguration < HashWithIndifferentAccess
|
5
|
+
|
6
|
+
# get a configuration by key.
|
7
|
+
# if it's not already available, it'll create
|
8
|
+
# the configuration merging the defaults
|
9
|
+
def [](key)
|
10
|
+
if self.include?(key)
|
11
|
+
super
|
12
|
+
else
|
13
|
+
self.store(key, {}.merge(self.base))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# get the base configuration
|
18
|
+
def base
|
19
|
+
@base ||= HashWithIndifferentAccess.new
|
20
|
+
end
|
21
|
+
|
22
|
+
# assign the base configuration
|
23
|
+
def base=(value)
|
24
|
+
@base = value
|
25
|
+
end
|
26
|
+
|
27
|
+
# updateds the configuration and overwrites all defaults
|
28
|
+
def update_with_defaults(key, value)
|
29
|
+
self.store(key, self.base.merge(value))
|
30
|
+
end
|
31
|
+
|
32
|
+
# returns a configuration by key according to the method name.
|
33
|
+
# it'll define the method for later use
|
34
|
+
def method_missing(method, *args, &block)
|
35
|
+
method_helper = ActiverecordYamless::MethodNameHelper.new(method)
|
36
|
+
if method_helper.setter_method? && args.first && args.first.is_a?(Hash)
|
37
|
+
define_methods_and_assign_value(method_helper, args.first)
|
38
|
+
else
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
# defines a method accessing a key on self and assigns the given value
|
45
|
+
def define_methods_and_assign_value(method_helper, value)
|
46
|
+
define_read_method!(method_helper)
|
47
|
+
define_write_method!(method_helper)
|
48
|
+
self.send(method_helper.write_method, value)
|
49
|
+
end
|
50
|
+
|
51
|
+
# defines the read method for a key
|
52
|
+
def define_read_method!(method_helper)
|
53
|
+
unless self.respond_to?(method_helper.read_method)
|
54
|
+
define_singleton_method(method_helper.read_method) do
|
55
|
+
return self[method_helper.read_method]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# defines the write method for a key
|
61
|
+
def define_write_method!(method_helper)
|
62
|
+
unless self.respond_to?(method_helper.write_method)
|
63
|
+
define_singleton_method(method_helper.write_method) do |value|
|
64
|
+
return self[method_helper.read_method] = self.base.merge(value)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiverecordYamless
|
2
|
+
|
3
|
+
|
4
|
+
class MethodNameHelper
|
5
|
+
|
6
|
+
attr_reader :original_name
|
7
|
+
attr_reader :read_method, :write_method
|
8
|
+
|
9
|
+
# holds methods name computed from name.
|
10
|
+
# provides +original_name+, +read_method+ and +write_method+
|
11
|
+
def initialize(name)
|
12
|
+
@original_name = name.to_s
|
13
|
+
@read_method = MethodNameHelper.extract_method_name(self.original_name)
|
14
|
+
@write_method = MethodNameHelper.setter_name(self.original_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
# returns true if it's already an setter method name
|
18
|
+
def setter_method?
|
19
|
+
MethodNameHelper.setter_method?(self.original_name)
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
# returns true if name is a setter method name
|
24
|
+
def setter_method?(name)
|
25
|
+
name.end_with?("=")
|
26
|
+
end
|
27
|
+
|
28
|
+
# extracts a getter method name
|
29
|
+
def extract_method_name(name)
|
30
|
+
if MethodNameHelper.setter_method?(name)
|
31
|
+
name[0, name.length - 1]
|
32
|
+
else
|
33
|
+
name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# returns a setter method name from name
|
38
|
+
def setter_name(name)
|
39
|
+
if MethodNameHelper.setter_method?(name)
|
40
|
+
name
|
41
|
+
else
|
42
|
+
"#{name}="
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'helpers/my_application'
|
4
|
+
describe Rails::Application::Configuration do
|
5
|
+
|
6
|
+
subject{ Rails.application.config.database_configuration }
|
7
|
+
|
8
|
+
it 'should return an instance of ActiverecordYamless::DatabaseConfiguration' do
|
9
|
+
subject.should be_an_instance_of(ActiverecordYamless::DatabaseConfiguration)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiverecordYamless::DatabaseConfiguration do
|
5
|
+
|
6
|
+
it 'should store a set of options within base' do
|
7
|
+
subject.base = {'name' => 'base_options'}
|
8
|
+
subject.base.should eq({'name' => 'base_options'})
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should assign a value from a setter method call with a hash' do
|
12
|
+
subject.development = {'user' => 'admin'}
|
13
|
+
subject[:development].should eq({'user' => 'admin'})
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should assign a value from a setter method and use base as defaults' do
|
17
|
+
subject.base = {'adapter' => 'sql', 'encoding' => 'latin1'}
|
18
|
+
subject.development = {'user' => 'admin', 'encoding' => 'utf8'}
|
19
|
+
subject[:development][:adapter].should eq('sql')
|
20
|
+
subject[:development][:user].should eq('admin')
|
21
|
+
subject[:development][:encoding].should eq('utf8')
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should create getter and setter when called with a setter method' do
|
25
|
+
subject.should_not respond_to(:development)
|
26
|
+
subject.should_not respond_to(:development=)
|
27
|
+
subject.development = {'user' => 'admin'}
|
28
|
+
subject.should respond_to(:development)
|
29
|
+
subject.should respond_to(:development=)
|
30
|
+
subject.development.should eq({'user' => 'admin'})
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should define a read method by name' do
|
34
|
+
method_helper = ActiverecordYamless::MethodNameHelper.new(:username)
|
35
|
+
subject.should_not respond_to(:username)
|
36
|
+
subject.send(:define_read_method!, method_helper)
|
37
|
+
subject.should respond_to(:username)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should read the hash through read method' do
|
41
|
+
method_helper = ActiverecordYamless::MethodNameHelper.new(:name)
|
42
|
+
subject[:name] = 'test'
|
43
|
+
subject.send(:define_read_method!, method_helper)
|
44
|
+
subject.name.should eq('test')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should write the hash through write method' do
|
48
|
+
method_helper = ActiverecordYamless::MethodNameHelper.new(:name)
|
49
|
+
subject[:name] = {'firstname' => 'Peter'}
|
50
|
+
subject.send(:define_write_method!, method_helper)
|
51
|
+
subject.name = {'firstname' => 'Sam'}
|
52
|
+
subject[:name][:firstname].should eq('Sam')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should define a write method by name' do
|
56
|
+
method_helper = ActiverecordYamless::MethodNameHelper.new(:username)
|
57
|
+
subject.should_not respond_to(:username=)
|
58
|
+
subject.send(:define_write_method!, method_helper)
|
59
|
+
subject.should respond_to(:username=)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should define read and write methods and assign a value' do
|
63
|
+
method_helper = ActiverecordYamless::MethodNameHelper.new(:username)
|
64
|
+
subject.should_not respond_to(:username)
|
65
|
+
subject.should_not respond_to(:username=)
|
66
|
+
subject.send(:define_methods_and_assign_value, method_helper, {'default' => true})
|
67
|
+
subject.should respond_to(:username)
|
68
|
+
subject.should respond_to(:username=)
|
69
|
+
subject[:username].include?(:default).should be_true
|
70
|
+
subject[:username][:default].should be_true
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiverecordYamless::MethodNameHelper do
|
5
|
+
|
6
|
+
context "when initialized with a write method" do
|
7
|
+
|
8
|
+
subject{ ActiverecordYamless::MethodNameHelper.new(:name=) }
|
9
|
+
|
10
|
+
it 'should return true for setter_method?' do
|
11
|
+
subject.setter_method?.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should return the correct read method name' do
|
15
|
+
subject.read_method.should eq('name')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should return the correct write method name' do
|
19
|
+
subject.write_method.should eq('name=')
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when initialized with a read method" do
|
25
|
+
|
26
|
+
subject{ ActiverecordYamless::MethodNameHelper.new(:name) }
|
27
|
+
|
28
|
+
it 'should return false for setter_method?' do
|
29
|
+
subject.setter_method?.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return the correct read method name' do
|
33
|
+
subject.read_method.should eq('name')
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should return the correct write method name' do
|
37
|
+
subject.write_method.should eq('name=')
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# loading all environment files which should contain the database configuration
|
2
|
+
namespace :db do
|
3
|
+
task :load_env_files do
|
4
|
+
require Rails.root.join("config/application.rb")
|
5
|
+
Dir[Rails.root.join("config/environments/*.rb")].each{|file| require(file) }
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
Rake::Task["db:load_config"].prerequisites << :load_env_files
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_yamless
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- menostos
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.5'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.5'
|
30
|
+
description: This gem removes the need to configuration the database using yaml
|
31
|
+
email:
|
32
|
+
- menostos@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- activerecord_yamless.gemspec
|
44
|
+
- lib/activerecord_yamless.rb
|
45
|
+
- lib/activerecord_yamless/configuration_extension.rb
|
46
|
+
- lib/activerecord_yamless/database_configuration.rb
|
47
|
+
- lib/activerecord_yamless/method_name_helper.rb
|
48
|
+
- lib/activerecord_yamless/railtie.rb
|
49
|
+
- lib/activerecord_yamless/version.rb
|
50
|
+
- spec/configuration_extension_spec.rb
|
51
|
+
- spec/database_configuration_spec.rb
|
52
|
+
- spec/helpers/my_application.rb
|
53
|
+
- spec/method_name_helper_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- tasks/database_extend.rake
|
56
|
+
homepage: ''
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.24
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: The Gem adds some functionality to the rails configuration allowing users
|
80
|
+
to configure their database within environment files
|
81
|
+
test_files:
|
82
|
+
- spec/configuration_extension_spec.rb
|
83
|
+
- spec/database_configuration_spec.rb
|
84
|
+
- spec/helpers/my_application.rb
|
85
|
+
- spec/method_name_helper_spec.rb
|
86
|
+
- spec/spec_helper.rb
|