ribbon-config 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f1d743fbe5e118ebb6ee44cc23cca481c1c3aa52
4
+ data.tar.gz: f15ea8e3fb99965bce5690f3b5952f4ea863e865
5
+ SHA512:
6
+ metadata.gz: 8d484ca6adf9f7169d78635d3b4b23f93bc6b71845c679c8a959fdb5b4f036b84870ac4914630c9307091c4bf0409fdfb61709b1067c27da19ef4b606486ef62
7
+ data.tar.gz: 5a2d13095b85a000835cbafd53f2f8a46926b6f707c41f943311f6af661747ae9ff8e6ee8eaafffabcc74bbe7023ae5fdbd6c64e89c88e9dc55c683b5777fd8d
@@ -0,0 +1,134 @@
1
+ require 'ribbon/config/version'
2
+
3
+ require 'yaml'
4
+
5
+ module Ribbon
6
+ class Config
7
+ # Putting this here instead of in Errors because I may move this into a
8
+ # separate gem.
9
+ class ConfigError < StandardError; end
10
+ class NotAddableError < ConfigError; end
11
+
12
+ class ProcArray < Array
13
+ def push(*procs)
14
+ raise ConfigError, "May only add blocks" unless procs.all? { |p| p.is_a?(Proc) }
15
+ super
16
+ end
17
+
18
+ def call(*args, &block)
19
+ map { |x| x.call(*args, &block) }
20
+ end
21
+ end
22
+
23
+ class UndefinedValue
24
+ def !
25
+ true
26
+ end
27
+ end
28
+
29
+ attr_reader :name
30
+
31
+ def initialize(name=nil, &block)
32
+ @name = name
33
+ define(&block) if block_given?
34
+ end
35
+
36
+ def define(&block)
37
+ case block.arity
38
+ when 0 then instance_eval(&block)
39
+ when 1 then block.call(self)
40
+ else raise ConfigError, 'invalid config block arity'
41
+ end
42
+ end
43
+
44
+ # Deep dup all the values.
45
+ def dup
46
+ super.tap do |new_config|
47
+ new_config.instance_variable_set(
48
+ :@_nested,
49
+ Hash[
50
+ _nested.map { |key, object|
51
+ [
52
+ key,
53
+ object.is_a?(Config) ? object.dup : object
54
+ ]
55
+ }
56
+ ]
57
+ )
58
+ end
59
+ end
60
+
61
+ def method_missing(meth, *args, &block)
62
+ meth_str = meth.to_s
63
+
64
+ if /^(\w+)\=$/.match(meth_str)
65
+ _set($1, *args, &block)
66
+ elsif args.length > 0 || block_given?
67
+ _add(meth, *args, &block)
68
+ elsif /^(\w+)\?$/.match(meth_str)
69
+ !!_get($1)
70
+ else
71
+ _get_or_create_namespace(meth)
72
+ end
73
+ end
74
+
75
+ def merge_hash!(hash)
76
+ hash.each { |k, v|
77
+ if v.is_a?(Hash)
78
+ send(k).merge_hash!(v)
79
+ else
80
+ send("#{k}=", v)
81
+ end
82
+ }
83
+
84
+ self
85
+ end
86
+
87
+ def merge_config_file!(file)
88
+ merge_hash!(YAML.load_file(file))
89
+ end
90
+
91
+ private
92
+ def _get(key)
93
+ _nested[key.to_sym]
94
+ end
95
+
96
+ def _get_or_create_namespace(key)
97
+ object = _get(key)
98
+
99
+ if object.is_a?(UndefinedValue)
100
+ object = _set(key, Config.new((name ? "#{name}." : '') + key.to_s))
101
+ end
102
+
103
+ object
104
+ end
105
+
106
+ def _set(key, *args, &block)
107
+ object = _args_to_object(*args, &block)
108
+ _nested[key.to_sym] = object
109
+ end
110
+
111
+ def _add(key, *args, &block)
112
+ raise NotAddableError, self.inspect if @_value && !@_value.is_a?(Array)
113
+ object = _args_to_object(*args, &block)
114
+ _set(key, object.is_a?(Proc) ? ProcArray.new : []) if !_get(key)
115
+ _get(key).push(object)
116
+ end
117
+
118
+ def _args_to_object(*args, &block)
119
+ if args.length == 1
120
+ args.first
121
+ elsif args.length > 1
122
+ args
123
+ elsif block_given?
124
+ block
125
+ else
126
+ raise ConfigError, 'must pass value or block'
127
+ end
128
+ end
129
+
130
+ def _nested
131
+ @_nested ||= Hash.new { |hash, key| hash[key] = UndefinedValue.new }
132
+ end
133
+ end # Config
134
+ end # Ribbon::EventBus
@@ -0,0 +1,5 @@
1
+ module Ribbon
2
+ class Config
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ribbon-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Honer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A general purpose config utility to simplify and standardize configuration
28
+ of ruby libraries.
29
+ email:
30
+ - robert@ribbonpayments.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/ribbon/config.rb
36
+ - lib/ribbon/config/version.rb
37
+ homepage: http://github.com/ribbon/config
38
+ licenses:
39
+ - BSD
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.4.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A general purpose config utility to simplify and standardize configuration
61
+ of ruby libraries.
62
+ test_files: []