hashconfig 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
+ *.swp
19
+ spec/testfiles/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hashconfig.gemspec
4
+ gemspec
5
+ gem 'rspec'
data/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ (General Public License)
2
+
3
+ Copyright (c) 2012 Philipp Böhm
4
+
5
+ This program is free software; you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation in version 3 of the License.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program; if not, write to the Free Software
16
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ MA 02110-1301, USA.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Hashconfig
2
+
3
+ Extends the Ruby Hash class with a method (merge_with_serialized) that
4
+ merges a hash instance with an serialized (YAML) instance of a hash. So
5
+ it is a lightweight mechanism for configuration.
6
+
7
+ [http://github.com/pboehm/hashconfig](http://github.com/pboehm/hashconfig)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'hashconfig'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hashconfig
22
+
23
+ ## Usage
24
+
25
+ require 'hashconfig'
26
+
27
+ STANDARD_CONFIG = {
28
+ :default_directory => File.join(File.expand_path("~"), "Downloads"),
29
+ :store_episode_info => false,
30
+ :store_path => File.join(CONFIG_DIR, "information_storage.yml"),
31
+ :byte_count_for_md5 => 2048,
32
+ }
33
+
34
+ config = STANDARD_CONFIG.merge_with_serialized('path/to/yaml/file')
35
+
36
+ # config now holds the data from STANDARD_CONFIG updated with values from
37
+ # the yaml file
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hashconfig/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Philipp Böhm"]
6
+ gem.email = ["philipp@i77i.de"]
7
+ gem.description = %q{Simple configuration mechanism through serialized hash}
8
+ gem.summary = %q{Simple configuration mechanism through serialized hash}
9
+ gem.homepage = "http://github.com/pboehm/hashconfig"
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 = "hashconfig"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Hashconfig::VERSION
17
+ end
@@ -0,0 +1,3 @@
1
+ module Hashconfig
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hashconfig.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "hashconfig/version"
2
+ require 'yaml'
3
+
4
+ module Hashconfig
5
+
6
+ class ::Hash
7
+
8
+ # this method merges a YAML-serialized hash-instance
9
+ # with self. if the given File is not exisiting this will write
10
+ # a serialized version of self to this file.
11
+ #
12
+ # if the given yaml file does not contain a serialized hash than it
13
+ # merges with an empty hash that returns self unchanged.
14
+ #
15
+ # returns a new Hash merged with deserialized version from the file
16
+ def merge_with_serialized(yaml_file)
17
+
18
+ unless File.file? yaml_file
19
+ File.open(yaml_file, 'w') {|f| f.write(self.to_yaml) }
20
+ end
21
+
22
+ persistent_config = YAML.load(File.new(yaml_file, "rb").read)
23
+ persistent_config = Hash.new unless persistent_config.is_a? Hash
24
+
25
+ config = self.merge(persistent_config)
26
+ File.open(yaml_file, 'w') {|f| f.write(config.to_yaml) }
27
+
28
+ return config
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ require 'yaml'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/hashconfig'
5
+
6
+ TEST_DATA = {
7
+ :first => "first parameter",
8
+ :second => "second parameter",
9
+ :third => "third parameter"
10
+ }
11
+
12
+ TEST_DIR = File.join( File.dirname(__FILE__), "testfiles")
13
+ TEST_CONFIG_FILE = File.join(TEST_DIR, "config.yml")
14
+ TEST_EMPTY_CONFIG_FILE = File.join(TEST_DIR, "empty.yml")
15
+ TEST_INVALID_CONFIG_FILE = File.join(TEST_DIR, "invalid.yml")
16
+ TEST_NOT_EXISTING = File.join(TEST_DIR, "not_exisiting.yml")
17
+
18
+
19
+ describe Hashconfig do
20
+
21
+ before(:all) do
22
+
23
+ @modified = TEST_DATA.merge({:third => "modified third parameter"})
24
+
25
+ FileUtils.mkdir(TEST_DIR) unless File.directory? TEST_DIR
26
+ File.open(TEST_CONFIG_FILE, 'w') {|f| f.write(@modified.to_yaml) }
27
+ File.open(TEST_EMPTY_CONFIG_FILE, 'w') {|f| f.write(Hash.new.to_yaml) }
28
+ File.open(TEST_EMPTY_CONFIG_FILE, 'w') {|f| f.write("invalid") }
29
+
30
+ end
31
+
32
+ after(:all) do
33
+ FileUtils.remove_dir(TEST_DIR) if File.directory? TEST_DIR
34
+ end
35
+
36
+ it "should add a method to the Ruby Hash class" do
37
+ Hash.new.respond_to?(:merge_with_serialized).should be_true
38
+ end
39
+
40
+ it "should return an updated hash when yaml file is valid" do
41
+ config = TEST_DATA.merge_with_serialized(TEST_CONFIG_FILE)
42
+ config[:third].should eq "modified third parameter"
43
+ end
44
+
45
+ it "should return original hash when yaml file is empty" do
46
+ config = TEST_DATA.merge_with_serialized(TEST_EMPTY_CONFIG_FILE)
47
+ config[:third].should eq "third parameter"
48
+ end
49
+
50
+ it "should return original hash when yaml file is invalid" do
51
+ config = TEST_DATA.merge_with_serialized(TEST_INVALID_CONFIG_FILE)
52
+ config[:third].should eq "third parameter"
53
+ end
54
+
55
+ it "should return original hash when yaml file is not exisiting" do
56
+ config = TEST_DATA.merge_with_serialized(TEST_INVALID_CONFIG_FILE)
57
+ config[:third].should eq "third parameter"
58
+ end
59
+
60
+ it "should update the yaml when it is merged with a different hash" do
61
+ updated = TEST_DATA.merge({:fourth => "parameter number 4"})
62
+ config = updated.merge_with_serialized(TEST_CONFIG_FILE)
63
+ config[:fourth].should eq "parameter number 4"
64
+ end
65
+ end
66
+
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ require 'fileutils'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashconfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philipp Böhm
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple configuration mechanism through serialized hash
15
+ email:
16
+ - philipp@i77i.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - hashconfig.gemspec
28
+ - lib/hashconfig.rb
29
+ - lib/hashconfig/version.rb
30
+ - spec/hashconfig_spec.rb
31
+ - spec/spec_helper.rb
32
+ homepage: http://github.com/pboehm/hashconfig
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.19
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Simple configuration mechanism through serialized hash
56
+ test_files:
57
+ - spec/hashconfig_spec.rb
58
+ - spec/spec_helper.rb