yaml-options 1.0.0

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.
@@ -0,0 +1 @@
1
+ require "yaml/options"
@@ -0,0 +1,55 @@
1
+ require "yaml"
2
+
3
+ module YAML
4
+ class Options < Hash
5
+ require "yaml/options/version"
6
+
7
+ def self.load(str)
8
+ hash = YAML.load(str)
9
+ raise ArgumentError, "str must be a YAML hash" unless hash.is_a?(Hash)
10
+ new(hash)
11
+ end
12
+
13
+ alias _key key
14
+ undef key
15
+
16
+ def initialize(hash = {})
17
+ hash.each do |key, value|
18
+ self[key] = convert(value)
19
+ end
20
+ end
21
+
22
+ def [](key)
23
+ super(key.to_sym)
24
+ end
25
+
26
+ def []=(key, value)
27
+ super(key.to_sym, value)
28
+ end
29
+
30
+ def method_missing(name, *args)
31
+ name_string = name.to_s
32
+ if name_string.chomp!("=")
33
+ self[name_string] = args.first
34
+ else
35
+ self[name]
36
+ end
37
+ end
38
+
39
+ def respond_to_missing?(name, include_private)
40
+ true
41
+ end
42
+
43
+ private
44
+ def convert(what)
45
+ case what
46
+ when Array
47
+ what.map { |element| convert(element) }
48
+ when Hash
49
+ self.class.new(what)
50
+ else
51
+ what
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,7 @@
1
+ require "yaml"
2
+
3
+ module YAML
4
+ class Options
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ require "test_helper"
2
+
3
+ describe YAML::Options do
4
+ describe "load" do
5
+ it "should parse YAML and return YAML::Options" do
6
+ options = YAML::Options.load("---\nkey: value\n")
7
+ options.must_be_instance_of(YAML::Options)
8
+ options.key.must_equal("value")
9
+ end
10
+ end
11
+
12
+ describe "_key" do
13
+ it "should be the original Hash#key" do
14
+ options = YAML::Options.new("key" => "value")
15
+ options._key("value").must_equal(:key)
16
+ end
17
+ end
18
+
19
+ describe "initialize" do
20
+ it "should convert a hash to YAML::Options" do
21
+ options = YAML::Options.new("key" => "value")
22
+ options.must_respond_to(:key)
23
+ options.key.must_equal("value")
24
+ end
25
+
26
+ it "should convert nested hash to nested YAML::Options" do
27
+ options = YAML::Options.new("top" => { "key" => "value" })
28
+ options.must_be_instance_of(YAML::Options)
29
+ options.top.must_be_instance_of(YAML::Options)
30
+ options.top.key.must_equal("value")
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml-options
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Samuel Kadolph
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
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: '3.2'
30
+ description: yaml-options
31
+ email:
32
+ - samuel@kadolph.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/yaml/options/version.rb
38
+ - lib/yaml/options.rb
39
+ - lib/yaml-options.rb
40
+ - test/yaml/options_test.rb
41
+ homepage: https://github.com/samuelkadolph/yaml-options
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.9.2
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: yaml-options
65
+ test_files:
66
+ - test/yaml/options_test.rb