dotconfig 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,7 @@
1
+ ## Version 0.0.1
2
+
3
+ * Initial release
4
+
5
+ ## Version 0.0.1
6
+
7
+ * Correcting the summary of the gem
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dot_config.gemspec
4
+ gemspec
5
+
6
+ # Gems used in develpment.
7
+ group :development do
8
+ # Generates better documentation.
9
+ gem 'redcarpet'
10
+ gem 'yard'
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Geoffrey Roguelon
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.
@@ -0,0 +1,67 @@
1
+ # DotConfig
2
+
3
+ The DotConfig gem give you access to your configuration via the dot syntax. You instantiate DotConfig::Configuration with a Hash and you will retrieve the values in calling the key as method name.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dot_config'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dot_config
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ # Instantiate the configuration with the shortcut:
23
+ app_config = DotConfig.new(language: 'Ruby', framework: 'Ruby on Rails')
24
+
25
+ app_config.language #=> 'Ruby'
26
+ app_config.framework #=> 'Ruby on Rails'
27
+ app_config.version #=> NoMethodError: undefined method `version' for #<DotConfig::Configuration:0x007fb1fed55dd8>
28
+
29
+ # Instantiate the configuration without the shortcut:
30
+ app_config = DotConfig::Configuration.new(config: { language: 'Ruby', framework: 'Ruby on Rails' })
31
+
32
+ # Instantiate the configuration with an instance of DotConfig::Configuration:
33
+ app_config = DotConfig::Configuration.new
34
+ app_config.config = { language: 'Ruby', framework: 'Ruby on Rails' }
35
+
36
+ # Instantiate the configuration with a YAML file:
37
+ app_config = DotConfig.new('PATH/TO/YOUR/FILE.yml')
38
+
39
+ # Use a nested Hash:
40
+ app_config = DotConfig.new(language: { ruby: 'Ruby on Rails' })
41
+
42
+ app_config.language #=> #<DotConfig::Configuration:0x007fb1fed55dd8>
43
+ app_config.language.ruby #=> 'Ruby on Rails'
44
+
45
+ # Enable the modification:
46
+ app_config = DotConfig.new({ language: 'Ruby', framework: 'Ruby on Rails' }, true)
47
+ # or
48
+ app_config = DotConfig.new(true)
49
+ app_config.config = { language: 'Ruby', framework: 'Ruby on Rails' }
50
+
51
+ app_config.language #=> 'Ruby'
52
+ app_config.language = 'Python'
53
+ app_config.language #=> 'Python'
54
+
55
+ # Export the configuration:
56
+ app_config = DotConfig.new(language: { ruby: 'Ruby on Rails' })
57
+
58
+ app_config.to_hash #=> { language: { ruby: 'Ruby on Rails' } }
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = false
8
+ t.warning = true
9
+ end
10
+
11
+ desc 'Run tests'
12
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'dot_config/version'
7
+
8
+ Gem::Specification.new do |gem|
9
+ gem.name = 'dotconfig'
10
+ gem.version = DotConfig::Version::STRING
11
+ gem.authors = 'Geoffrey Roguelon'
12
+ gem.email = 'geoffrey.roguelon@gmail.com'
13
+ gem.description = %q{Simplify your configuration with: DotConfig.new(language: :ruby).language #=> 'Ruby'.}
14
+ gem.summary = %q{The DotConfig gem give you access to your configuration via the dot syntax. You instantiate DotConfig::Configuration with a Hash and you will retrieve the values in calling the key as method name.}
15
+ gem.license = 'MIT'
16
+ gem.homepage = 'http://github.com/GRoguelon/DotConfig'
17
+ gem.has_rdoc = false
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = %w{lib}
23
+
24
+ gem.add_development_dependency 'activesupport', '~> 3.0'
25
+
26
+ gem.required_ruby_version = '>= 1.9.1'
27
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'dot_config/configuration'
4
+ require 'dot_config/version'
5
+ require 'yaml'
6
+
7
+ # This module provides a way to define a set of configuration with read access
8
+ # only.
9
+ #
10
+ # @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
11
+ module DotConfig
12
+
13
+ # Returns an instance of +Configuration+.
14
+ #
15
+ # @param [Hash] config The +Hash+ to convert in configuration or a +String+
16
+ # which represent the YAML file to load.
17
+ # @param [Boolean] writing Determines if the +Configuration+ is mutable.
18
+ # @return [Configuration] The +Configuration+ instance.
19
+ def self.new(config, writing = false)
20
+ if config.is_a?(Hash)
21
+ Configuration.new(config: config, writing: writing)
22
+ elsif config.is_a?(String) && File.exists?(config)
23
+ Configuration.new(config: YAML.load(File.read(config)), writing: writing)
24
+ else
25
+ raise ArgumentError, 'Unable to process the config argument'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,78 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module DotConfig
4
+
5
+ # This class gives you access to the configuration with dot syntax.
6
+ #
7
+ # @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
8
+ class Configuration
9
+
10
+ # @!attribute config [r]
11
+ # @return [Hash] Stores the configuration like a +Hash+.
12
+ # @!attribute writing [r]
13
+ # @return [Hash] Determines if the configuration is mutable.
14
+ attr_reader :config, :writing
15
+
16
+ # Class constructor.
17
+ #
18
+ # @return [self]
19
+ def initialize(options = {})
20
+ raise ArgumentError, 'Invalid argument' if !options.is_a?(Hash)
21
+
22
+ @writing = options[:writing] ? true : false
23
+ self.config = options[:config] if options[:config].is_a?(Hash)
24
+ end
25
+
26
+ # Assigns the configuration to the instance.
27
+ #
28
+ # @param [Hash] hash The +Hash+ configuration.
29
+ # @return [Hash] The +Hash+ configuration.
30
+ def config=(hash)
31
+ raise ArgumentError, 'Invalid argument' if !hash.is_a?(Hash) || hash.empty?
32
+
33
+ keys = (hash.keys + hash.keys.map { |key| "#{key}=" }).map(&:to_sym)
34
+ system = self.instance_variables + self.class.instance_methods(true)
35
+
36
+ if (conflict = keys & system).empty?
37
+ @config = Hash.new
38
+ hash.each do |key, value|
39
+ if value.is_a?(Hash)
40
+ @config[key.to_sym] = self.class.new(config: value, writing: @writing)
41
+ else
42
+ @config[key.to_sym] = value
43
+ end
44
+ end
45
+ else
46
+ raise "The key(s) (#{conflict.join(', ')}) is already used by Ruby"
47
+ end
48
+ end
49
+
50
+ # Post treatment of call method which have failed.
51
+ #
52
+ # @param [Symbol] name The name of the method called.
53
+ # @param [Array] arguments The arguments provided with the call.
54
+ # @return [Object] The value of the config or call the super method.
55
+ def method_missing(name, *arguments)
56
+ if name[-1, 1] == '=' && @config.key?(name[0..-2].to_sym)
57
+ if @writing
58
+ @config[name[0..-2].to_sym] = arguments.first
59
+ else
60
+ raise 'The configuration is not allowed in writing'
61
+ end
62
+ elsif @config.key?(name)
63
+ @config[name]
64
+ else
65
+ super
66
+ end
67
+ end
68
+
69
+ # Returns the config like a +Hash+.
70
+ #
71
+ # @return [Hash] The original hash with keys symbolized.
72
+ def to_hash
73
+ hash = Hash.new
74
+ @config.each { |k, v| hash[k] = v.is_a?(self.class) ? v.to_hash : v }
75
+ hash
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module DotConfig
4
+
5
+ # This module defines the version of the gem.
6
+ #
7
+ # @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
8
+ module Version
9
+
10
+ # The major number version.
11
+ MAJOR = 0
12
+ # The minor number version.
13
+ MINOR = 0
14
+ # The tiny number version.
15
+ TINY = 2
16
+ # The pre realese number version.
17
+ PRE = nil
18
+ # The complete number version.
19
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
20
+ end
21
+ end
@@ -0,0 +1,85 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+
5
+ class DotConfig::ConfigurationTest < ActiveSupport::TestCase
6
+
7
+ test 'class is defined' do
8
+ assert DotConfig.const_defined?(:Configuration), 'Class undefined'
9
+ end
10
+
11
+ test 'default writing value' do
12
+ assert_equal false, Configuration.new.writing
13
+ assert_equal false, Configuration.new(writing: false).writing
14
+ assert_equal true, Configuration.new(writing: true).writing
15
+ end
16
+
17
+ test 'has new method' do
18
+ assert_respond_to Configuration, :new, 'Method undefined'
19
+
20
+ assert_nothing_raised(ArgumentError) do
21
+ Configuration.new
22
+ Configuration.new(writing: true)
23
+ Configuration.new(config: { hello: :world })
24
+ Configuration.new(config: { hello: :world }, writing: true)
25
+ end
26
+ end
27
+
28
+ test 'new with config' do
29
+ simple_hash = { language: 'Ruby', framework: 'Ruby on Rails' }
30
+ config = Configuration.new(config: simple_hash)
31
+
32
+ assert_equal simple_hash.keys, config.config.keys, 'Loosen keys'
33
+ simple_hash.each { |key, value| assert_equal value, config.send(key.to_sym) }
34
+ end
35
+
36
+ test 'has config= method' do
37
+ assert_respond_to Configuration.new, :config=, 'Method undefined'
38
+
39
+ assert_nothing_raised(ArgumentError) do
40
+ Configuration.new.config = { hello: :world }
41
+ Configuration.new(writing: true).config = { hello: :world }
42
+ end
43
+ end
44
+
45
+ test 'with nested config' do
46
+ nested_hash = { language: 'Ruby',
47
+ framework: { rails: [:active_record, :active_support] } }
48
+ config = Configuration.new(config: nested_hash)
49
+
50
+ assert_instance_of Configuration, config.framework, 'Not a Configuration'
51
+ assert_equal nested_hash[:framework][:rails], config.framework.rails
52
+ end
53
+
54
+ test 'getter method' do
55
+ nested_hash = { language: 'Ruby',
56
+ framework: { rails: [:active_record, :active_support] } }
57
+ config = Configuration.new(config: nested_hash)
58
+
59
+ assert_instance_of nested_hash[:language].class, config.language
60
+ assert_instance_of Configuration, config.framework
61
+ assert_instance_of nested_hash[:framework][:rails].class, config.framework.rails
62
+ end
63
+
64
+ test 'setter method' do
65
+ nested_hash = { language: 'Ruby',
66
+ framework: { rails: [:active_record, :active_support] } }
67
+
68
+ config = Configuration.new(config: nested_hash)
69
+ assert_raise(RuntimeError) { config.language = 'Python' }
70
+ assert_equal nested_hash[:language], config.language
71
+
72
+ config = Configuration.new(config: nested_hash, writing: true)
73
+ config.language = 'Python'
74
+ assert_equal 'Python', config.language
75
+ end
76
+
77
+ test 'to_hash method' do
78
+ assert_respond_to Configuration.new, :to_hash, 'Method undefined'
79
+
80
+ nested_hash = { language: 'Ruby',
81
+ framework: { rails: [:active_record, :active_support] } }
82
+ config = Configuration.new(config: nested_hash)
83
+ assert_equal config.to_hash, nested_hash
84
+ end
85
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+
5
+ class DotConfig::VersionTest < ActiveSupport::TestCase
6
+
7
+ test 'Version is defined' do
8
+ assert DotConfig.const_defined?(:Version), 'Module undefined'
9
+ end
10
+
11
+ test 'version constants is defined' do
12
+ assert Version.const_defined?(:MAJOR), 'MAJOR undefined'
13
+ assert Version.const_defined?(:MINOR), 'MINOR undefined'
14
+ assert Version.const_defined?(:TINY), 'TINY undefined'
15
+ assert Version.const_defined?(:PRE), 'PRE undefined'
16
+ assert Version.const_defined?(:STRING), 'STRING undefined'
17
+ end
18
+
19
+ test 'version string is equal' do
20
+ major, minor, tiny, pre = Version::STRING.split('.')
21
+
22
+ assert_equal Version::MAJOR.to_s, major, 'Major not equal'
23
+ assert_equal Version::MINOR.to_s, minor, 'Minor not equal'
24
+ assert_equal Version::TINY.to_s, tiny, 'Tiny not equal'
25
+ assert_equal Version::PRE, pre, 'Pre not equal'
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+
5
+ class DotConfigTest < ActiveSupport::TestCase
6
+
7
+ test 'module is defined' do
8
+ assert Object.const_defined?(:DotConfig), 'Module undefined'
9
+ end
10
+
11
+ test 'has new method' do
12
+ assert_respond_to DotConfig, :new, 'Method undefined'
13
+ assert_instance_of DotConfig::Configuration, DotConfig.new(world: 'hello')
14
+ assert_raise(ArgumentError) { DotConfig.new(:world) }
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'dot_config'
4
+ require 'active_support/test_case'
5
+ require 'test/unit'
6
+
7
+ # Add extra methods to ActiveSupport::TestCase.
8
+ #
9
+ # @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
10
+ class ActiveSupport::TestCase
11
+ include DotConfig
12
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotconfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Geoffrey Roguelon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
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: '3.0'
30
+ description: ! 'Simplify your configuration with: DotConfig.new(language: :ruby).language
31
+ #=> ''Ruby''.'
32
+ email: geoffrey.roguelon@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - CHANGELOG.md
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - dotconfig.gemspec
44
+ - lib/dot_config.rb
45
+ - lib/dot_config/configuration.rb
46
+ - lib/dot_config/version.rb
47
+ - test/dot_config/configuration_test.rb
48
+ - test/dot_config/version_test.rb
49
+ - test/dot_config_test.rb
50
+ - test/test_helper.rb
51
+ homepage: http://github.com/GRoguelon/DotConfig
52
+ licenses:
53
+ - MIT
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: 1.9.1
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.23
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: The DotConfig gem give you access to your configuration via the dot syntax.
76
+ You instantiate DotConfig::Configuration with a Hash and you will retrieve the values
77
+ in calling the key as method name.
78
+ test_files:
79
+ - test/dot_config/configuration_test.rb
80
+ - test/dot_config/version_test.rb
81
+ - test/dot_config_test.rb
82
+ - test/test_helper.rb
83
+ has_rdoc: false