Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Autoload #1

Closed
wants to merge 2 commits into from

2 participants

Michael Krisher John Nunemaker
Michael Krisher

figured out my issue, Marshal.load was complaining about classes not yet being loaded, so I added an autoload method. Had actually found reference to defunkt having to do something similar in a memcache plugin. Digging that you had watchr setup already in place, wish everyone used watchr.

John Nunemaker
Owner

So long ago and bin is basically dead. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 28 additions and 3 deletions. Show diff stats Hide diff stats

  1. +12 2 lib/bin/store.rb
  2. +16 1 spec/bin/store_spec.rb
14 lib/bin/store.rb
@@ -25,11 +25,21 @@ def write(key, value, options={})
25 25 def read(key, options=nil)
26 26 super do
27 27 if doc = collection.find_one(:_id => key.to_s, :expires_at => {'$gt' => Time.now.utc})
28   - doc['raw'] ? doc['value'] : Marshal.load(doc['value'].to_s)
  28 + autoload_missing_constants do
  29 + doc['raw'] ? doc['value'] : Marshal.load(doc['value'].to_s)
  30 + end
29 31 end
30 32 end
31 33 end
32 34
  35 + def autoload_missing_constants
  36 + yield
  37 + rescue ArgumentError => error
  38 + lazy_load ||= Hash.new { |hash, hash_key| hash[hash_key] = true; false }
  39 + if error.to_s[/undefined class|referred/] && !lazy_load[error.to_s.split.last.sub(/::$/, '').constantize] then retry
  40 + else raise error end
  41 + end
  42 +
33 43 def delete(key, options=nil)
34 44 super do
35 45 collection.remove(:_id => key.to_s)
@@ -81,4 +91,4 @@ def counter_key_upsert(key, amount)
81 91 }, :upsert => true)
82 92 end
83 93 end
84   -end
  94 +end
17 spec/bin/store_spec.rb
@@ -97,6 +97,21 @@
97 97 it "works with symbol" do
98 98 store.read(:foo).should == 'bar'
99 99 end
  100 +
  101 + it "works with not yet loaded classes" do
  102 + class Klass
  103 + def initialize(str)
  104 + @str = str
  105 + end
  106 + def say_hello
  107 + @str
  108 + end
  109 + end
  110 + obj = Klass.new("hello\n")
  111 + store.write('foo', obj)
  112 + dump = store.read('foo')
  113 + dump.say_hello.should == "hello\n"
  114 + end
100 115 end
101 116
102 117 describe "#delete" do
@@ -207,4 +222,4 @@
207 222 end
208 223 end
209 224 end
210   -end
  225 +end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.