mutaconf 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,12 +3,15 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
+ gem 'multi_json', '~> 1.7'
7
+
6
8
  # Add dependencies to develop your gem here.
7
9
  # Include everything needed to run rake, tests, features, etc.
8
10
  group :development do
9
11
  gem 'bundler'
10
12
  gem 'rake'
11
13
  gem 'rspec'
14
+ gem 'fakefs', require: 'fakefs/safe'
12
15
  gem 'jeweler'
13
16
  gem 'gemcutter'
14
17
  gem 'gem-release'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,11 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Mutaconf
4
- VERSION = '0.1.1'
5
-
6
- def self.dsl *args
7
- DSL.new *args
8
- end
4
+ VERSION = '0.2.0'
9
5
 
10
6
  def self.env *args
11
7
  options = args.last.kind_of?(Hash) ? args.pop : {}
@@ -18,16 +14,12 @@ module Mutaconf
18
14
  end
19
15
  end
20
16
 
21
- def self.extract source, key
22
- if source.kind_of? Hash
23
- source[key.to_sym]
24
- elsif source.kind_of? OpenStruct
25
- source.send key.to_sym
26
- elsif source.kind_of?(String) or source.kind_of?(Symbol)
27
- source
28
- elsif source
29
- source.send key.to_sym
30
- end
17
+ def self.config *args, &block
18
+ Config.find *args, &block
19
+ end
20
+
21
+ def self.config_file *args, &block
22
+ Config.find_file *args, &block
31
23
  end
32
24
  end
33
25
 
@@ -0,0 +1,180 @@
1
+ require 'yaml'
2
+ require 'multi_json'
3
+
4
+ module Mutaconf
5
+
6
+ class Config
7
+ attr_reader :file, :parser
8
+ DEFAULT_FORMAT = :ruby
9
+
10
+ class Error < StandardError; end
11
+
12
+ def initialize file, options = {}
13
+ @file = file
14
+ @parser = options[:parser]
15
+ end
16
+
17
+ def raw
18
+ @raw ||= File.read @file
19
+ end
20
+
21
+ def contents
22
+ @contents ||= @parser ? @parser.parse(raw) : raw
23
+ end
24
+
25
+ def self.find *args
26
+
27
+ file_options = args.last.kind_of?(Hash) ? args.pop : {}
28
+ options = [ :read, :load, :parser, :parser_options ].inject({}){ |memo,o| memo[o] = file_options.delete o; memo }
29
+ file_options.delete :all
30
+
31
+ parser = if options[:parser]
32
+ options[:parser]
33
+ elsif file_options[:format] == :yaml
34
+ YamlParser.new options[:parser_options]
35
+ elsif file_options[:format] == :json
36
+ JsonParser.new options[:parser_options]
37
+ end
38
+
39
+ file = find_file *(args.push file_options)
40
+ return nil unless file
41
+
42
+ raise Error, "Parser must respond to :parse" if parser and !parser.respond_to?(:parse)
43
+ config = Config.new file, parser: parser
44
+ config.raw if options[:read]
45
+ config.load if options[:load]
46
+
47
+ yield config if block_given?
48
+ config
49
+ end
50
+
51
+ def self.find_file *args
52
+
53
+ options = args.last.kind_of?(Hash) ? args.pop : {}
54
+
55
+ format = selected_format options
56
+ all = options[:all]
57
+
58
+ found = []
59
+
60
+ args.each do |name|
61
+
62
+ # TODO: check user-supplied locations for duplicates
63
+ selected_locations(options).each do |location|
64
+
65
+ prefix = dot?(location, options) ? '.' : nil
66
+ path = location[:path] || File.expand_path(Dir.pwd)
67
+
68
+ selected_types(options).each do |type|
69
+
70
+ build_suffixes(format, type, options).each do |suffix|
71
+
72
+ file = File.expand_path(File.join(path, "#{prefix}#{name}#{suffix}"))
73
+ if File.file? file
74
+ return file unless all
75
+ found << file
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ all ? found : nil
83
+ end
84
+
85
+ class YamlParser
86
+
87
+ def initialize options = {}
88
+ end
89
+
90
+ def parse raw
91
+ YAML.load raw
92
+ end
93
+ end
94
+
95
+ class JsonParser
96
+
97
+ def initialize options = {}
98
+ @options = options
99
+ end
100
+
101
+ def parse raw
102
+ MultiJson.load raw, @options
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def self.dot? location, options = {}
109
+ (options.key?(:dot) ? options[:dot] : true) and (location.key?(:dot) ? location[:dot] : true)
110
+ end
111
+
112
+ def self.selected_format options = {}
113
+ options.key?(:format) ? options[:format] : DEFAULT_FORMAT
114
+ end
115
+
116
+ def self.build_suffixes format, type, options = {}
117
+
118
+ suffixes = type[:suffix]
119
+ if suffixes == :format and FORMATS[format]
120
+ exts = FORMATS[format][:extension]
121
+ exts = [ exts ] unless exts.kind_of? Array
122
+ suffixes = exts.collect{ |ext| ".#{ext}" }
123
+ end
124
+
125
+ suffixes = [ suffixes ] unless suffixes.kind_of? Array
126
+ suffixes
127
+ end
128
+
129
+ def self.selected_types options = {}
130
+ filter TYPES, options[:types] || options[:type], TYPES.collect{ |t| t[:name] }
131
+ end
132
+
133
+ def self.selected_locations options = {}
134
+ locations = filter LOCATIONS, options[:locations] || options[:location], LOCATIONS.collect{ |l| l[:name] }
135
+ locations.delete_if{ |l| l[:name] == :cwd } if locations.any?{ |l| l[:name] != :cwd && File.expand_path(Dir.pwd) == File.expand_path(l[:path]) }
136
+ locations
137
+ end
138
+
139
+ def self.filter a, criteria, defaults
140
+ filters = build_filters criteria, defaults
141
+ a.select{ |o| filters[:only].include? o[:name] }.reject{ |o| filters[:except].include? o[:name] }
142
+ end
143
+
144
+ def self.build_filters criteria, defaults
145
+ h = criteria.kind_of?(Hash) ? criteria : { only: criteria.kind_of?(Array) ? criteria : [ criteria ].compact }
146
+ h[:only] = defaults if h[:only].nil? or h[:only].empty?
147
+ h[:only] = [ h[:only] ].compact unless h[:only].kind_of?(Array)
148
+ h[:except] = [ h[:except] ].compact unless h[:except].kind_of?(Array)
149
+ h
150
+ end
151
+
152
+ FORMATS = {
153
+ ruby: {
154
+ extension: 'rb'
155
+ },
156
+ yaml: {
157
+ extension: [ 'yml', 'yaml' ],
158
+ parser: YamlParser
159
+ },
160
+ json: {
161
+ extension: 'json',
162
+ parser: JsonParser
163
+ }
164
+ }
165
+
166
+ LOCATIONS = [
167
+ { name: :cwd },
168
+ { name: :home, path: '~' },
169
+ { name: :etc, path: '/etc', dot: false }
170
+ ]
171
+
172
+ TYPES = [
173
+ { name: :plain },
174
+ { name: :rc, suffix: 'rc' },
175
+ { name: :dotrc, suffix: '.rc'},
176
+ { name: :conf, suffix: '.conf'},
177
+ { name: :format, suffix: :format }
178
+ ]
179
+ end
180
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutaconf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-29 00:00:00.000000000 Z
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: multi_json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.7'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.7'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: bundler
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +75,22 @@ dependencies:
59
75
  - - ! '>='
60
76
  - !ruby/object:Gem::Version
61
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: fakefs
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
62
94
  - !ruby/object:Gem::Dependency
63
95
  name: jeweler
64
96
  requirement: !ruby/object:Gem::Requirement
@@ -147,30 +179,12 @@ extra_rdoc_files:
147
179
  - LICENSE.txt
148
180
  - README.md
149
181
  files:
150
- - .rspec
151
- - .ruby-version
152
- - .screenrc
153
182
  - Gemfile
154
- - Gemfile.lock
155
183
  - LICENSE.txt
156
184
  - README.md
157
- - Rakefile
158
185
  - VERSION
159
186
  - lib/mutaconf.rb
160
- - lib/mutaconf/dsl.rb
161
- - lib/mutaconf/errors.rb
162
- - lib/mutaconf/target.rb
163
- - mutaconf.gemspec
164
- - spec/block_spec.rb
165
- - spec/env_spec.rb
166
- - spec/extract_spec.rb
167
- - spec/fixtures/eval.rb
168
- - spec/helper.rb
169
- - spec/proxy_spec.rb
170
- - spec/source_spec.rb
171
- - spec/subclass_spec.rb
172
- - spec/target_spec.rb
173
- - spec/version_spec.rb
187
+ - lib/mutaconf/config.rb
174
188
  homepage: http://github.com/AlphaHydrae/mutaconf
175
189
  licenses:
176
190
  - MIT
@@ -197,3 +211,4 @@ signing_key:
197
211
  specification_version: 3
198
212
  summary: Configuration utilities.
199
213
  test_files: []
214
+ has_rdoc:
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --format doc
@@ -1 +0,0 @@
1
- 1.9.3
data/.screenrc DELETED
@@ -1,8 +0,0 @@
1
- source $HOME/.screenrc
2
-
3
- screen -t vim 0
4
- stuff "\${PROJECT_EDITOR-\$EDITOR}\012"
5
- screen -t zsh 1
6
- screen -t spec 2
7
- stuff "rake"
8
- select vim
@@ -1,44 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- diff-lcs (1.2.3)
5
- gem-release (0.5.3)
6
- gemcutter (0.7.1)
7
- git (1.2.5)
8
- jeweler (1.8.4)
9
- bundler (~> 1.0)
10
- git (>= 1.2.5)
11
- rake
12
- rdoc
13
- json (1.7.7)
14
- multi_json (1.7.2)
15
- rake (10.0.4)
16
- rake-version (0.3.1)
17
- rake (~> 10)
18
- rdoc (4.0.1)
19
- json (~> 1.4)
20
- rspec (2.13.0)
21
- rspec-core (~> 2.13.0)
22
- rspec-expectations (~> 2.13.0)
23
- rspec-mocks (~> 2.13.0)
24
- rspec-core (2.13.1)
25
- rspec-expectations (2.13.0)
26
- diff-lcs (>= 1.1.3, < 2.0)
27
- rspec-mocks (2.13.1)
28
- simplecov (0.7.1)
29
- multi_json (~> 1.0)
30
- simplecov-html (~> 0.7.1)
31
- simplecov-html (0.7.1)
32
-
33
- PLATFORMS
34
- ruby
35
-
36
- DEPENDENCIES
37
- bundler
38
- gem-release
39
- gemcutter
40
- jeweler
41
- rake
42
- rake-version
43
- rspec
44
- simplecov
data/Rakefile DELETED
@@ -1,51 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "mutaconf"
18
- gem.homepage = "http://github.com/AlphaHydrae/mutaconf"
19
- gem.license = "MIT"
20
- gem.summary = %Q{Configuration utilities.}
21
- gem.description = %Q{Create simple DSLs and read configuration from hashes or objects.}
22
- gem.email = "hydrae.alpha@gmail.com"
23
- gem.authors = ["AlphaHydrae"]
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
27
-
28
- Rake::TaskManager.class_eval do
29
- def remove_task(task_name)
30
- @tasks.delete(task_name.to_s)
31
- end
32
- end
33
-
34
- [ 'version', 'version:bump:major', 'version:bump:minor', 'version:bump:patch', 'version:write' ].each do |task|
35
- Rake.application.remove_task task
36
- end
37
-
38
- # version tasks
39
- require 'rake-version'
40
- RakeVersion::Tasks.new do |v|
41
- v.copy 'lib/mutaconf.rb'
42
- end
43
-
44
- require 'rspec/core/rake_task'
45
- desc "Run specs"
46
- RSpec::Core::RakeTask.new do |t|
47
- #t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
48
- # Put spec opts in a file named .rspec in root
49
- end
50
-
51
- task :default => :spec
@@ -1,104 +0,0 @@
1
-
2
- module Mutaconf
3
-
4
- class DSL
5
- attr_accessor :lenient
6
-
7
- def initialize options = {}
8
-
9
- @attr_targets = {}
10
- @proxy_targets = {}
11
- @lenient = options[:lenient] if options.key?(:lenient)
12
-
13
- if options[:attrs]
14
- options[:attrs].each_pair do |target,attrs|
15
- if !!attrs == attrs
16
- @attr_targets.default = Target.new target
17
- else
18
- [ attrs ].flatten.each do |attr|
19
- if !!attr == attr
20
- @attr_targets.default = Target.new target
21
- else
22
- @attr_targets[attr] = Target.new target
23
- end
24
- end
25
- end
26
- end
27
- end
28
-
29
- if options[:proxy]
30
- options[:proxy].each_pair do |target,attrs|
31
- if !!attrs == attrs
32
- @proxy_targets.default = target
33
- else
34
- [ attrs ].flatten.each do |attr|
35
- if !!attr == attr
36
- @proxy_targets.default = target
37
- else
38
- @proxy_targets[attr] = target
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
45
-
46
- def configure source = nil, options = {}, &block
47
-
48
- if source.kind_of? Hash
49
- configure_from_hash source, options
50
- elsif source.kind_of? String
51
- configure_from_file source, options
52
- elsif source
53
- configure_from_object source, options
54
- end
55
-
56
- instance_exec self, &block if block
57
-
58
- self
59
- end
60
-
61
- private
62
-
63
- def configure_from_hash h, options = {}
64
- h.each_pair{ |attr,value| send attr, value }
65
- end
66
-
67
- def configure_from_file f, options = {}
68
- args = [ File.open(f, 'r').read, f ]
69
- args << options[:lineno] if options[:lineno]
70
- instance_eval *args
71
- end
72
-
73
- def configure_from_object o, options = {}
74
- @attr_targets.each_pair do |attr,target|
75
- send attr, o.send(attr.to_sym) if o.respond_to? attr.to_sym
76
- end
77
- end
78
-
79
- def has? attr, method
80
- @attr_targets.default or @attr_targets.key?(attr) or @proxy_targets.default or @proxy_targets.key?(method)
81
- end
82
-
83
- def set attr, value
84
-
85
- end
86
-
87
- def method_missing name, *args, &block
88
-
89
- m = name.to_s.match(/\A(\w+)\=?\Z/)
90
-
91
- attr, method = m[1].to_sym, m[0].to_sym
92
- if attr
93
- raise KeyError, attr unless has?(attr, method) or @lenient
94
- if @attr_targets.default or @attr_targets.key?(attr)
95
- return @attr_targets[attr].get attr if args.empty?
96
- @attr_targets[attr].set attr, args.first
97
- end
98
- @proxy_targets[attr].send *(args.unshift method), &block if @proxy_targets.default or @proxy_targets.key?(attr)
99
- else
100
- super
101
- end
102
- end
103
- end
104
- end
@@ -1,14 +0,0 @@
1
-
2
- module Mutaconf
3
-
4
- class Error < StandardError; end
5
-
6
- class KeyError < Error
7
- attr_reader :key
8
-
9
- def initialize key
10
- super "No such property '#{key}'"
11
- @key = key
12
- end
13
- end
14
- end
@@ -1,32 +0,0 @@
1
- require 'ostruct'
2
-
3
- module Mutaconf
4
-
5
- class Target
6
- attr_reader :object
7
-
8
- def initialize object
9
- @object = object
10
- end
11
-
12
- def set key, value
13
- if @object.kind_of? Hash
14
- @object[key.to_sym] = value
15
- else
16
- @object.send "#{key}=", value
17
- end
18
- end
19
-
20
- def get key
21
- if @object.kind_of? Hash
22
- @object[key.to_sym]
23
- else
24
- @object.send key.to_sym
25
- end
26
- end
27
-
28
- def has? key
29
- @object.kind_of?(Hash) or @object.kind_of?(OpenStruct) or @object.respond_to?("#{key}=")
30
- end
31
- end
32
- end
@@ -1,84 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = "mutaconf"
8
- s.version = "0.1.1"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["AlphaHydrae"]
12
- s.date = "2013-04-29"
13
- s.description = "Create simple DSLs and read configuration from hashes or objects."
14
- s.email = "hydrae.alpha@gmail.com"
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.md"
18
- ]
19
- s.files = [
20
- ".rspec",
21
- ".ruby-version",
22
- ".screenrc",
23
- "Gemfile",
24
- "Gemfile.lock",
25
- "LICENSE.txt",
26
- "README.md",
27
- "Rakefile",
28
- "VERSION",
29
- "lib/mutaconf.rb",
30
- "lib/mutaconf/dsl.rb",
31
- "lib/mutaconf/errors.rb",
32
- "lib/mutaconf/target.rb",
33
- "mutaconf.gemspec",
34
- "spec/block_spec.rb",
35
- "spec/env_spec.rb",
36
- "spec/extract_spec.rb",
37
- "spec/fixtures/eval.rb",
38
- "spec/helper.rb",
39
- "spec/proxy_spec.rb",
40
- "spec/source_spec.rb",
41
- "spec/subclass_spec.rb",
42
- "spec/target_spec.rb",
43
- "spec/version_spec.rb"
44
- ]
45
- s.homepage = "http://github.com/AlphaHydrae/mutaconf"
46
- s.licenses = ["MIT"]
47
- s.require_paths = ["lib"]
48
- s.rubygems_version = "1.8.25"
49
- s.summary = "Configuration utilities."
50
-
51
- if s.respond_to? :specification_version then
52
- s.specification_version = 3
53
-
54
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
- s.add_development_dependency(%q<bundler>, [">= 0"])
56
- s.add_development_dependency(%q<rake>, [">= 0"])
57
- s.add_development_dependency(%q<rspec>, [">= 0"])
58
- s.add_development_dependency(%q<jeweler>, [">= 0"])
59
- s.add_development_dependency(%q<gemcutter>, [">= 0"])
60
- s.add_development_dependency(%q<gem-release>, [">= 0"])
61
- s.add_development_dependency(%q<rake-version>, [">= 0"])
62
- s.add_development_dependency(%q<simplecov>, [">= 0"])
63
- else
64
- s.add_dependency(%q<bundler>, [">= 0"])
65
- s.add_dependency(%q<rake>, [">= 0"])
66
- s.add_dependency(%q<rspec>, [">= 0"])
67
- s.add_dependency(%q<jeweler>, [">= 0"])
68
- s.add_dependency(%q<gemcutter>, [">= 0"])
69
- s.add_dependency(%q<gem-release>, [">= 0"])
70
- s.add_dependency(%q<rake-version>, [">= 0"])
71
- s.add_dependency(%q<simplecov>, [">= 0"])
72
- end
73
- else
74
- s.add_dependency(%q<bundler>, [">= 0"])
75
- s.add_dependency(%q<rake>, [">= 0"])
76
- s.add_dependency(%q<rspec>, [">= 0"])
77
- s.add_dependency(%q<jeweler>, [">= 0"])
78
- s.add_dependency(%q<gemcutter>, [">= 0"])
79
- s.add_dependency(%q<gem-release>, [">= 0"])
80
- s.add_dependency(%q<rake-version>, [">= 0"])
81
- s.add_dependency(%q<simplecov>, [">= 0"])
82
- end
83
- end
84
-
@@ -1,71 +0,0 @@
1
- require 'helper'
2
-
3
- describe Mutaconf::DSL do
4
-
5
- let(:target){ {} }
6
- let(:dsl){ Mutaconf::DSL.new options }
7
- let(:options){ { attrs: { target => true } } }
8
-
9
- it "should configure properties with instance evaluation" do
10
- result = dsl.configure do
11
- a 'b'
12
- c 'd'
13
- end
14
- expected = { a: 'b', c: 'd' }
15
- result.should be(dsl)
16
- target.should == expected
17
- end
18
-
19
- it "should configure properties with a configuration object" do
20
- result = dsl.configure do |config|
21
- config.e = 'f'
22
- config.g = 'h'
23
- config.i = 'j'
24
- end
25
- expected = { e: 'f', g: 'h', i: 'j' }
26
- result.should be(dsl)
27
- target.should == expected
28
- end
29
-
30
- context "with restricted keys" do
31
-
32
- let(:options){ { attrs: { target => [ :a ] } } }
33
-
34
- it "should raise a key error for a unknown key with instance evaluation" do
35
- lambda{ dsl.configure{ b 'c' } }.should raise_error(Mutaconf::KeyError, /'b'/)
36
- end
37
-
38
- it "should raise a key error for an unknown key with a configuration object" do
39
- lambda{ dsl.configure{ |config| config.b = 'c' } }.should raise_error(Mutaconf::KeyError, /'b'/)
40
- end
41
- end
42
-
43
- context "with restricted keys in lenient mode" do
44
-
45
- let(:options){ { attrs: { target => [ :a, :e ] }, lenient: true } }
46
-
47
- it "should configure restricted properties with instance evaluation" do
48
- result = dsl.configure do
49
- a 'b'
50
- c 'd'
51
- e 'f'
52
- g 'h'
53
- end
54
- expected = { a: 'b', e: 'f' }
55
- result.should be(dsl)
56
- target.should == expected
57
- end
58
-
59
- it "should configure restricted properties with a configuration object" do
60
- result = dsl.configure do |config|
61
- config.a = 'b'
62
- config.c = 'd'
63
- config.e = 'f'
64
- config.g = 'h'
65
- end
66
- expected = { a: 'b', e: 'f' }
67
- result.should be(dsl)
68
- target.should == expected
69
- end
70
- end
71
- end
@@ -1,23 +0,0 @@
1
- require 'helper'
2
-
3
- describe "Mutaconf.env" do
4
-
5
- it "should extract properties from environment variables" do
6
- ENV['FOO'] = 'bar'
7
- ENV['BAR'] = 'foo'
8
- ENV.delete 'NIL'
9
- Mutaconf.env(:foo, :bar, :nil).should == { foo: 'bar', bar: 'foo', nil: nil }
10
- end
11
-
12
- it "should not upcase keys if specified" do
13
- ENV['foo'] = 'bar'
14
- ENV['bar'] = 'foo'
15
- Mutaconf.env(:foo, :bar, upcase: false).should == { foo: 'bar', bar: 'foo' }
16
- end
17
-
18
- it "should use the given prefix" do
19
- ENV['MUTACONF_FOO'] = 'bar'
20
- ENV['MUTACONF_BAR'] = 'foo'
21
- Mutaconf.env(:foo, :bar, prefix: :mutaconf_).should == { foo: 'bar', bar: 'foo' }
22
- end
23
- end
@@ -1,38 +0,0 @@
1
- require 'helper'
2
-
3
- describe "Mutaconf.extract" do
4
-
5
- class Source
6
- attr_accessor :a, :c
7
-
8
- def initialize
9
- @a, @c = 'b', 'd'
10
- end
11
- end
12
-
13
- it "should extract properties from a hash" do
14
- s = { a: 'b', c: 'd' }
15
- Mutaconf.extract(s, :a).should == 'b'
16
- Mutaconf.extract(s, :c).should == 'd'
17
- Mutaconf.extract(s, :e).should be_nil
18
- end
19
-
20
- it "should extract properties from an open struct" do
21
- s = OpenStruct.new a: 'b', c: 'd'
22
- Mutaconf.extract(s, :a).should == 'b'
23
- Mutaconf.extract(s, :c).should == 'd'
24
- Mutaconf.extract(s, :e).should be_nil
25
- end
26
-
27
- it "should extract properties from an object" do
28
- s = Source.new
29
- Mutaconf.extract(s, :a).should == 'b'
30
- Mutaconf.extract(s, :c).should == 'd'
31
- lambda{ Mutaconf.extract s, :e }.should raise_error(NoMethodError)
32
- end
33
-
34
- it "should return a string or symbol" do
35
- Mutaconf.extract('string', :a).should == 'string'
36
- Mutaconf.extract(:symbol, :c).should == :symbol
37
- end
38
- end
@@ -1,4 +0,0 @@
1
- a 'b'
2
- c 'd'
3
- e 'f'
4
- g 'h'
@@ -1,16 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler'
3
-
4
- begin
5
- Bundler.setup(:default, :development)
6
- rescue Bundler::BundlerError => e
7
- $stderr.puts e.message
8
- $stderr.puts "Run `bundle install` to install missing gems"
9
- exit e.status_code
10
- end
11
-
12
- require 'simplecov'
13
- SimpleCov.start
14
-
15
- require 'rspec'
16
- require 'mutaconf'
@@ -1,73 +0,0 @@
1
- require 'helper'
2
-
3
- describe 'DSL proxies' do
4
-
5
- let(:target){ OpenStruct.new }
6
- let(:dsl){ Mutaconf::DSL.new options }
7
- let(:options){ { proxy: { target => true } } }
8
- let(:block){ lambda{} }
9
-
10
- it "should proxy method calls with instance evaluation" do
11
- target.should_receive(:a).with('b')
12
- target.should_receive(:c=).with('d')
13
- target.should_receive(:e).with('f', &block)
14
- result = dsl.configure do
15
- a 'b'
16
- self.c = 'd'
17
- e 'f', &block
18
- end
19
- result.should be(dsl)
20
- end
21
-
22
- it "should proxy method calls with a configuration object" do
23
- target.should_receive(:a=).with('b')
24
- target.should_receive(:c=).with('d')
25
- result = dsl.configure do |config|
26
- config.a = 'b'
27
- config.c = 'd'
28
- end
29
- result.should be(dsl)
30
- end
31
-
32
- context "with restricted keys" do
33
-
34
- let(:options){ { proxy: { target => [ :a ] } } }
35
-
36
- it "should raise a key error for a unknown key with instance evaluation" do
37
- lambda{ dsl.configure{ b 'c' } }.should raise_error(Mutaconf::KeyError, /'b'/)
38
- end
39
-
40
- it "should raise a key error for an unknown key with a configuration object" do
41
- lambda{ dsl.configure{ |config| config.b = 'c' } }.should raise_error(Mutaconf::KeyError, /'b'/)
42
- end
43
- end
44
-
45
- context "with restricted keys in lenient mode" do
46
-
47
- let(:options){ { proxy: { target => [ :a, :e ] }, lenient: true } }
48
-
49
- it "should configure restricted properties with instance evaluation" do
50
- target.should_receive(:a).with('b', &block)
51
- target.should_receive(:e).with('f')
52
- result = dsl.configure do
53
- a 'b', &block
54
- c 'd'
55
- e 'f'
56
- g 'h'
57
- end
58
- result.should be(dsl)
59
- end
60
-
61
- it "should configure restricted properties with a configuration object" do
62
- target.should_receive(:a=).with('b')
63
- target.should_receive(:e=).with('f')
64
- result = dsl.configure do |config|
65
- config.a = 'b'
66
- config.c = 'd'
67
- config.e = 'f'
68
- config.g = 'h'
69
- end
70
- result.should be(dsl)
71
- end
72
- end
73
- end
@@ -1,66 +0,0 @@
1
- require 'helper'
2
- require 'ostruct'
3
-
4
- describe Mutaconf::DSL do
5
-
6
- let(:target){ {} }
7
- let(:dsl){ Mutaconf::DSL.new options }
8
- let(:options){ { attrs: { target => true } } }
9
-
10
- it "should configure properties from a hash source" do
11
- result = dsl.configure a: 'b', c: 'd'
12
- expected = { a: 'b', c: 'd' }
13
- result.should be(dsl)
14
- target.should == expected
15
- end
16
-
17
- it "should configure properties from a file with instance evaluation" do
18
- result = dsl.configure fixture(:eval)
19
- expected = { a: 'b', c: 'd', e: 'f', g: 'h' }
20
- result.should be(dsl)
21
- target.should == expected
22
- end
23
-
24
- context "with restricted keys" do
25
-
26
- let(:options){ { attrs: { target => [ :a ] } } }
27
-
28
- it "should raise a key error for an unknown key from a hash source" do
29
- lambda{ dsl.configure b: 'c' }.should raise_error(Mutaconf::KeyError, /'b'/)
30
- end
31
-
32
- it "should raise a key error for an unknown key from a file with instance evaluation" do
33
- lambda{ dsl.configure fixture(:eval) }.should raise_error(Mutaconf::KeyError, /'c'/)
34
- end
35
- end
36
-
37
- context "with restricted keys in lenient mode" do
38
-
39
- let(:options){ { attrs: { target => [ :a, :e ] }, lenient: true } }
40
-
41
- it "should configure restricted properties from a hash source" do
42
- result = dsl.configure a: 'b', c: 'd', e: 'f', g: 'h'
43
- expected = { a: 'b', e: 'f' }
44
- result.should be(dsl)
45
- target.should == expected
46
- end
47
-
48
- it "should configure restricted properties from an object" do
49
- result = dsl.configure OpenStruct.new(a: 'b', c: 'd', e: 'f', g: 'h')
50
- expected = { a: 'b', e: 'f' }
51
- result.should be(dsl)
52
- target.should == expected
53
- end
54
-
55
- it "should configure restricted properties from a file with instance evaluation" do
56
- result = dsl.configure fixture(:eval)
57
- expected = { a: 'b', e: 'f' }
58
- result.should be(dsl)
59
- target.should == expected
60
- end
61
- end
62
-
63
- def fixture name
64
- File.join File.dirname(__FILE__), 'fixtures', "#{name}.rb"
65
- end
66
- end
@@ -1,32 +0,0 @@
1
- require 'helper'
2
-
3
- describe Mutaconf::DSL do
4
-
5
- class CustomDSL < Mutaconf::DSL
6
- attr_reader :value
7
-
8
- def initialize options = {}
9
- super options
10
- @value = 0
11
- end
12
-
13
- def increase by
14
- @value += by
15
- end
16
- end
17
-
18
- let(:target){ {} }
19
- let(:dsl){ CustomDSL.new attrs: { target => true } }
20
-
21
- it "should work when subclassed" do
22
- result = dsl.configure do
23
- a 'b'
24
- c 'd'
25
- increase 5
26
- end
27
- expected = { a: 'b', c: 'd' }
28
- result.should be(dsl)
29
- target.should == expected
30
- dsl.value.should == 5
31
- end
32
- end
@@ -1,74 +0,0 @@
1
- require 'helper'
2
-
3
- describe Mutaconf::Target do
4
-
5
- subject{ Mutaconf::Target.new target }
6
-
7
- context "with a hash" do
8
-
9
- let(:target){ { a: 'z' } }
10
-
11
- it "should set values" do
12
- subject.set :a, 'b'
13
- subject.set :c, 'd'
14
- target.should == { a: 'b', c: 'd' }
15
- end
16
-
17
- it "should get values" do
18
- subject.get(:a).should == 'z'
19
- end
20
-
21
- it "should have all values" do
22
- ('a'..'z').each{ |key| subject.has?(key).should be_true }
23
- end
24
- end
25
-
26
- context "with an open struct" do
27
-
28
- let(:target){ OpenStruct.new a: 'z' }
29
-
30
- it "should set values" do
31
- subject.set :a, 'b'
32
- subject.set :c, 'd'
33
- target.a.should == 'b'
34
- target.c.should == 'd'
35
- end
36
-
37
- it "should get values" do
38
- subject.get(:a).should == 'z'
39
- end
40
-
41
- it "should have all values" do
42
- ('a'..'z').each{ |key| subject.has?(key).should be_true }
43
- end
44
- end
45
-
46
- context "with an object" do
47
-
48
- let(:target) do
49
- Class.new do
50
- attr_accessor :a, :c
51
-
52
- def initialize
53
- @a = 'z'
54
- end
55
- end.new
56
- end
57
-
58
- it "should set values" do
59
- subject.set :a, 'b'
60
- subject.set :c, 'd'
61
- end
62
-
63
- it "should get values" do
64
- subject.get(:a).should == 'z'
65
- end
66
-
67
- it "should have values corresponding to its setters" do
68
- subject.has?(:a).should be_true
69
- subject.has?(:b).should be_false
70
- subject.has?(:c).should be_true
71
- ('d'..'z').each{ |key| subject.has?(key).should be_false }
72
- end
73
- end
74
- end
@@ -1,9 +0,0 @@
1
- require 'helper'
2
-
3
- describe "Version" do
4
-
5
- it "should be correct" do
6
- version_file = File.join File.dirname(__FILE__), '..', 'VERSION'
7
- Mutaconf::VERSION.should == File.open(version_file, 'r').read
8
- end
9
- end