using_yaml 0.3.1 → 0.3.3

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.
data/README.rdoc CHANGED
@@ -41,7 +41,46 @@ From Gemcutter:
41
41
 
42
42
  == Pathname
43
43
 
44
- Coming soon!
44
+ By default, UsingYAML will look for .yml files in your home directory. There are several ways to configure this:
45
+
46
+ === With strings:
47
+
48
+ class ExampleUsage
49
+ include UsingYAML
50
+
51
+ using_yaml :example, :path => '/your/path/here'
52
+ end
53
+
54
+ === Using a Proc:
55
+
56
+ class ExampleUsage
57
+ include UsingYAML
58
+
59
+ using_yaml :example, :path => lambda { |c| c.pathname }
60
+ attr_accessor :pathname
61
+ end
62
+
63
+ example = ExampleUsage.new
64
+ example.pathname = '/your/path/here'
65
+
66
+ === Overriding using_yaml_path:
67
+
68
+ class ExampleUsage
69
+ include UsingYAML
70
+
71
+ using_yaml :example
72
+
73
+ def using_yaml_path
74
+ '/your/code/here'
75
+ end
76
+ end
77
+
78
+
79
+ == Error messages
80
+
81
+ By default, UsingYAML will return nil for missing files. It will also complain on STDERR. If you want to disable the complaint:
82
+
83
+ UsingYAML.squelch!
45
84
 
46
85
  == Note on Patches/Pull Requests
47
86
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.3
@@ -1,31 +1,44 @@
1
- def OpenHash(hash, pathname = nil)
2
- Module.new do
3
- hash.each_pair do |key, value|
4
- define_method(key) do
5
- case value
6
- when Hash
7
- value.to_ohash
8
- when Array
9
- value.collect { |i| i === Hash ? i.to_ohash : i }
10
- else
11
- value
1
+ module OpenHash
2
+ class << self
3
+ def from_hash(hash, pathname = nil)
4
+ Module.new do
5
+ hash.each_pair do |key, value|
6
+ define_method(key) do
7
+ value.respond_to?(:to_ohash) ? value.to_ohash : value
8
+ end
9
+
10
+ define_method("#{key}=") do |value|
11
+ case value
12
+ when Hash
13
+ send(:[]=, key, value.to_ohash)
14
+ else
15
+ send(:[]=, key, value)
16
+ end
17
+ end
12
18
  end
13
- end
14
19
 
15
- define_method("#{key}=") do |value|
16
- case value
17
- when Hash
18
- send(:[]=, key, value.to_ohash)
19
- else
20
- send(:[]=, key, value)
20
+ if pathname
21
+ define_method(:save) do
22
+ File.open(pathname, 'w') do |f|
23
+ f.puts self.to_yaml
24
+ end
25
+ end
21
26
  end
22
27
  end
23
28
  end
24
29
 
25
- if pathname
26
- define_method(:save) do
27
- File.open(pathname, 'w') do |f|
28
- f.puts self.to_yaml
30
+ def from_array(array, pathname = nil)
31
+ Module.new do
32
+ array.each do |e|
33
+ e.to_ohash(e)
34
+ end
35
+
36
+ if pathname
37
+ define_method(:save) do
38
+ File.open(pathname, 'w') do |f|
39
+ f.puts self.to_yaml
40
+ end
41
+ end
29
42
  end
30
43
  end
31
44
  end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def to_ohash(pathname)
3
+ self.extend OpenHash.from_array(self, pathname)
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  class Hash
2
2
  def to_ohash(pathname)
3
- self.extend OpenHash(self, pathname)
3
+ self.extend OpenHash.from_hash(self, pathname)
4
4
  end
5
5
  end
data/lib/using_yaml.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'yaml'
2
2
  require 'pathname'
3
3
  require 'using_yaml/open_hash'
4
+ require 'using_yaml/patches/array'
4
5
  require 'using_yaml/patches/hash'
5
6
 
6
7
  module UsingYAML
@@ -10,7 +10,14 @@ describe 'UsingYAML#core' do
10
10
  end
11
11
 
12
12
  it "should return valid settings files" do
13
- YAML.stubs(:load_file).with(anything).returns({})
14
- @person.children.should == {}
13
+ YAML.stubs(:load_file).with(anything).returns({ 'foo' => 'bar' })
14
+ @person.children.should == { 'foo' => 'bar' }
15
+ @person.children.foo.should == 'bar'
16
+ end
17
+
18
+ it "should work with arrays" do
19
+ YAML.stubs(:load_file).with(anything).returns([ { 'foo' => 'bar' } ])
20
+ @person.children.class.name.should == 'Array'
21
+ @person.children.first.should == { 'foo' => 'bar' }
15
22
  end
16
23
  end
data/using_yaml.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{using_yaml}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marc Bowes"]
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "lib/using_yaml.rb",
27
27
  "lib/using_yaml/open_hash.rb",
28
+ "lib/using_yaml/patches/array.rb",
28
29
  "lib/using_yaml/patches/hash.rb",
29
30
  "spec/spec_helper.rb",
30
31
  "spec/using_yaml/path_spec.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: using_yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc Bowes
@@ -40,6 +40,7 @@ files:
40
40
  - VERSION
41
41
  - lib/using_yaml.rb
42
42
  - lib/using_yaml/open_hash.rb
43
+ - lib/using_yaml/patches/array.rb
43
44
  - lib/using_yaml/patches/hash.rb
44
45
  - spec/spec_helper.rb
45
46
  - spec/using_yaml/path_spec.rb