globals 0.1.3 → 0.1.4

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/globals.rb +20 -34
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7542984347db703149f4405cb03327ad4887eb3d
4
- data.tar.gz: e9c740c9a8b129c56f41a091b10dc67e0a78a2f6
3
+ metadata.gz: 8f1e3877c0a1ad58b4d779ccb9fc27c3b7884dd7
4
+ data.tar.gz: 5d98f47841f74d2dde7a69de8b8ca943b0001345
5
5
  SHA512:
6
- metadata.gz: 53299d7c08715a7e8f8dafff7af306855ae83a1ffd4165c9f21893576f915e20585e4535317128eb879a7b862d1f0635e0c5e1a8c0de67983097836d3e364e16
7
- data.tar.gz: 4c24aca1a3898e0248f4fc4aecb6c27541dfc2839b537185a81ceffafc932cc5597478bf3d1bc1d30f75004d0ea777674ff827ceebb584515ae66f5fa3fac3cf
6
+ metadata.gz: 8618d0aac56e4c24c9db766bdeb468c35455152619d16932287ad1ab90ad0f5bec8abff897251dcecb575306e20e016875529ebe1a3295e3da98c7ef10107202
7
+ data.tar.gz: 6e448deea08f5dc25df480b002863b79addc7dab18d4e66634b5757faca379fe362eb80c258ab06e7d98dd976e657da1d47b155c586b04bebf9980c5f98e6f86
data/lib/globals.rb CHANGED
@@ -4,8 +4,7 @@ require 'yaml'
4
4
  class Hash
5
5
  def recursive_merge!(that)
6
6
  that.each_pair do |k, v|
7
- if v.is_a? Hash
8
- self[k] ||= {}
7
+ if v.is_a?(Hash) && self[k].is_a?(Hash)
9
8
  self[k].recursive_merge!(v)
10
9
  else
11
10
  self[k] = v
@@ -15,42 +14,40 @@ class Hash
15
14
  end
16
15
 
17
16
  class Globals
18
- def self.load(globals_file, env)
19
- yaml = YAML.load ERB.new(File.read globals_file).result
20
- globals = yaml['defaults'] || {}
21
- globals.recursive_merge!(yaml[env] || {})
22
- globals
23
- end
24
-
25
- def self.read(globals_file, env='development')
26
- raise "#{globals_file} does not exist." unless File.exists? globals_file
27
-
28
- globals = load(globals_file, env)
17
+ def initialize(hash, env)
18
+ @globals = hash
19
+ @environment = env
20
+ @cache = {}
29
21
 
30
- new(globals, env)
22
+ define_accessors
31
23
  end
32
24
 
33
- def initialize(globals, env)
34
- @globals = globals
35
- @environment = env
36
- @cache = {}
25
+ def self.load(filename, env)
26
+ yaml = YAML.load ERB.new(File.read filename).result
27
+ hash = yaml['defaults'] || {}
28
+ hash.recursive_merge!(yaml[env] || {})
37
29
 
38
- unless @globals["feature"].nil?
39
- @globals["feature"].each_pair do |k, v|
30
+ unless hash["feature"].nil?
31
+ hash["feature"].each_pair do |k, v|
40
32
  raise "A feature can only be true or false." if ![true, false].include?(v)
41
33
  end
42
34
  end
43
35
 
44
- define_accessors
36
+ hash
37
+ end
38
+
39
+ def self.read(filename, env='development')
40
+ env = env.to_s
41
+ hash = load(filename, env)
42
+ new(hash, env)
45
43
  end
46
44
 
47
45
  def override(override_file_path)
48
- puts "Override file is " + override_file_path
49
46
  overrides = self.class.load(override_file_path, @environment)
50
47
 
51
48
  if overrides
52
- @cache.clear
53
49
  @globals.recursive_merge! overrides
50
+ @cache.clear
54
51
  define_accessors
55
52
  end
56
53
 
@@ -75,15 +72,4 @@ class Globals
75
72
  end
76
73
  end
77
74
  end
78
-
79
- def self.recursive_merge(a, b)
80
- b.each_pair do |k, v|
81
- if v.is_a? Hash
82
- a[k] ||= {}
83
- recursive_merge(a[k], v)
84
- else
85
- a[k] = v
86
- end
87
- end
88
- end
89
75
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globals
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sujoy Gupta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-12 00:00:00.000000000 Z
11
+ date: 2013-11-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A simple way to define globals for use in a rails application.
13
+ description: A simple way to define globals for use in a Ruby application.
14
14
  email: sujoyg@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -40,5 +40,5 @@ rubyforge_project:
40
40
  rubygems_version: 2.1.4
41
41
  signing_key:
42
42
  specification_version: 4
43
- summary: A simple way to define globals for use in a rails application.
43
+ summary: A simple way to define globals for use in a Ruby application.
44
44
  test_files: []