tree_config 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f0acdbdd68faf35290409c21419d9cd959bbb210
4
+ data.tar.gz: 22f33ea5cc1b2c098609d4e5f271b01546abe16a
5
+ SHA512:
6
+ metadata.gz: 03a7324b648ddcc40032f3c9da99b0224e4651332cfa79ff0b73e5bc8ac49c1d1746269420539577cbdfaf496c25a52a05f780879ba1be2d9556c6df471755c4
7
+ data.tar.gz: 010754a3702b5d258c389cec9e9869a7606c272192fede9d5178853dde2887c928ece8fad35d518231cd25354747eaff985af8249540721bdd4672ef602bcdee
data/.gitignore ADDED
@@ -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,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tree_config.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem "rake", "~> 10.0"
8
+ gem "rspec", "~> 2.14"
9
+ gem "awesome_print", "~> 1.2"
10
+ gem 'coveralls', require: false
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 huydx
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,50 @@
1
+ # TreeConfig
2
+ Rails very simple configuration gem, which use "folder structure" as settings key
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'tree_config'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install tree_config
17
+
18
+ ## Usage
19
+ Use rails generator to generate default config folder
20
+ ```
21
+ rails g tree_config:install
22
+ ```
23
+
24
+ This will generate `config/initializers/rails_config.rb` and generate a default config folder `config/settings` with `common.yml` file.
25
+
26
+ ## Accessing the Settings object
27
+ Generally, all settings will be set into global `Settings` variable.
28
+ Folder name, file name will be use as key, and content of file will be set as this key's value. For example, given the follow settings folder structure
29
+
30
+ ```
31
+ -config
32
+ |-folder1
33
+ -A.yml
34
+ -B.yml
35
+ |-folder2
36
+ |-folder3
37
+ -C.yml
38
+ ```
39
+
40
+ Then we can access A.yml content by `Settings.folder1.A`.
41
+ The content of A is access by properties notation (For example Settings.folder1.A.var1)
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( http://github.com/tree/tree_config/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
50
+ . Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/lib/tree_config'
@@ -0,0 +1,19 @@
1
+ module TreeConfig
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ desc "Generates a custom Hekk Config initializer file."
5
+
6
+ def self.source_root
7
+ @_tree_config_source_root ||= File.expand_path('../templates', __FILE__)
8
+ end
9
+
10
+ def copy_initializer
11
+ template "tree_config.rb", "config/initializers/tree_config.rb"
12
+ end
13
+
14
+ def copy_settings
15
+ directory "settings", "config/settings"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ TreeConfig.setup do |config|
2
+ config.const_name = "Settings"
3
+ end
@@ -0,0 +1,7 @@
1
+ module TreeConfig
2
+ class Error < StandardError; end
3
+ class RailsNotInitialized < Error; end
4
+ class KeyIsFixNum < Error; end
5
+ class FolderNotDefine < Error; end
6
+ end
7
+
@@ -0,0 +1,16 @@
1
+ module TreeConfig
2
+ module Rails
3
+ if defined?(Rails::Railtie)
4
+ class Railtie < Rails::Railtie
5
+ initializer :load_custom_hekk_config, :before => :load_environment_config, :group => :all do
6
+ initializer = Rails.root.join("config", "initializer", "hekk_config.rb")
7
+ require initializer if File.exists?(initializer)
8
+ end
9
+
10
+ initializer :load_hekk_config_settings, :after => :load_custom_rails_config, :before => :load_environment_config, :group => :all do
11
+ HekkConfig.load(Rails.root.join("config", "settings"))
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ module TreeConfig
2
+ class DeepStruct < OpenStruct
3
+ def initialize(hash=nil)
4
+ @table = {}
5
+ @hash_table = {}
6
+
7
+ if hash
8
+ hash.each do |k,v|
9
+ raise KeyIsFixNum if k.instance_of?(Fixnum)
10
+ @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
11
+ @hash_table[k.to_sym] = v
12
+ new_ostruct_member(k)
13
+ end
14
+ end
15
+ end
16
+
17
+ def to_h
18
+ @hash_table
19
+ end
20
+
21
+ def empty?
22
+ self.hash == 0
23
+ end
24
+
25
+ def size
26
+ sum = 0
27
+ self.each_pair { |p| sum += 1 }
28
+ sum
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ require 'ostruct'
2
+ require 'yaml'
3
+
4
+ module TreeConfig
5
+ class DirectoryTree
6
+ def self.load(dir, struct_obj)
7
+ Dir.foreach(dir) do |entry|
8
+ next if (entry == '..' || entry == '.')
9
+ full_path = File.join(dir, entry)
10
+ if File.directory? (full_path)
11
+ struct_obj.send("#{entry}=", DeepStruct.new)
12
+ struct_obj.send("#{entry}=", self.load(full_path, struct_obj.send("#{entry}")))
13
+ else
14
+ hash = ActiveSupport::HashWithIndifferentAccess.new(
15
+ begin
16
+ YAML.load(File.open(full_path))
17
+ rescue
18
+ end
19
+ )
20
+ name = File.basename(entry, '.*')
21
+ struct_obj.send("#{name}=", DeepStruct.new(hash))
22
+ end
23
+ end
24
+ return struct_obj
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module TreeConfig
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ require "tree_config/version"
2
+ require 'active_support/core_ext'
3
+ require 'tree_config/vendor/directory_tree'
4
+ require 'tree_config/vendor/deep_struct'
5
+ require 'tree_config/error'
6
+
7
+ module TreeConfig
8
+ @@_ran_once = false
9
+
10
+ mattr_accessor :const_name
11
+ @@const_name = "Settings"
12
+
13
+ def default_config_folder
14
+ @@folder = "setting"
15
+ end
16
+
17
+ def self.setup
18
+ yield self if @@_ran_once == false
19
+ @@_ran_once = true
20
+ end
21
+
22
+ def self._load(root_folder)
23
+ return DirectoryTree.load(root_folder, DeepStruct.new)
24
+ end
25
+
26
+ def self.load(root_folder=nil)
27
+ raise FolderNotDefine unless root_folder and File.directory? root_folder
28
+ Kernel.send(:remove_const, TreeConfig.const_name) if Kernel.const_defined?(TreeConfig.const_name)
29
+ Kernel.const_set(TreeConfig.const_name, TreeConfig._load(root_folder))
30
+ end
31
+ end
32
+
33
+ require('tree_config/integration/rails') if defined?(::Rails)
@@ -0,0 +1,6 @@
1
+ A:
2
+ a1: bar
3
+ a2: baz
4
+ B:
5
+ a3:
6
+ a4: foo
@@ -0,0 +1,6 @@
1
+ A:
2
+ a1: bar
3
+ a2: baz
4
+ B:
5
+ a3:
6
+ a4: foo
@@ -0,0 +1,6 @@
1
+ A:
2
+ a1: bar
3
+ a2: baz
4
+ B:
5
+ a3:
6
+ a4: foo
@@ -0,0 +1,6 @@
1
+ A:
2
+ a1: bar
3
+ a2: baz
4
+ B:
5
+ a3:
6
+ a4: foo
@@ -0,0 +1,3 @@
1
+ size: 1
2
+ server: google.com
3
+ 1: "one"
@@ -0,0 +1,6 @@
1
+ A:
2
+ a1: bar
3
+ a2: baz
4
+ B:
5
+ a3:
6
+ a4: foo
@@ -0,0 +1,6 @@
1
+ A:
2
+ a1: bar
3
+ a2: baz
4
+ B:
5
+ a3:
6
+ a4: foo
@@ -0,0 +1,16 @@
1
+ require 'tree_config'
2
+ require 'pathname'
3
+ require 'bundler/setup'
4
+
5
+ RSpec.configure do |c|
6
+ c.color = true
7
+ c.run_all_when_everything_filtered = true
8
+ c.before(:all) do
9
+ @fixture_path = Pathname.new(File.join(File.dirname(__FILE__), '/fixtures'))
10
+ raise "Fixture folder not found" unless @fixture_path.directory?
11
+ end
12
+
13
+ def setting_folder(folder_name)
14
+ @fixture_path.join(folder_name)
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe TreeConfig do
4
+ it "should load common setting file" do
5
+ config = TreeConfig.load(setting_folder("empty"))
6
+ config.size.should == 0
7
+ end
8
+
9
+ it "should not load fixnum key in yml" do
10
+ expect { TreeConfig.load(setting_folder("onefile_num")) }.to raise_error(TreeConfig::KeyIsFixNum)
11
+ end
12
+
13
+ it "should load one file" do
14
+ config = TreeConfig.load(setting_folder("onefile"))
15
+ config.test.size.should == 2
16
+ config.test.A.size.should == 2
17
+ config.test.A.a1.should == "bar"
18
+ config.test.A.a2.should == "baz"
19
+ config.test.B.a3.a4.should == "foo"
20
+ end
21
+
22
+ it "should load two file" do
23
+ config = TreeConfig.load(setting_folder("twofile"))
24
+ config.size.should == 2
25
+ config.test1.A.a1.should == "bar"
26
+ config.test2.A.a1.should == "bar"
27
+ end
28
+
29
+ it "should load nested folder" do
30
+ config = TreeConfig.load(setting_folder("nest1"))
31
+ config.size.should == 1
32
+ config.A1.test.A.a1.should == "bar"
33
+ end
34
+
35
+ it "should load complex nested folder" do
36
+ config = TreeConfig.load(setting_folder("nest2"))
37
+ config.size.should == 2
38
+ config.B.size.should == 0
39
+ config.A.size.should == 2
40
+ config.A.test.A.a1.should == "bar"
41
+ config.A.C.test.A.a1.should == "bar"
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tree_config/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tree_config"
8
+ spec.version = TreeConfig::VERSION
9
+ spec.authors = ["huydx"]
10
+ spec.email = ["doxuanhuy@gmail.com"]
11
+ spec.summary = %q{Rails config store gem}
12
+ spec.description = %q{Rails config store gem used for project has complex config structure}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", ">=3.0"
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tree_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - huydx
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rails config store gem used for project has complex config structure
70
+ email:
71
+ - doxuanhuy@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - init.rb
82
+ - lib/generators/tree_config/install_generator.rb
83
+ - lib/generators/tree_config/templates/settings/common.yml
84
+ - lib/generators/tree_config/templates/tree_config.rb
85
+ - lib/tree_config.rb
86
+ - lib/tree_config/error.rb
87
+ - lib/tree_config/integration/rails.rb
88
+ - lib/tree_config/vendor/deep_struct.rb
89
+ - lib/tree_config/vendor/directory_tree.rb
90
+ - lib/tree_config/version.rb
91
+ - spec/fixtures/nest1/A1/test.yml
92
+ - spec/fixtures/nest2/A/C/test.yml
93
+ - spec/fixtures/nest2/A/test.yml
94
+ - spec/fixtures/onefile/test.yml
95
+ - spec/fixtures/onefile_num/test.yml
96
+ - spec/fixtures/twofile/test1.yml
97
+ - spec/fixtures/twofile/test2.yml
98
+ - spec/spec_helper.rb
99
+ - spec/tree_config_spec.rb
100
+ - tree_config.gemspec
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Rails config store gem
125
+ test_files:
126
+ - spec/fixtures/nest1/A1/test.yml
127
+ - spec/fixtures/nest2/A/C/test.yml
128
+ - spec/fixtures/nest2/A/test.yml
129
+ - spec/fixtures/onefile/test.yml
130
+ - spec/fixtures/onefile_num/test.yml
131
+ - spec/fixtures/twofile/test1.yml
132
+ - spec/fixtures/twofile/test2.yml
133
+ - spec/spec_helper.rb
134
+ - spec/tree_config_spec.rb
135
+ has_rdoc: