multi_toml 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ ## Copyright and license
2
+
3
+ Copyright 2013 James A. Rosen
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # multi_toml
2
+
3
+ multi_toml is an interface gem for https://github.com/mojombo/toml.
4
+
5
+ There are many great TOML libraries out there. If you're writing an application,
6
+ you should be able to use any one you want. If you're writing a library that
7
+ other libraries or applications will use, you should use multi_toml so that you
8
+ don't impose a particular implementation on the consuming application.
9
+
10
+ ## Implementations
11
+
12
+ * https://rubygems.org/gems/toml
13
+ * https://rubygems.org/gems/toml-ruby
14
+ * https://rubygems.org/gems/toml_parser-ruby
15
+ * https://rubygems.org/gems/toml2
16
+
17
+ ## Seriously?
18
+
19
+ Not entirely seriously, no.
20
+
21
+ ## But Why?
22
+
23
+ Because application developers shouldn't have to install 3 TOML libraries.
data/lib/multi_toml.rb ADDED
@@ -0,0 +1,52 @@
1
+ module MultiToml
2
+
3
+ PARSER_MAP = {
4
+ :toml => lambda { |toml| TOML::Parser.new(toml).parsed },
5
+ :toml_ruby => lambda { |toml| Toml.load(toml) },
6
+ :toml2 => lambda { |toml| TOML.load(toml) }
7
+ }
8
+
9
+ class NoParserError < RuntimeError
10
+ def initialize(adapter)
11
+ super(adapter.nil? ? 'Could not detect a TOML parser' : "No TOML parser for #{adapter}")
12
+ end
13
+ end
14
+
15
+ def load(input)
16
+ parser.call(input)
17
+ end
18
+
19
+ def adapter
20
+ return @adapter if defined?(@adapter)
21
+ @adapter = detect_adapter
22
+ end
23
+
24
+ def adapter=(adapter)
25
+ @adapter = adapter
26
+ end
27
+
28
+ def detect_adapter
29
+ begin
30
+ require 'toml'
31
+ TOML.respond_to?(:load) ? :toml2 : :toml
32
+ rescue LoadError
33
+ begin
34
+ require 'toml-ruby'
35
+ :toml_ruby
36
+ rescue LoadError
37
+ nil
38
+ end
39
+ end
40
+ end
41
+
42
+ def parser
43
+ PARSER_MAP[adapter] or raise NoParserError.new(adapter)
44
+ end
45
+
46
+ extend self
47
+
48
+ class << self
49
+ private :detect_adapter, :parser
50
+ end
51
+
52
+ end
File without changes
@@ -0,0 +1,57 @@
1
+ require 'helper'
2
+
3
+ describe 'MultiToml' do
4
+
5
+ let(:tomler) do
6
+ Object.new.tap do |o|
7
+ o.extend MultiToml
8
+ end
9
+ end
10
+
11
+ subject { tomler.load('anything') }
12
+
13
+ describe 'toml adapter' do
14
+ before do
15
+ parser = Class.new
16
+ parser.class_eval do
17
+ def initialize(input)
18
+ end
19
+
20
+ def parsed
21
+ 'result'
22
+ end
23
+ end
24
+ toml = Module.new
25
+ toml.const_set :Parser, parser
26
+ Object.const_set(:TOML, toml)
27
+ tomler.adapter = :toml
28
+ end
29
+ after do
30
+ Object.send :remove_const, :TOML
31
+ end
32
+ it { should == 'result' }
33
+ end
34
+
35
+ describe 'toml_ruby adapter' do
36
+ before do
37
+ Object.const_set(:Toml, double(:load => 'result'))
38
+ tomler.adapter = :toml_ruby
39
+ end
40
+ after do
41
+ Object.send :remove_const, :Toml
42
+ end
43
+ it { should == 'result' }
44
+ end
45
+
46
+ describe 'toml2 adapter' do
47
+ before do
48
+ Object.const_set(:TOML, double(:load => 'result'))
49
+ tomler.adapter = :toml2
50
+ end
51
+ after do
52
+ Object.send :remove_const, :TOML
53
+ end
54
+ it { should == 'result' }
55
+ end
56
+
57
+ end
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+
3
+ describe 'MultiToml.detect_adapter' do
4
+
5
+ let(:tomler) do
6
+ Object.new.tap do |o|
7
+ o.extend MultiToml
8
+ end
9
+ end
10
+
11
+ subject { tomler.adapter }
12
+
13
+ describe 'when no adapter is available' do
14
+ before { tomler.stub(:require).and_raise(LoadError) }
15
+ it { should == nil }
16
+ end
17
+
18
+ # TODO: I would like to change these tests to actually load the libraries
19
+ # and then unload them after each run. I tried this four different
20
+ # ways and then gave up. Right now, they're pretty brittle tests.
21
+
22
+ describe 'when toml is available' do
23
+ before do
24
+ tomler.stub(:require).with('toml')
25
+ tomler.stub(:require).with('toml-ruby').and_raise(LoadError)
26
+ Object.const_set(:TOML, Object.new)
27
+ end
28
+ after do
29
+ Object.send :remove_const, :TOML
30
+ end
31
+ it { should == :toml }
32
+ end
33
+
34
+ describe 'when toml-ruby is available' do
35
+ before do
36
+ tomler.stub(:require).with('toml').and_raise(LoadError)
37
+ tomler.stub(:require).with('toml-ruby')
38
+ end
39
+ it { should == :toml_ruby }
40
+ end
41
+
42
+ describe 'when toml-ruby is available' do
43
+ before do
44
+ tomler.stub(:require).with('toml')
45
+ tomler.stub(:require).with('toml-ruby').and_raise(LoadError)
46
+ Object.const_set(:TOML, double(:load => ''))
47
+ end
48
+ after do
49
+ Object.send :remove_const, :TOML
50
+ end
51
+ it { should == :toml2 }
52
+ end
53
+
54
+ end
@@ -0,0 +1,20 @@
1
+ require 'helper'
2
+
3
+ describe 'MultiToml, when no adapter is loaded' do
4
+
5
+ describe '' do
6
+ let(:tomler) do
7
+ Object.new.tap do |o|
8
+ o.extend MultiToml
9
+ o.stub(:detect_adapter).and_return(nil)
10
+ end
11
+ end
12
+
13
+ subject { tomler.load('foo = bar') }
14
+
15
+ it 'raises an error' do
16
+ expect { subject }.to raise_error(MultiToml::NoParserError)
17
+ end
18
+ end
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_toml
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James A. Rosen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70095622560160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '10.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70095622560160
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70095622559680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.11'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70095622559680
36
+ description: A gem to provide easy switching between different TOML backends, including
37
+ toml, toml-ruby, and toml2.
38
+ email: james.a.rosen@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/multi_toml/implementation.rb
44
+ - lib/multi_toml.rb
45
+ - README.md
46
+ - LICENSE
47
+ - spec/adapter_spec.rb
48
+ - spec/detect_adapter_spec.rb
49
+ - spec/no_adapter_spec.rb
50
+ homepage: http://github.com/jamesarosen/multi_toml
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: A gem to provide swappable TOML backends.
74
+ test_files:
75
+ - spec/adapter_spec.rb
76
+ - spec/detect_adapter_spec.rb
77
+ - spec/no_adapter_spec.rb
78
+ has_rdoc: 'false'