buff-config 0.1.2 → 0.2.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: 0093e2b1a2758408cc99fa9589d4485226506fec
4
+ data.tar.gz: 4b1432e19086a328d91ed4b35a2750979c24a1b9
5
+ SHA512:
6
+ metadata.gz: 47714f8fb416f8d171bcc7923afce1e6fd2471047976bc861c3d49f7e5456d414dfc7456f1eeaf65a4f0ea173985bf3559282ec195975d6cf9d5638b561f74fc
7
+ data.tar.gz: df6eecb04d69de2d7e2c05eb92afbcc9193d12bf9c37121859c726a178f5b3756d8a7bddc8321a0880aa6a68d86b29277c4b1eb6e58879e38c491abc0cc009b8
@@ -1 +1 @@
1
- 1.9.3-p429
1
+ 2.0.0-p247
@@ -0,0 +1,155 @@
1
+ require 'buff/config'
2
+
3
+ module Buff
4
+ module Config
5
+ class Ruby < Config::Base
6
+ class Evaluator
7
+ class << self
8
+ # Parse the contents of the Ruby file into a Hash.
9
+ #
10
+ # @param [String] contents
11
+ #
12
+ # @return [Hash]
13
+ def parse(contents)
14
+ self.new(contents).send(:attributes)
15
+ end
16
+ end
17
+
18
+ # @param [String] contents
19
+ def initialize(contents)
20
+ @attributes = Hash.new
21
+ instance_eval(contents)
22
+ rescue Exception => ex
23
+ raise Errors::InvalidConfig, ex
24
+ end
25
+
26
+ # @see {Buff::Config::Ruby.platform_specific_path}
27
+ def platform_specific_path(path)
28
+ Buff::Config::Ruby.platform_specific_path(path)
29
+ end
30
+
31
+ def method_missing(m, *args, &block)
32
+ if args.size > 0
33
+ attributes[m.to_sym] = (args.length == 1) ? args[0] : args
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :attributes
42
+ end
43
+
44
+ class << self
45
+ # @param [String] data
46
+ #
47
+ # @return [Buff::Config::Ruby]
48
+ def from_ruby(contents)
49
+ new.from_ruby(contents)
50
+ end
51
+
52
+ # @param [String] path
53
+ #
54
+ # @raise [Buff::Errors::ConfigNotFound]
55
+ #
56
+ # @return [Buff::Config::Ruby]
57
+ def from_file(path)
58
+ path = File.expand_path(path)
59
+ contents = File.read(path)
60
+ new(path).from_ruby(contents)
61
+ rescue TypeError, Errno::ENOENT, Errno::EISDIR
62
+ raise Errors::ConfigNotFound, "No configuration found at: '#{path}'"
63
+ end
64
+
65
+ # Converts a path to a path usable for your current platform
66
+ #
67
+ # @param [String] path
68
+ #
69
+ # @return [String]
70
+ def platform_specific_path(path)
71
+ if RUBY_PLATFORM =~ /mswin|mingw|windows/
72
+ system_drive = ENV['SYSTEMDRIVE'] ? ENV['SYSTEMDRIVE'] : ""
73
+ path = win_slashify File.join(system_drive, path.split('/')[2..-1])
74
+ end
75
+
76
+ path
77
+ end
78
+
79
+ private
80
+
81
+ # Convert a unixy filepath to a windowsy filepath. Swaps forward slashes for
82
+ # double backslashes
83
+ #
84
+ # @param [String] path
85
+ # filepath to convert
86
+ #
87
+ # @return [String]
88
+ # converted filepath
89
+ def win_slashify(path)
90
+ path.gsub(File::SEPARATOR, (File::ALT_SEPARATOR || '\\'))
91
+ end
92
+ end
93
+
94
+ def initialize(path = nil, options = {})
95
+ super
96
+ from_ruby(File.read(path)) if path && File.exists?(path)
97
+ end
98
+
99
+ # @raise [Buff::Errors::InvalidConfig]
100
+ #
101
+ # @return [Buff::Config::Ruby]
102
+ def from_ruby(contents)
103
+ hash = Buff::Config::Ruby::Evaluator.parse(contents)
104
+ mass_assign(hash)
105
+ self
106
+ end
107
+
108
+ # Convert the result to Ruby.
109
+ #
110
+ # @return [String]
111
+ def to_ruby
112
+ self.to_hash.map do |k,v|
113
+ value = if const = find_constant(v)
114
+ const
115
+ else
116
+ v.inspect
117
+ end
118
+
119
+ "#{k.to_s}(#{value})"
120
+ end.join("\n")
121
+ end
122
+ alias_method :to_rb, :to_ruby
123
+
124
+ def save(destination = self.path)
125
+ if destination.nil?
126
+ raise Errors::ConfigSaveError, "Cannot save configuration without a destination. " +
127
+ "Provide one to save or set one on the object."
128
+ end
129
+
130
+ FileUtils.mkdir_p(File.dirname(destination))
131
+ File.open(destination, 'w+') do |f|
132
+ f.write(to_ruby)
133
+ end
134
+ end
135
+
136
+ # Reload the current configuration file from disk
137
+ #
138
+ # @return [Buff::Config::Ruby]
139
+ def reload
140
+ mass_assign(self.class.from_file(path).to_hash)
141
+ self
142
+ end
143
+
144
+ private
145
+
146
+ def find_constant(name)
147
+ Module.constants.find do |const|
148
+ begin
149
+ Module.const_get(const) == name
150
+ rescue NameError; end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -1,5 +1,5 @@
1
1
  module Buff
2
2
  module Config
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+ require 'buff/config/ruby'
3
+
4
+ describe Buff::Config::Ruby do
5
+ let(:ruby) do
6
+ %(
7
+ log_level :info
8
+ log_location STDOUT
9
+ cookbook_path ['cookbooks']
10
+ )
11
+ end
12
+
13
+ let(:klass) do
14
+ Class.new(Buff::Config::Ruby) do
15
+ attribute :log_level
16
+ attribute :log_location
17
+ attribute :node_name, default: 'bacon'
18
+ attribute :cookbook_path
19
+ end
20
+ end
21
+
22
+ describe 'ClassMethods' do
23
+ subject { klass }
24
+
25
+ describe '.from_ruby' do
26
+ it 'returns an instance of the inheriting class' do
27
+ expect(subject.from_ruby(ruby)).to be_a(subject)
28
+ end
29
+
30
+ it 'assigns values for each defined attribute' do
31
+ config = subject.from_ruby(ruby)
32
+
33
+ expect(config[:log_level]).to eq(:info)
34
+ expect(config[:log_location]).to eq(STDOUT)
35
+ expect(config[:node_name]).to eq('bacon')
36
+ expect(config[:cookbook_path]).to eq(['cookbooks'])
37
+ end
38
+ end
39
+
40
+ describe '::from_file' do
41
+ let(:filepath) { tmp_path.join('test_config.rb').to_s }
42
+
43
+ before { File.stub(:read).with(filepath).and_return(ruby) }
44
+
45
+ it 'returns an instance of the inheriting class' do
46
+ expect(subject.from_file(filepath)).to be_a(subject)
47
+ end
48
+
49
+ it 'sets the object\'s filepath to the path of the loaded filepath' do
50
+ expect(subject.from_file(filepath).path).to eq(filepath)
51
+ end
52
+
53
+ context 'given a filepath that does not exist' do
54
+ before { File.stub(:read).and_raise(Errno::ENOENT) }
55
+
56
+ it 'raises a Buff::Errors::ConfigNotFound error' do
57
+ expect {
58
+ subject.from_file(filepath)
59
+ }.to raise_error(Buff::Errors::ConfigNotFound)
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ subject { klass.new }
66
+
67
+ describe '#to_rb' do
68
+ it 'returns ruby with key values for each attribute' do
69
+ subject.log_level = :info
70
+ subject.log_location = STDOUT
71
+ subject.node_name = 'bacon'
72
+ subject.cookbook_path = ['cookbooks']
73
+
74
+ lines = subject.to_ruby.strip.split("\n")
75
+
76
+ expect(lines[0]).to eq('log_level(:info)')
77
+ expect(lines[1]).to eq('log_location(STDOUT)')
78
+ expect(lines[2]).to eq('node_name("bacon")')
79
+ expect(lines[3]).to eq('cookbook_path(["cookbooks"])')
80
+ end
81
+ end
82
+
83
+ describe '#from_ruby' do
84
+ it 'returns an instance of the updated class' do
85
+ expect(subject.from_ruby(ruby)).to be_a(Buff::Config::Ruby)
86
+ end
87
+
88
+ it 'assigns values for each defined attribute' do
89
+ config = subject.from_ruby(ruby)
90
+
91
+ expect(config[:log_level]).to eq(:info)
92
+ expect(config[:log_location]).to eq(STDOUT)
93
+ expect(config[:node_name]).to eq('bacon')
94
+ expect(config[:cookbook_path]).to eq(['cookbooks'])
95
+ end
96
+ end
97
+
98
+ describe '#save' do
99
+ it 'raises a ConfigSaveError if no path is set or given' do
100
+ subject.path = nil
101
+ expect {
102
+ subject.save
103
+ }.to raise_error(Buff::Errors::ConfigSaveError)
104
+ end
105
+ end
106
+
107
+ describe '#reload' do
108
+ before do
109
+ subject.path = 'foo/bar.rb'
110
+ File.stub(:read).and_return(ruby)
111
+ end
112
+
113
+ it 'returns self' do
114
+ expect(subject.reload).to eq(subject)
115
+ end
116
+
117
+ it 'updates the contents of self from disk' do
118
+ subject.log_level = :warn
119
+ subject.node_name = 'eggs'
120
+
121
+ expect(subject.log_level).to eq(:warn)
122
+ expect(subject.node_name).to eq('eggs')
123
+
124
+ subject.reload
125
+
126
+ expect(subject.log_level).to eq(:info)
127
+ expect(subject.node_name).to eq('bacon')
128
+ end
129
+
130
+ it 'raises ConfigNotFound if the path is nil' do
131
+ subject.path = nil
132
+ expect {
133
+ subject.reload
134
+ }.to raise_error(Buff::Errors::ConfigNotFound)
135
+ end
136
+ end
137
+ end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buff-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jamie Winsor
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-19 00:00:00.000000000 Z
11
+ date: 2013-06-30 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: varia_model
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: buff-extensions
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: buff-ruby_engine
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: thor
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: bundler
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ~>
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ~>
92
81
  - !ruby/object:Gem::Version
@@ -94,129 +83,113 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: rake
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - '>='
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - '>='
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: rspec
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - '>='
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - '>='
124
109
  - !ruby/object:Gem::Version
125
110
  version: '0'
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: fuubar
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
- - - ! '>='
115
+ - - '>='
132
116
  - !ruby/object:Gem::Version
133
117
  version: '0'
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
- - - ! '>='
122
+ - - '>='
140
123
  - !ruby/object:Gem::Version
141
124
  version: '0'
142
125
  - !ruby/object:Gem::Dependency
143
126
  name: guard
144
127
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
128
  requirements:
147
- - - ! '>='
129
+ - - '>='
148
130
  - !ruby/object:Gem::Version
149
131
  version: '0'
150
132
  type: :development
151
133
  prerelease: false
152
134
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
135
  requirements:
155
- - - ! '>='
136
+ - - '>='
156
137
  - !ruby/object:Gem::Version
157
138
  version: '0'
158
139
  - !ruby/object:Gem::Dependency
159
140
  name: guard-rspec
160
141
  requirement: !ruby/object:Gem::Requirement
161
- none: false
162
142
  requirements:
163
- - - ! '>='
143
+ - - '>='
164
144
  - !ruby/object:Gem::Version
165
145
  version: '0'
166
146
  type: :development
167
147
  prerelease: false
168
148
  version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
149
  requirements:
171
- - - ! '>='
150
+ - - '>='
172
151
  - !ruby/object:Gem::Version
173
152
  version: '0'
174
153
  - !ruby/object:Gem::Dependency
175
154
  name: guard-spork
176
155
  requirement: !ruby/object:Gem::Requirement
177
- none: false
178
156
  requirements:
179
- - - ! '>='
157
+ - - '>='
180
158
  - !ruby/object:Gem::Version
181
159
  version: '0'
182
160
  type: :development
183
161
  prerelease: false
184
162
  version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
163
  requirements:
187
- - - ! '>='
164
+ - - '>='
188
165
  - !ruby/object:Gem::Version
189
166
  version: '0'
190
167
  - !ruby/object:Gem::Dependency
191
168
  name: spork
192
169
  requirement: !ruby/object:Gem::Requirement
193
- none: false
194
170
  requirements:
195
- - - ! '>='
171
+ - - '>='
196
172
  - !ruby/object:Gem::Version
197
173
  version: '0'
198
174
  type: :development
199
175
  prerelease: false
200
176
  version_requirements: !ruby/object:Gem::Requirement
201
- none: false
202
177
  requirements:
203
- - - ! '>='
178
+ - - '>='
204
179
  - !ruby/object:Gem::Version
205
180
  version: '0'
206
181
  - !ruby/object:Gem::Dependency
207
182
  name: json_spec
208
183
  requirement: !ruby/object:Gem::Requirement
209
- none: false
210
184
  requirements:
211
- - - ! '>='
185
+ - - '>='
212
186
  - !ruby/object:Gem::Version
213
187
  version: '0'
214
188
  type: :development
215
189
  prerelease: false
216
190
  version_requirements: !ruby/object:Gem::Requirement
217
- none: false
218
191
  requirements:
219
- - - ! '>='
192
+ - - '>='
220
193
  - !ruby/object:Gem::Version
221
194
  version: '0'
222
195
  description: A simple configuration class
@@ -240,39 +213,38 @@ files:
240
213
  - lib/buff/config.rb
241
214
  - lib/buff/config/errors.rb
242
215
  - lib/buff/config/json.rb
216
+ - lib/buff/config/ruby.rb
243
217
  - lib/buff/config/version.rb
244
218
  - spec/buff/config/json_spec.rb
219
+ - spec/buff/config/ruby_spec.rb
245
220
  - spec/buff/config_spec.rb
246
221
  - spec/spec_helper.rb
247
222
  homepage: https://github.com/RiotGames/buff-config
248
223
  licenses:
249
224
  - Apache 2.0
225
+ metadata: {}
250
226
  post_install_message:
251
227
  rdoc_options: []
252
228
  require_paths:
253
229
  - lib
254
230
  required_ruby_version: !ruby/object:Gem::Requirement
255
- none: false
256
231
  requirements:
257
- - - ! '>='
232
+ - - '>='
258
233
  - !ruby/object:Gem::Version
259
234
  version: 1.9.2
260
235
  required_rubygems_version: !ruby/object:Gem::Requirement
261
- none: false
262
236
  requirements:
263
- - - ! '>='
237
+ - - '>='
264
238
  - !ruby/object:Gem::Version
265
239
  version: '0'
266
- segments:
267
- - 0
268
- hash: 269088379032573538
269
240
  requirements: []
270
241
  rubyforge_project:
271
- rubygems_version: 1.8.23
242
+ rubygems_version: 2.0.3
272
243
  signing_key:
273
- specification_version: 3
244
+ specification_version: 4
274
245
  summary: A simple configuration class
275
246
  test_files:
276
247
  - spec/buff/config/json_spec.rb
248
+ - spec/buff/config/ruby_spec.rb
277
249
  - spec/buff/config_spec.rb
278
250
  - spec/spec_helper.rb