globals 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.
Files changed (2) hide show
  1. data/lib/globals.rb +64 -0
  2. metadata +45 -0
data/lib/globals.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'erb'
2
+ require 'rails'
3
+
4
+ class Globals
5
+ def self.read(globals_file, env=Rails.env)
6
+ env = env.to_s
7
+
8
+ yaml = YAML.load ERB.new(File.read globals_file).result
9
+ if yaml && yaml.include?(env)
10
+ new(yaml[env], env)
11
+ else
12
+ raise "Globals were not defined for environment: #{env} in #{globals_file}"
13
+ end
14
+ end
15
+
16
+ def initialize(globals, env)
17
+ @globals = globals
18
+ @environment = env
19
+ @cache = {}
20
+
21
+ unless @globals["feature"].nil?
22
+ @globals["feature"].each_pair do |k, v|
23
+ raise "A feature can only be true or false." if ![true, false].include?(v)
24
+ end
25
+ end
26
+
27
+ define_accessors
28
+ end
29
+
30
+ def override(override_file_path)
31
+ overrides = YAML.load(ERB.new(File.read(override_file_path)).result)[@environment]
32
+ if overrides
33
+ @cache.clear
34
+ recursive_merge @globals, overrides
35
+ define_accessors
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def define_accessors
42
+ @globals.each_pair do |key, value|
43
+ define_singleton_method key do
44
+ if value.is_a?(Hash)
45
+ # Cache the instances for efficiency and allowing stubbing.
46
+ @cache[key] ||= Globals.new(value, @environment)
47
+ else
48
+ value
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ def recursive_merge(a, b)
55
+ b.each_pair do |k, v|
56
+ if v.is_a? Hash
57
+ a[k] ||= {}
58
+ recursive_merge(a[k], v)
59
+ else
60
+ a[k] = v
61
+ end
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: globals
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sujoy Gupta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: sujoyg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/globals.rb
21
+ homepage: http://rubygems.org/gems/globals
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: A simple way to define globals for use in a rails application.
45
+ test_files: []