gaston 0.0.1 → 0.0.2

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.
data/lib/gaston.rb CHANGED
@@ -6,6 +6,7 @@ class Gaston
6
6
  include Singleton
7
7
  require 'gaston/configuration'
8
8
  require 'gaston/store'
9
+ require 'gaston/parse'
9
10
 
10
11
  # Parse yml config files, merge them, and store into
11
12
  # gaston::Store
@@ -13,14 +14,15 @@ class Gaston
13
14
  # @since 0.0.1
14
15
  #
15
16
  def store
16
- @store ||= Gaston::Store.new (files.inject({}) {|hash, config|
17
- parse = YAML.load_file(config)[env]
18
- hash.merge(parse) if parse
19
- })
17
+ @store ||= Gaston::Store.new(hash_from_files)
20
18
  end
21
19
 
22
20
  class << self
23
21
 
22
+ # Access to Gaston::Store.
23
+ #
24
+ # @since 0.0.1
25
+ #
24
26
  def retrieve
25
27
  self.instance.store
26
28
  end
@@ -62,4 +64,14 @@ class Gaston
62
64
  Gaston::Configuration.env.to_sym
63
65
  end
64
66
 
67
+ # Return one merged hash from yaml config files.
68
+ #
69
+ # @return [ Hash ]
70
+ #
71
+ # @since 0.0.2
72
+ #
73
+ def hash_from_files
74
+ @hash_from_files ||= Gaston::Parse.new(files, env).to_hash
75
+ end
76
+
65
77
  end # Gaston
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ class Gaston
3
+ class Parse
4
+
5
+ # Initialize Parsing.
6
+ #
7
+ # @param [ Array ] List of yaml files.
8
+ # @param [ Symbol ] Environment.
9
+ #
10
+ # @since 0.0.2
11
+ #
12
+ def initialize(files, env)
13
+ @env = env
14
+ @hash = files.inject({}) do |hash, file|
15
+ @parse = YAML.load_file(file) || {}
16
+ hash.merge(deep_merge_hash(default_values, env_values))
17
+ end
18
+ end
19
+
20
+ # @return [ Hash ]
21
+ #
22
+ # @since 0.0.2
23
+ #
24
+ def to_hash
25
+ @hash
26
+ end
27
+
28
+ private
29
+
30
+ # Default values from yaml.
31
+ #
32
+ # @return [ Hash ]
33
+ #
34
+ # @since 0.0.2
35
+ #
36
+ def default_values
37
+ @parse[:gaston] || {}
38
+ end
39
+
40
+ # Env values from hash.
41
+ #
42
+ # @return [ Hash ]
43
+ #
44
+ # @since 0.0.2
45
+ #
46
+ def env_values
47
+ @parse[@env] || {}
48
+ end
49
+
50
+ # Deep merge on hash.
51
+ #
52
+ # First hash will receive merge from second hash.
53
+ #
54
+ # @param [ Hash ]
55
+ # @param [ Hash ]
56
+ #
57
+ # @return [ Hash]
58
+ #
59
+ # @since 0.0.2
60
+ #
61
+ def deep_merge_hash(hash1,hash2)
62
+ hash2.each_key do |k1|
63
+ if hash1.key?(k1) && hash2[k1].is_a?(Hash)
64
+ deep_merge_hash(hash1[k1],hash2[k1])
65
+ else
66
+ hash1[k1] = hash2[k1]
67
+ end
68
+ end
69
+ hash1
70
+ end
71
+ end
72
+ end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  class Gaston
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gaston
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-28 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2011-09-30 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rspec
17
- requirement: &20411480 !ruby/object:Gem::Requirement
16
+ requirement: &17328900 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
@@ -22,7 +21,7 @@ dependencies:
22
21
  version: '2.6'
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *20411480
24
+ version_requirements: *17328900
26
25
  description: ! '[Dead simple Ruby config store.]'
27
26
  email: jboyer@af83.com
28
27
  executables: []
@@ -31,9 +30,9 @@ extra_rdoc_files: []
31
30
  files:
32
31
  - lib/gaston.rb
33
32
  - lib/gaston/configuration.rb
33
+ - lib/gaston/parse.rb
34
34
  - lib/gaston/store.rb
35
35
  - lib/gaston/version.rb
36
- has_rdoc: true
37
36
  homepage: https://github.com/chatgris/gaston
38
37
  licenses: []
39
38
  post_install_message:
@@ -54,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
53
  version: '0'
55
54
  requirements: []
56
55
  rubyforge_project:
57
- rubygems_version: 1.6.2
56
+ rubygems_version: 1.8.10
58
57
  signing_key:
59
58
  specification_version: 3
60
59
  summary: ! '[Dead simple Ruby config store.]'