using_yaml 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -36,5 +36,26 @@ Spec::Rake::SpecTask.new do |t|
36
36
  t.spec_files = Rake::FileList["spec/**/*_spec.rb"]
37
37
  t.spec_opts = ["-c"]
38
38
  end
39
-
39
+
40
+ desc "Run a simple performance benchmark"
41
+ task :benchmark do
42
+ require 'lib/using_yaml'
43
+ require 'benchmark'
44
+
45
+ was_squelched = UsingYAML.squelched?
46
+ UsingYAML.squelch!
47
+ class Person
48
+ include UsingYAML
49
+ using_yaml :children
50
+ end
51
+ p = Person.new
52
+ p.children # "cache" the nil
53
+ n = 10000
54
+ Benchmark.bmbm(10) do |x|
55
+ x.report("normal") { n.times do; p.children && p.children['something'] && p.children['invalid']; end }
56
+ x.report("chained") { n.times do; p.children.something.invalid; end }
57
+ end
58
+ UsingYAML.unsquelch! unless was_squelched
59
+ end
60
+
40
61
  task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -25,5 +25,6 @@ module UsingYAML
25
25
 
26
26
  # Load in the extensions for this instance
27
27
  array.extend(extensions)
28
+ array
28
29
  end
29
30
  end
@@ -13,7 +13,8 @@ module UsingYAML
13
13
  name = args.shift.to_s
14
14
 
15
15
  if args.empty?
16
- send(:[], name) || UsingYAML.add_extensions(nil)
16
+ value = send(:[], name)
17
+ value.nil? ? UsingYAML::NilClass : value
17
18
  elsif args.size == 1 && name =~ /(.+)=/
18
19
  # This is an "alias" turning self.key= into self[key]=
19
20
  # Also extends the incoming value so that it behaves
@@ -53,5 +54,6 @@ module UsingYAML
53
54
 
54
55
  # Load in the extensions for this instance
55
56
  hash.extend(extensions)
57
+ hash
56
58
  end
57
59
  end
@@ -1,10 +1,10 @@
1
1
  module UsingYAML
2
2
  def self.add_nilclass_extensions(instance, pathname)
3
3
  extensions = Module.new do
4
- define_method(:method_missing) do
4
+ define_method(:method_missing) do |*args|
5
5
  # Child objects should not have #save
6
6
  if respond_to? :save
7
- add_extensions(nil)
7
+ UsingYAML::NilClass
8
8
  else
9
9
  # One nil is the same as the next :)
10
10
  self
@@ -29,5 +29,6 @@ module UsingYAML
29
29
  end
30
30
 
31
31
  instance.extend(extensions)
32
+ instance
32
33
  end
33
34
  end
data/lib/using_yaml.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'yaml'
2
2
  require 'pathname'
3
- require 'using_yaml/array'
4
- require 'using_yaml/hash'
5
- require 'using_yaml/nilclass'
3
+
4
+ Dir[File.join(File.dirname(__FILE__), 'using_yaml/*')].each do |ext|
5
+ require ext
6
+ end
6
7
 
7
8
  # UsingYAML provides convenient class extensions which make it easy to
8
9
  # interact with YAML-storage of settings files by defining accessors
@@ -21,6 +22,8 @@ require 'using_yaml/nilclass'
21
22
  #
22
23
  # See +using_yaml+ for usage information.
23
24
  module UsingYAML
25
+ NilClass = add_nilclass_extensions(nil, nil)
26
+
24
27
  class << self
25
28
  # Extends the incoming Array/Hash with magic which makes it
26
29
  # possible to +save+ and (in the case of Hashes) use methods
@@ -31,10 +34,14 @@ module UsingYAML
31
34
  add_array_extensions(object, pathname)
32
35
  when Hash
33
36
  add_hash_extensions(object, pathname)
34
- when NilClass
35
- add_nilclass_extensions(object, pathname)
37
+ when ::NilClass
38
+ if pathname
39
+ add_nilclass_extensions(object, pathname)
40
+ else
41
+ UsingYAML::NilClass
42
+ end
36
43
  end
37
-
44
+
38
45
  object
39
46
  end
40
47
 
@@ -67,6 +74,11 @@ module UsingYAML
67
74
  def squelched?
68
75
  defined?(@@squelched) && @@squelched
69
76
  end
77
+
78
+ # Opposite of +squelch!+
79
+ def unsquelch!
80
+ @@squelched = false
81
+ end
70
82
  end
71
83
 
72
84
  def self.included(base)
@@ -130,7 +142,7 @@ module UsingYAML
130
142
 
131
143
  # If the yaml exists in our cache, then we don't need to hit
132
144
  # the disk.
133
- return yaml if yaml
145
+ return yaml if @using_yaml_cache.has_key? pathname
134
146
 
135
147
  # Safe disk read which either reads and parses a YAML object
136
148
  # (and caches it against future reads) or graciously ignores
@@ -142,7 +154,7 @@ module UsingYAML
142
154
  @using_yaml_cache[pathname] = UsingYAML.add_extensions(YAML.load_file(pathname), pathname)
143
155
  rescue Exception => e
144
156
  $stderr.puts "(UsingYAML) Could not load #{filename}: #{e.message}" unless UsingYAML.squelched?
145
- UsingYAML.add_extensions(nil)
157
+ @using_yaml_cache[pathname] = UsingYAML.add_extensions(nil, pathname)
146
158
  end
147
159
  end
148
160
 
@@ -11,7 +11,12 @@ describe 'UsingYAML' do
11
11
  end
12
12
 
13
13
  it "should gracefully handle nil.nil..." do
14
- @person.children.invalid.call.should be_nil
14
+ @person.children.something.invalid.should be_nil
15
+ end
16
+
17
+ it "should return false when expected" do
18
+ YAML.stubs(:load_file).with(anything).returns({ 'example' => false })
19
+ @person.children.example.should == false
15
20
  end
16
21
  end
17
22
 
data/using_yaml.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{using_yaml}
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
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"]
12
- s.date = %q{2010-03-06}
12
+ s.date = %q{2010-03-30}
13
13
  s.description = %q{"Load, save and use YAML files as if they were objects"}
14
14
  s.email = %q{marcbowes@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
35
35
  s.homepage = %q{http://github.com/marcbowes/UsingYAML}
36
36
  s.rdoc_options = ["--charset=UTF-8"]
37
37
  s.require_paths = ["lib"]
38
- s.rubygems_version = %q{1.3.5}
38
+ s.rubygems_version = %q{1.3.6}
39
39
  s.summary = %q{"Load, save and use YAML files"}
40
40
  s.test_files = [
41
41
  "spec/using_yaml/path_spec.rb",
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: using_yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 0
9
+ version: 1.1.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Marc Bowes
@@ -9,19 +14,21 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-06 00:00:00 -05:00
17
+ date: 2010-03-30 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: thoughtbot-shoulda
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
- version:
30
+ type: :development
31
+ version_requirements: *id001
25
32
  description: "\"Load, save and use YAML files as if they were objects\""
26
33
  email: marcbowes@gmail.com
27
34
  executables: []
@@ -59,18 +66,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
66
  requirements:
60
67
  - - ">="
61
68
  - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
62
71
  version: "0"
63
- version:
64
72
  required_rubygems_version: !ruby/object:Gem::Requirement
65
73
  requirements:
66
74
  - - ">="
67
75
  - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
68
78
  version: "0"
69
- version:
70
79
  requirements: []
71
80
 
72
81
  rubyforge_project:
73
- rubygems_version: 1.3.5
82
+ rubygems_version: 1.3.6
74
83
  signing_key:
75
84
  specification_version: 3
76
85
  summary: "\"Load, save and use YAML files\""