config_man 0.0.1

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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in config_man.gemspec
4
+ gemspec
@@ -0,0 +1,32 @@
1
+ # ConfigMan
2
+
3
+ ConfigMan is a simple library, without external dependencies, to load config files and expose an interface on top of them.
4
+
5
+ ## How to Use
6
+
7
+ Let's say for example we have this yaml config file:
8
+
9
+ ```yaml
10
+ foo:
11
+ bar:
12
+ baz: qux
13
+ barbar: [1,2,3]
14
+ bazbaz:
15
+ foofoo:
16
+ barbar: qux
17
+ ```
18
+
19
+ We can load it like this:
20
+
21
+ ```ruby
22
+ config = ConfigMan::Loader.new('config/config.yml')
23
+ config.foo.bar.baz # "qux"
24
+ config.foo.barbar # [1,2,3]
25
+ config.foo.bazbaz.foofoo.barbar # "qux"
26
+ ```
27
+
28
+ Instead of acessing it directly, we can also export as a hash:
29
+
30
+ ```ruby
31
+ config.foo.to_hash # {bar: {baz: "qux"}, barbar: [1,2,3], ...}
32
+ ```
@@ -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/config_man/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Cainã Costa"]
6
+ gem.email = ["cainan.costa@gmail.com"]
7
+ gem.description = %q{Simplifies repetitive work of parsing configuration files}
8
+ gem.summary = %q{Simplifies repetitive work of parsing configuration files}
9
+ gem.homepage = "https://github.com/sryche/config_man"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "config_man"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ConfigMan::VERSION
17
+ end
@@ -0,0 +1,41 @@
1
+ require 'yaml'
2
+ require 'ostruct'
3
+ require 'delegate'
4
+ require "config_man/version"
5
+
6
+ module ConfigMan
7
+ class Item < OpenStruct
8
+ def initialize(attributes)
9
+ @attributes = attributes
10
+
11
+ attributes = attributes.map do |k,v|
12
+ v = self.class.new(v) if v.is_a? Hash
13
+ [k,v]
14
+ end
15
+
16
+ super(attributes)
17
+ end
18
+
19
+ def to_hash
20
+ @attributes
21
+ end
22
+ end
23
+
24
+ class Loader < SimpleDelegator
25
+ def initialize(contents)
26
+ struct = YAML.load(contents).map do |k,v|
27
+ v = Item.new(symbolize_keys(v)) if v.is_a? Hash
28
+ [k,v]
29
+ end
30
+
31
+ super(Item.new(Hash[struct]))
32
+ end
33
+
34
+ private
35
+ # Borrowed from ActiveSupport
36
+ def symbolize_keys(hash)
37
+ map = hash.map { |(k,v)| [(k.to_sym rescue k), v] }
38
+ Hash[map]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module ConfigMan
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
2
+ require 'minitest/autorun'
3
+ require 'config_man'
4
+
5
+ class ConfigLoaderTest < MiniTest::Unit::TestCase
6
+ def sample
7
+ @sample ||= { 'foo' => 'bar',
8
+ 'baz' => {'foo' => 'bar'},
9
+ 'qux' => {'foo' => {'bar' => 'baz'}}
10
+ }.to_yaml
11
+ end
12
+
13
+ def subject
14
+ @subject ||= ConfigMan::Loader.new(sample)
15
+ end
16
+
17
+ def assert_is_a(target, klass, msg = nil)
18
+ msg ||= "Expected #{target} to be a #{klass}"
19
+ assert target.is_a?(klass), msg
20
+ end
21
+
22
+ def test_generates_methods_for_top_level_configurations
23
+ assert_equal 'bar', subject.foo
24
+ end
25
+
26
+ def test_generates_methods_recursively
27
+ assert_is_a subject.baz, ConfigMan::Item
28
+ assert_is_a subject.qux, ConfigMan::Item
29
+ assert_is_a subject.qux.foo, ConfigMan::Item
30
+
31
+ assert_equal 'bar', subject.baz.foo
32
+ assert_equal 'baz', subject.qux.foo.bar
33
+ end
34
+
35
+ def test_export_keys_to_hashes
36
+ assert_equal({foo: 'bar'}, subject.baz.to_hash)
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_man
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cainã Costa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-14 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Simplifies repetitive work of parsing configuration files
15
+ email:
16
+ - cainan.costa@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - config_man.gemspec
26
+ - lib/config_man.rb
27
+ - lib/config_man/version.rb
28
+ - test/config_man_test.rb
29
+ homepage: https://github.com/sryche/config_man
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.10
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Simplifies repetitive work of parsing configuration files
53
+ test_files:
54
+ - test/config_man_test.rb