configer 1.1.5 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e72459d74ed683e0d0529b0c53522e9fbc51f77
4
- data.tar.gz: 36aa9b4fc7e64b85f23596d0646dd60ca30e5afb
3
+ metadata.gz: 202d00d5d27f01cfa525c1276d2f2f15f9c52337
4
+ data.tar.gz: f9981a345e25028fdf592597691240dcc555c47c
5
5
  SHA512:
6
- metadata.gz: 8ab0982f807eb9f37915d1ca10f18e97b40174eaa20a10b66de4abf8baea51a3888c4e4eb16e751012cef2181a0a3e08d67d75fdc5ccd67c009422b9c36517ce
7
- data.tar.gz: ae1060fa007af42b1fd08adf4c5ddb2acc039f0b7b7ed5debe37befaa788dddb619cf641a912aae9a1067ad95f255711dce1e60c9e940f3ac958789484f9ecc3
6
+ metadata.gz: e1da607c49dad2517dc7768667b7bf47c70fb73d69beff18406a5d960da6d64365b6845b1e360f44edd677fe3c7570b2d0ccd471cbc9ff019604617b1e59fac8
7
+ data.tar.gz: 9611db7e29d26b23c11f395d12d7ef4bd0b062d3c8989c42ac1fc8d7b8c51715afeab99a688fdebb3f73929520f9ead9508dc34bf3418ef59fb1aef4e9bd37ef
data/README.md CHANGED
@@ -1,29 +1,61 @@
1
1
  configer
2
2
  ========
3
-
4
3
  super easy to use configuration module for ruby apps
5
4
 
5
+ ### install
6
+
7
+ $ gem install configer
8
+
6
9
  ### example
7
10
 
11
+ #### Lazy Config object
12
+
13
+ The configers default behavior is , when you put a yaml or a json file into the following directories
14
+ * app_root/lib/meta/**/*
15
+ * app_root/lib/meta/*
16
+ * app_root/lib/custom_libary_name/meta/*
17
+ * app_root/config/*
18
+ * app_root/config/environments/*
19
+
20
+ The meta tag can be aliased with META
21
+
22
+ The logic is the following:
23
+ * in the lib/meta/*
24
+ * will be merged into the __config__ hash object under the key of the file name
25
+ * in the lib/meta/folder/*
26
+ * will be merged into the __config__ object with the folder as main key and under that file name as key for the content
27
+ * in the lib/folder/meta/*
28
+ * will be merged into the __config__ object with the folder as main key, and the file names as sub keys followed by the content
29
+ * in the config/* && config/environments/*
30
+ * will be merged into the __config__ object as is. Deep merge will used so already existing keys will only be override partially
31
+ * the following is the order if yaml/json files names as enviornments
32
+ * default
33
+ * development
34
+ * test
35
+ * production
36
+
37
+ I personally say, put everything into the lib/gem_name/meta/* so you can have auto separated configs for each gem/module
38
+ The __config__ object will not be generated util it's being called.
39
+
40
+ #### Loading up Yaml and Json files from the application directory
41
+
42
+ You can mount JSON and yaml files with manually.
43
+ This will make key paths based on FileSystem logic
44
+
8
45
  ```ruby
9
46
 
10
- require_relative "../lib/configer.rb"
47
+ require "configer"
11
48
 
12
- #> you can use root/r/folder key,
13
- # for set home directory
49
+ #> optons:
14
50
  #
15
- #> you can point to an object if you want change the default __config__ obj
16
- # keys: o/out/to
51
+ # root/r/folder/dir/directory
52
+ # - set the folder where the mount will begin
17
53
  #
18
- Configer.mount_yaml
19
- Configer.mount_json
20
-
21
- # this will merge new objects by default to the __config__ object (or configer)
22
- puts __config__ #> you can use configer alias as well
23
-
24
- # for example we can call the dir.pwd folder's sample/meta/test.yml file parsed data as
25
- puts __config__.sample.meta.test #> { hello: "world" }
26
- puts __config__.sample.meta.test.hello #> "world"
54
+ # to/out/o
55
+ # - point to a hashlike object where you want the config objects to be merged
56
+ #
57
+ Configer.mount_yaml #> return Configer::Object that contain parsed yaml
58
+ Configer.mount_json #> return Configer::Object that contain parsed json
27
59
 
28
60
  ```
29
61
 
@@ -33,19 +65,11 @@ example for the mount options:
33
65
 
34
66
  require "configer"
35
67
 
36
- asdf= Configer.new( {hello: "world"} )
68
+ asdf = {hello: "world"}
37
69
 
38
70
  Configer.mount_yaml out: asdf
39
71
  Configer.mount_json out: asdf
40
72
 
41
- puts __config__
42
- #<Configer::ConfigObject>
43
-
44
73
  puts asdf
45
- #<Configer::ConfigObject hello="world"
46
- # sample=#<Configer::ConfigObject meta=#<Configer::ConfigObject
47
- # hello=#<Configer::ConfigObject hello="world">
48
- # test=#<Configer::ConfigObject hello="world">>>>
49
-
50
74
 
51
75
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.5
1
+ 1.2.0
@@ -14,8 +14,6 @@ Gem::Specification.new do |spec|
14
14
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
15
  spec.require_paths = ["lib"]
16
16
 
17
- spec.add_dependency "hashie"
18
-
19
17
  spec.add_development_dependency "bundler"
20
18
  spec.add_development_dependency "rake"
21
19
 
@@ -1,17 +1,8 @@
1
+ require "configer"
1
2
 
2
- require 'hashie'
3
+ asdf = {hello: "world"}
3
4
 
4
- %W[ data ].each do |name_tag|
5
- require File.expand_path File.join File.dirname(__FILE__),'..','lib','configer',name_tag
6
- end
5
+ Configer.mount_yaml out: asdf
6
+ Configer.mount_json out: asdf
7
7
 
8
- var = Configer.new(
9
- {
10
- capacity: {
11
- max: 9000
12
- }
13
- }
14
- )
15
-
16
- puts var
17
- # puts var.public_methods
8
+ puts asdf
@@ -1,5 +1,8 @@
1
- require 'hashie'
2
- require 'configer/data'
1
+ require 'configer/extension'
2
+ require 'configer/object'
3
+ require 'configer/support'
4
+
3
5
  require 'configer/json'
4
6
  require 'configer/yaml'
5
- require 'configer/ext'
7
+
8
+ require 'configer/dsl'
@@ -0,0 +1,14 @@
1
+
2
+ module Configer
3
+
4
+ module ObjectEXT
5
+
6
+ def __config__
7
+ ::Configer::Data.config_hash
8
+ end;alias __CONFIG__ __config__
9
+
10
+ end
11
+
12
+ end
13
+
14
+ Object.__send__ :include, Configer::ObjectEXT
@@ -0,0 +1,40 @@
1
+ module Configer
2
+
3
+ module HashExtension
4
+
5
+ def deep_merge(other_hash)
6
+
7
+ var = self.dup
8
+ var.__send__ :extend, HashExtension unless var.respond_to?(:deep_merge!)
9
+
10
+ return var.__send__ :deep_merge!, other_hash
11
+
12
+ end
13
+
14
+ def deep_merge!(other_hash)
15
+
16
+ other_hash.each_pair do |k,v|
17
+
18
+ self[k] = if self[k].class <= ::Hash && v.class <= ::Hash
19
+
20
+ [self[k],v].each do |obj|
21
+ obj.__send__ :extend, HashExtension unless obj.respond_to?(:deep_merge!)
22
+ end
23
+
24
+ self[k].deep_merge(v)
25
+
26
+ else
27
+ v
28
+ end
29
+
30
+ end
31
+
32
+ return self
33
+
34
+ end
35
+
36
+ end
37
+
38
+
39
+
40
+ end
@@ -7,13 +7,14 @@ module Configer
7
7
  raise(ArgumentError) unless opts.class <= Hash
8
8
  require 'json'
9
9
 
10
- opts[:root] ||= opts.delete(:r) || opts.delete(:folder) || Dir.pwd
11
- opts[:out] ||= opts.delete(:o) || opts.delete(:to) || Configer::Data.config_hash
12
-
13
- raise unless opts[:out].class <= Hash
14
- unless opts[:out].class == Configer::ConfigObject
15
- opts[:out]= Configer::ConfigObject.new( opts[:out] )
10
+ [:r,:folder,:dir,:directory].each do |sym|
11
+ opts[:root] ||= opts.delete(sym)
16
12
  end
13
+ opts[:root] ||= Dir.pwd
14
+
15
+ opts[:out] ||= opts.delete(:o) || opts.delete(:to) || Configer::Object
16
+ raise(ArgumentError,"out/to must point to hashlike object") unless opts[:out].class <= ::Hash
17
+ opts[:out].__send__ :extend, HashExtension unless opts[:out].respond_to?(:deep_merge!)
17
18
 
18
19
  Dir.glob( File.join( File.absolute_path(opts[:root]), "**","*.{json}" ) ).each do |file_path|
19
20
 
@@ -31,12 +32,13 @@ module Configer
31
32
 
32
33
  key_str= path_elements[index]
33
34
  (current_obj ||= tmp_hsh)
34
- current_obj[key_str]= {} #ConfigObject.new
35
+ current_obj[key_str]= {}
35
36
  current_obj= current_obj[key_str] unless index == (path_elements.count-1)
36
37
 
37
38
  }
38
39
 
39
40
  current_obj[ path_elements.last ]= JSON.parse File.read file_path
41
+
40
42
  opts[:out].deep_merge!(tmp_hsh)
41
43
 
42
44
  return nil
@@ -0,0 +1,86 @@
1
+ module Configer
2
+
3
+ class Object < ::Hash
4
+
5
+ self.instance_methods.each do |sym|
6
+ self.__send__ :protected, sym
7
+ end
8
+
9
+ def method_missing( method_name, *args )
10
+
11
+ if method_name.to_s[-1] == '='
12
+ self[method_name.to_s[0..-2]]= *args
13
+ return self[method_name.to_s[0..-2]]
14
+ else
15
+
16
+ if self[method_name.to_s].nil? && self.respond_to?(method_name)
17
+ return self.__send__(method_name)
18
+ else
19
+ return self[method_name.to_s]
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
26
+ public :__send__,:respond_to?
27
+
28
+ #> allowed Hash methods
29
+ public :to_s,:inspect,:delete,:delete_if,
30
+ :merge!,:merge,:keys,:values,:freeze
31
+
32
+ #> allowed Enumerable methods
33
+ public :each,:each_pair,:map,:reduce,:group_by,
34
+ :select,:to_a,:grep,:count,:size
35
+
36
+ #> allowed object methods
37
+ public :class,:dup
38
+
39
+ #> allowed boolean methods
40
+ public :==,:===,:include?
41
+
42
+ public
43
+
44
+ def [] key
45
+ key = key.to_s
46
+ super
47
+ end
48
+
49
+ def []= key,value
50
+ key = key.to_s
51
+ super
52
+ end
53
+
54
+ #> parse object
55
+ def self.parse obj
56
+ raise(ArgumentError,"input must be Hash") unless obj.is_a? ::Hash
57
+
58
+ hash = self.new.merge!(obj)
59
+ hash.each_pair do |key,value|
60
+ if value.class <= ::Hash
61
+ hash[key]= self.parse(value)
62
+ end
63
+ end
64
+ return hash
65
+
66
+ end
67
+
68
+ end
69
+
70
+ module Data
71
+ def self.config_hash
72
+ return @@config ||= Object.parse(Support.mount_config_and_lib_meta)
73
+ end
74
+ end
75
+
76
+ class << self
77
+
78
+ def new *args
79
+ self::Object.new(*args)
80
+ end
81
+
82
+ alias :init :new
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,110 @@
1
+ module Configer
2
+
3
+ module Support
4
+
5
+ #> return mounted config objects
6
+ def self.mount_config_and_lib_meta
7
+
8
+ return_hash = {}
9
+ return_hash.__send__ :extend, HashExtension
10
+
11
+ config_yaml_paths= []
12
+ config_yaml_paths.instance_eval do
13
+ def push_path(*paths)
14
+ paths.each do |path|
15
+ case true
16
+
17
+ when path.downcase.include?('default')
18
+ self.insert 0, path
19
+
20
+ when path.downcase.include?('development')
21
+ self.insert 1, path
22
+
23
+ when path.downcase.include?('test')
24
+ self.insert 2, path
25
+
26
+ when path.downcase.include?('production')
27
+ self.insert 3, path
28
+
29
+ else
30
+ self.push path
31
+
32
+ end
33
+ end
34
+ end;alias push_paths push_path
35
+ end
36
+
37
+ #> load lib meta folders files
38
+ if File.exist?(File.join(Dir.pwd,'lib'))
39
+ config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'lib','**','{meta,META}','*.{yaml,yml,json}'))
40
+ config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'lib','{meta,META}','**','*.{yaml,yml,json}'))
41
+ config_yaml_paths.compact!
42
+ end
43
+
44
+ #> load config folder
45
+ if File.exist?(File.join(Dir.pwd,'config'))
46
+
47
+ config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'config','*.{yaml,yml,json}'))
48
+ if File.exist?(File.join(Dir.pwd,'config','environments'))
49
+ config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'config','environments','*.{yaml,yml,json}'))
50
+ end
51
+ config_yaml_paths.compact!
52
+
53
+ end
54
+
55
+ config_yaml_paths.each do |path|
56
+
57
+ path_parts = path.split(File::Separator)
58
+
59
+ extension = path_parts.last.split('.')[-1]
60
+
61
+ category_key = case
62
+
63
+ #> lib/meta/*
64
+ when path_parts[-2].downcase == 'meta' && path_parts[-3] == 'lib'
65
+ nil
66
+
67
+ #> lib/meta/**/*
68
+ when path_parts[-3].downcase == 'meta' && path_parts[-4] == 'lib'
69
+ path_parts[-2]
70
+
71
+ #> lib/**/meta/*
72
+ when path_parts[-2].downcase == 'meta' && path_parts[-3] != 'lib' && path_parts[-4] == 'lib'
73
+ path_parts[-3]
74
+
75
+ else
76
+ nil
77
+
78
+ end
79
+
80
+ object = if %W[ yaml yml ].include?(extension)
81
+ require 'yaml'
82
+ YAML.safe_load(File.read(path))
83
+ elsif extension == 'json'
84
+ require 'json'
85
+ JSON.parse(File.read(path))
86
+ else
87
+ {}
88
+ end
89
+
90
+ if path.downcase.include?('meta')
91
+ object = {
92
+ ( path_parts.last.split('.')[-2] || path ) => object
93
+ }
94
+ end
95
+
96
+ if category_key.nil?
97
+ return_hash.deep_merge!(object)
98
+ else
99
+ return_hash.deep_merge!(category_key => object)
100
+ end
101
+
102
+ end
103
+
104
+ return return_hash
105
+
106
+ end
107
+
108
+
109
+ end
110
+ end
@@ -7,13 +7,14 @@ module Configer
7
7
  raise(ArgumentError) unless opts.class <= Hash
8
8
  require 'yaml'
9
9
 
10
- opts[:root] ||= opts.delete(:r) || opts.delete(:folder) || Dir.pwd
11
- opts[:out] ||= opts.delete(:o) || opts.delete(:to) || Configer::Data.config_hash
12
-
13
- raise unless opts[:out].class <= Hash
14
- unless opts[:out].class == Configer::ConfigObject
15
- opts[:out]= Configer::ConfigObject.new( opts[:out] )
10
+ [:r,:folder,:dir,:directory].each do |sym|
11
+ opts[:root] ||= opts.delete(sym)
16
12
  end
13
+ opts[:root] ||= Dir.pwd
14
+
15
+ opts[:out] ||= opts.delete(:o) || opts.delete(:to) || Configer::Object
16
+ raise(ArgumentError,"out/to must point to hashlike object") unless opts[:out].class <= ::Hash
17
+ opts[:out].__send__ :extend, HashExtension unless opts[:out].respond_to?(:deep_merge!)
17
18
 
18
19
  Dir.glob( File.join( File.absolute_path(opts[:root]), "**","*.{yaml,yml}" ) ).each do |file_path|
19
20
 
@@ -31,12 +32,13 @@ module Configer
31
32
 
32
33
  key_str= path_elements[index]
33
34
  (current_obj ||= tmp_hsh)
34
- current_obj[key_str]= {} #ConfigObject.new
35
+ current_obj[key_str]= {}
35
36
  current_obj= current_obj[key_str] unless index == (path_elements.count-1)
36
37
 
37
38
  }
38
39
 
39
40
  current_obj[ path_elements.last ]= YAML.safe_load File.read file_path
41
+
40
42
  opts[:out].deep_merge!(tmp_hsh)
41
43
 
42
44
  return nil
@@ -0,0 +1,5 @@
1
+ $TEST = true
2
+ require 'configer'
3
+
4
+ # var = Configer::Object.new
5
+ # var.hello = 'world'
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-03 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: hashie
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: bundler
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -68,14 +54,17 @@ files:
68
54
  - configer.gemspec
69
55
  - example/sample.rb
70
56
  - lib/configer.rb
71
- - lib/configer/data.rb
72
- - lib/configer/ext.rb
57
+ - lib/configer/dsl.rb
58
+ - lib/configer/extension.rb
73
59
  - lib/configer/json.rb
60
+ - lib/configer/object.rb
61
+ - lib/configer/support.rb
74
62
  - lib/configer/yaml.rb
75
63
  - sample/load_meta_files.rb
76
64
  - sample/meta/hello.json
77
65
  - sample/meta/test.yml
78
66
  - sample/mount_opts.rb
67
+ - sandbox/sup.rb
79
68
  homepage:
80
69
  licenses: []
81
70
  metadata: {}
@@ -1,23 +0,0 @@
1
-
2
- module Configer
3
-
4
- class Object < ::Hashie::Mash
5
- end
6
-
7
- module Data
8
- def self.config_hash
9
- @@config ||= Object.new
10
- end
11
- end
12
-
13
- class << self
14
-
15
- def new *args
16
- self::Object.new(*args)
17
- end
18
-
19
- alias :init :new
20
-
21
- end
22
-
23
- end
@@ -1,17 +0,0 @@
1
-
2
- module Configer
3
-
4
- module ObjectEXT
5
-
6
- def configer
7
- ::Configer::Data.config_hash
8
- end
9
-
10
- alias :__config__ :configer
11
- alias :__CONFIG__ :configer
12
-
13
- end
14
-
15
- end
16
-
17
- Object.__send__ :include, Configer::ObjectEXT