Permalink
Browse files

Fixed a couple of persistence bugs and improved 1.8 compatibility.

  • Loading branch information...
1 parent 9f24525 commit 06a62d9cbf323cc804413bf156d680f88ada7469 @razielgn committed Jan 1, 2012
View
17 lib/tyrion/criteria.rb
@@ -1,7 +1,4 @@
module Tyrion
- class CriteriaException < Exception
- end
-
class Criteria
include Enumerable
@@ -67,7 +64,7 @@ def fetch_documents
case type
when :where
- @data.select! do |doc|
+ @data = @data.select do |doc|
args.each_pair.map do |k, v|
doc.read_attribute(k) == v
end.inject(&:&)
@@ -78,8 +75,16 @@ def fetch_documents
@data = Array(@data[args..-1])
when :sort
keys, dir = args
- @data.sort_by! do |doc|
- keys.map{|s| doc.read_attribute(s) }
+ if @data.respond_to? :sort_by!
+ @data.sort_by! do |doc|
+ keys.map{|s| doc.read_attribute(s) }
+ end
+ else
+ @data.sort! do |a, b|
+ first = keys.map{|s| a.read_attribute(s) }
+ second = keys.map{|s| b.read_attribute(s) }
+ first <=> second
+ end
end
@data.reverse! unless dir
end
View
2 lib/tyrion/document.rb
@@ -9,8 +9,8 @@ def self.included(receiver)
include Tyrion::Attributes
include Tyrion::Querying
include Tyrion::Validations
- include Tyrion::Storage
include Tyrion::Persistence
+ include Tyrion::Storage
include InstanceMethods
end
end
View
1 lib/tyrion/persistence.rb
@@ -12,6 +12,7 @@ def create(*args)
def delete_all
storage[klass_name].clear
+ save_storage(klass_name)
end
end
View
2 lib/tyrion/storage.rb
@@ -17,7 +17,7 @@ def reload
if File.exists?(path)
raw_file = File.read(path)
- storage[klass_name] = MultiJson.decode(raw_file).map{ |doc| create doc }
+ storage[klass_name] = MultiJson.decode(raw_file).map{ |doc| new(doc) }
else
storage[klass_name] = []
end
View
2 lib/tyrion/version.rb
@@ -1,3 +1,3 @@
module Tyrion #:nodoc:
- VERSION = '0.2.0'
+ VERSION = '0.3.0'
end
View
12 spec/persistence_spec.rb
@@ -1,10 +1,15 @@
require 'test_helper'
require 'examples/post'
-add_connection_cleanup!
-
describe Tyrion::Persistence do
- before(:each){ Post.reload }
+ before(:each) do
+ Tyrion::Connection.path = Dir.mktmpdir
+ Post.reload
+ end
+
+ after(:each) do
+ FileUtils.rm_rf Tyrion::Connection.path
+ end
describe '.create' do
it 'should save a new instance with given attributes' do
@@ -19,6 +24,7 @@
it 'should delete every document' do
5.times{ Post.create :title => 'Title', :body => 'Body' }
Post.delete_all
+ Post.reload
Post.count.should == 0
end
end
View
12 spec/test_helper.rb
@@ -14,15 +14,3 @@
FileUtils.rm_rf tmpdir
end
end
-
-def add_connection_cleanup!
- RSpec.configure do |config|
- config.before(:each) do
- Tyrion::Connection.path = Dir.mktmpdir
- end
-
- config.after(:each) do
- FileUtils.rm_rf Tyrion::Connection.path
- end
- end
-end

0 comments on commit 06a62d9

Please sign in to comment.