Skip to content

Commit

Permalink
Optimized ERB rendering a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Aug 7, 2010
1 parent 606e328 commit f2bb986
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
migrate your cassettes and deal with migration warnings, then upgrade to the current release.
* Added some code to VCR::Cassette.new to check the options passed to the cassette and raise an error if any
invalid options are passed.
* Optimized ERB rendering a bit. Rather than creating a new struct subclass for each time we render an ERB
cassette with locals, we keep a cache of reusable struct subclasses based on the desired attributes.
[Benchmarking](http://gist.github.com/512948) reveals this is about 28% faster.

## 1.0.3 (August 5, 2010)

Expand Down
6 changes: 5 additions & 1 deletion lib/vcr/cassette.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def load_recorded_interactions
VCR.http_stubbing_adapter.stub_requests(recorded_interactions)
end

@@struct_cache = Hash.new do |hash, attributes|
hash[attributes] = Struct.new(*attributes)
end

def raw_yaml_content
content = File.read(file)
return content unless @erb
Expand All @@ -114,7 +118,7 @@ def raw_yaml_content
return template.result unless @erb.is_a?(Hash)

# create an object with methods for each desired local variable...
local_variables = Struct.new(*@erb.keys).new(*@erb.values)
local_variables = @@struct_cache[@erb.keys].new(*@erb.values)

# instance_eval seems to be the only way to get the binding for ruby 1.9: http://redmine.ruby-lang.org/issues/show/2161
template.result(local_variables.instance_eval { binding })
Expand Down

0 comments on commit f2bb986

Please sign in to comment.