Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switched from store to adapter.

Makes more sense since that is all the store is.
  • Loading branch information...
commit d45e6f31164942cae606c7295ad9d6705045fbda 1 parent 0bfc1c7
@jnunemaker authored
View
4 examples/attributes_abbreviation.rb
@@ -10,7 +10,7 @@
class User
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
attribute :email, String
attribute :my_really_long_field_name, String, :abbr => :my
@@ -21,6 +21,6 @@ class User
:my_really_long_field_name => 'something',
})
-pp Marshal.load(User.store.client[user.id])
+pp Marshal.load(User.adapter.client[user.id])
# Abbreviated attributes are stored in the database as the abbreviation for when you want to conserve space. The abbreviation and the full attribute name work exactly the same in Ruby, the only difference is how they get persisted.
# {"my"=>"something", "email"=>"nunemaker@gmail.com"}
View
4 examples/attributes_virtual.rb
@@ -10,7 +10,7 @@
class User
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
attribute :email, String
attribute :crypted_password, String
@@ -36,6 +36,6 @@ def encrypt(password)
:password_confirmation => 'testing',
})
-pp Marshal.load(User.store.client[user.id])
+pp Marshal.load(User.adapter.client[user.id])
# Virtual attributes are never persisted. In the data store, only email and crypted_password are stored.
# {"crypted_password"=>"testing", "email"=>"nunemaker@gmail.com"}
View
2  examples/identity_map.rb
@@ -16,7 +16,7 @@
class User
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
# identity_map_off # if uncommented, User would not use identity map
View
2  examples/memcached.rb
@@ -10,7 +10,7 @@
class GameList
include Toy::Store
- store :memcached, Memcached.new
+ adapter :memcached, Memcached.new
attribute :source, Hash
end
View
2  examples/memory.rb
@@ -10,7 +10,7 @@
class User
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
attribute :name, String
end
View
6 examples/models.rb
@@ -9,7 +9,7 @@
class Address
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
attribute :city, String
attribute :state, String
@@ -25,14 +25,14 @@ class PhoneNumber
class Company
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
attribute :name, String
end
class User
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
attribute :name, String
attribute :age, Integer
View
2  examples/mongo.rb
@@ -10,7 +10,7 @@
class User
include Toy::Store
- store :mongo, Mongo::Connection.new.db('adapter')['testing']
+ adapter :mongo, Mongo::Connection.new.db('adapter')['testing']
attribute :name, String
end
View
2  examples/redis.rb
@@ -10,7 +10,7 @@
class User
include Toy::Store
- store :redis, Redis.new
+ adapter :redis, Redis.new
attribute :name, String
end
View
2  examples/riak.rb
@@ -10,7 +10,7 @@
class GameList
include Toy::Store
- store :riak, Riak::Client.new['adapter_example']
+ adapter :riak, Riak::Client.new['adapter_example']
attribute :source, Hash
end
View
4 lib/toy.rb
@@ -29,7 +29,7 @@ def reset
models.clear
end
- # Clears all the stores for all the models. Useful in specs/tests/etc.
+ # Clears all the adapters for all the models. Useful in specs/tests/etc.
# Do not use in production, harty harr harr.
#
# Note: that if your models are auto-loaded like in Rails, you will need
@@ -37,7 +37,7 @@ def reset
# not be aware of their existence.
def clear
models.each do |model|
- model.store.clear if model.has_store?
+ model.adapter.clear if model.has_adapter?
model.identity_map.clear if model.identity_map_on?
end
end
View
2  lib/toy/attributes.rb
@@ -40,7 +40,7 @@ def initialize_from_database(attrs={})
end
def reload
- if attrs = store.read(id)
+ if attrs = adapter.read(id)
attrs['id'] = id
instance_variables.each { |ivar| instance_variable_set(ivar, nil) }
initialize_attributes_with_defaults
View
6 lib/toy/identity_map.rb
@@ -48,7 +48,7 @@ def get(id)
def get_from_identity_map(id)
return nil unless identity_map_on?
if record = identity_map[id]
- log_operation(:img, self.name, store, id)
+ log_operation(:img, self.name, adapter, id)
record
end
end
@@ -81,13 +81,13 @@ def delete(*)
def add_to_identity_map
return unless self.class.identity_map_on?
identity_map[id] = self
- log_operation(:ims, self.class.name, store, id)
+ log_operation(:ims, self.class.name, adapter, id)
end
def remove_from_identity_map
return unless self.class.identity_map_on?
identity_map.delete(id)
- log_operation(:imd, self.class.name, store, id)
+ log_operation(:imd, self.class.name, adapter, id)
end
private
View
41 lib/toy/persistence.rb
@@ -3,15 +3,27 @@ module Persistence
extend ActiveSupport::Concern
module ClassMethods
- def store(name=nil, client=nil, options={})
+ def adapter(name=nil, client=nil, options={})
assert_client(name, client)
- @store = Adapter[name].new(client, options) if !name.nil? && !client.nil?
- assert_store(name, client)
- @store
+ if !name.nil? && !client.nil?
+ @adapter = Adapter[name].new(client, options)
+ end
+ assert_adapter(name, client)
+ @adapter
+ end
+
+ def store(*args)
+ warn '[DEPRECATED] store is deprecated in favor of adapter'
+ adapter(*args)
+ end
+
+ def has_adapter?
+ !@adapter.nil?
end
def has_store?
- !@store.nil?
+ warn '[DEPRECATED] has_store? is deprecated in favor of has_adapter?'
+ has_adapter?
end
def create(attrs={})
@@ -31,14 +43,19 @@ def assert_client(name, client)
raise(ArgumentError, 'Client is required') if !name.nil? && client.nil?
end
- def assert_store(name, client)
- raise(StandardError, "No store has been set") if name.nil? && client.nil? && !has_store?
+ def assert_adapter(name, client)
+ raise(StandardError, "No adapter has been set") if name.nil? && client.nil? && !has_adapter?
end
end
module InstanceMethods
+ def adapter
+ self.class.adapter
+ end
+
def store
- self.class.store
+ warn '[DEPRECATED] store is deprecated in favor of adapter'
+ self.class.adapter
end
def new_record?
@@ -68,8 +85,8 @@ def destroy
def delete
@_destroyed = true
- log_operation(:del, self.class.name, store, id)
- store.delete(id)
+ log_operation(:del, self.class.name, adapter, id)
+ adapter.delete(id)
end
private
@@ -88,8 +105,8 @@ def persist
def persist!
attrs = persisted_attributes
attrs.delete('id') # no need to persist id as that is key
- store.write(id, attrs)
- log_operation(:set, self.class.name, store, id, attrs)
+ adapter.write(id, attrs)
+ log_operation(:set, self.class.name, adapter, id, attrs)
persist
each_embedded_object { |doc| doc.send(:persist) }
true
View
8 lib/toy/querying.rb
@@ -4,8 +4,8 @@ module Querying
module ClassMethods
def get(id)
- log_operation(:get, self, store, id)
- load(id, store.read(id))
+ log_operation(:get, self, adapter, id)
+ load(id, adapter.read(id))
end
def get!(id)
@@ -25,8 +25,8 @@ def get_or_create(id)
end
def key?(id)
- log_operation(:key, self, store, id)
- store.key?(id)
+ log_operation(:key, self, adapter, id)
+ adapter.key?(id)
end
alias :has_key? :key?
View
2  lib/toy/validations.rb
@@ -20,7 +20,7 @@ def validates_embedded(*names)
invalid.each do |obj|
invalid_messages << [obj.attributes, obj.errors.full_messages]
end
- log_operation(:iem, self.name, store, record.id, invalid_messages)
+ log_operation(:iem, self.name, adapter, record.id, invalid_messages)
end
end
end
View
12 perf/reads.rb
@@ -13,7 +13,7 @@
class User
include Toy::Store
identity_map_off
- store(:memory, {})
+ adapter(:memory, {})
attribute :name, String
end
@@ -22,19 +22,19 @@ class User
times = 10_000
client_result = Benchmark.realtime {
- times.times { User.store.decode(User.store.client[User.store.key_for(id)]) }
+ times.times { User.adapter.decode(User.adapter.client[User.adapter.key_for(id)]) }
}
-store_result = Benchmark.realtime {
+adapter_result = Benchmark.realtime {
times.times { User.get(id) }
}
puts 'Client', client_result
-puts 'Toystore', store_result
-puts 'Ratio', store_result / client_result
+puts 'Toystore', adapter_result
+puts 'Ratio', adapter_result / client_result
# PerfTools::CpuProfiler.start('prof_client') do
-# times.times{ User.store.decode(User.store.client[User.store.key_for(id)]) }
+# times.times{ User.adapter.decode(User.adapter.client[User.adapter.key_for(id)]) }
# end
# PerfTools::CpuProfiler.start('prof_reads') do
View
10 perf/writes.rb
@@ -13,7 +13,7 @@
class User
include Toy::Store
identity_map_off
- store(:memory, {})
+ adapter(:memory, {})
end
times = 10_000
@@ -22,12 +22,12 @@ class User
attrs = user.persisted_attributes
client_result = Benchmark.realtime {
- times.times { User.store.write(id, attrs) }
+ times.times { User.adapter.write(id, attrs) }
}
-store_result = Benchmark.realtime {
+adapter_result = Benchmark.realtime {
times.times { User.create }
}
puts 'Client', client_result
-puts 'Toystore', store_result
-puts 'Ratio', store_result / client_result
+puts 'Toystore', adapter_result
+puts 'Ratio', adapter_result / client_result
View
2  spec/support/constants.rb
@@ -34,7 +34,7 @@ def self.name; '#{name}' end
def self.to_s; '#{name}' end
""" if name
model.send(:include, Toy::Store)
- model.store(:memory, {})
+ model.adapter(:memory, {})
end
end
end
View
4 spec/toy/attributes_spec.rb
@@ -323,9 +323,9 @@
user.twitter_access_token.should == '1234'
end
- it "persists to store using abbreviation" do
+ it "persists to adapter using abbreviation" do
user = User.create(:twitter_access_token => '1234')
- raw = user.store.read(user.id)
+ raw = user.adapter.read(user.id)
raw['tat'].should == '1234'
raw.should_not have_key('twitter_access_token')
end
View
4 spec/toy/identity/uuid_key_factory_spec.rb
@@ -3,11 +3,11 @@
describe Toy::Identity::UUIDKeyFactory do
uses_constants('User')
- it "should use String as store_type" do
+ it "should use String as key_type" do
Toy::Identity::UUIDKeyFactory.new.key_type.should be(String)
end
- it "should use uuid for next key" do
+ it "should use uuid for next_key" do
Toy::Identity::UUIDKeyFactory.new.next_key(nil).length.should == 36
end
View
6 spec/toy/identity_map_spec.rb
@@ -60,7 +60,7 @@
it "does not query if in map" do
user = User.create
user.should be_in_identity_map
- user.store.should_not_receive(:read)
+ user.adapter.should_not_receive(:read)
User.get(user.id).should equal(user)
end
end
@@ -69,7 +69,7 @@
it "forces new query each time and skips the identity map" do
user = User.create
user.should be_in_identity_map
- User.store.should_receive(:read).with(user.id).and_return({})
+ User.adapter.should_receive(:read).with(user.id).and_return({})
user.reload
end
end
@@ -117,7 +117,7 @@
user = User.create
user.should be_in_identity_map
User.identity_map_off
- user.store.should_receive(:read).with(user.id).and_return(user.persisted_attributes)
+ user.adapter.should_receive(:read).with(user.id).and_return(user.persisted_attributes)
User.get(user.id)
end
end
View
50 spec/toy/persistence_spec.rb
@@ -7,43 +7,43 @@
Class.new { include Toy::Store }
end
- describe ".store" do
+ describe ".adapter" do
it "sets if arguments and reads if not" do
- User.store(:memory, {})
- User.store.should == Adapter[:memory].new({})
+ User.adapter(:memory, {})
+ User.adapter.should == Adapter[:memory].new({})
end
it "defaults options to empty hash" do
Adapter[:memory].should_receive(:new).with({}, {})
- User.store(:memory, {})
+ User.adapter(:memory, {})
end
it "works with options" do
Adapter[:memory].should_receive(:new).with({}, :something => true)
- User.store(:memory, {}, :something => true)
+ User.adapter(:memory, {}, :something => true)
end
it "raises argument error if name provided but not client" do
lambda do
- klass.store(:memory)
+ klass.adapter(:memory)
end.should raise_error(ArgumentError, 'Client is required')
end
it "raises argument error if no name or client provided and has not been set" do
lambda do
- klass.store
- end.should raise_error(StandardError, 'No store has been set')
+ klass.adapter
+ end.should raise_error(StandardError, 'No adapter has been set')
end
end
- describe ".has_store?" do
- it "returns true if store set" do
- klass.store(:memory, {})
- klass.has_store?.should be_true
+ describe ".has_adapter?" do
+ it "returns true if adapter set" do
+ klass.adapter(:memory, {})
+ klass.has_adapter?.should be_true
end
- it "returns false if store not set" do
- klass.has_store?.should be_false
+ it "returns false if adapter not set" do
+ klass.has_adapter?.should be_false
end
end
@@ -56,7 +56,7 @@
let(:doc) { @doc }
it "creates key in database with attributes" do
- User.store.read(doc.id).should == {
+ User.adapter.read(doc.id).should == {
'name' => 'John',
'age' => 50,
}
@@ -113,9 +113,9 @@
end
end
- describe "#store" do
+ describe "#adapter" do
it "delegates to class" do
- User.new.store.should equal(User.store)
+ User.new.adapter.should equal(User.adapter)
end
end
@@ -163,7 +163,7 @@
end
it "does not persist virtual attributes" do
- @doc.store.read(@doc.id).should_not include('accepted_terms')
+ @doc.adapter.read(@doc.id).should_not include('accepted_terms')
end
end
@@ -171,23 +171,23 @@
before do
@doc = User.create(:name => 'John', :age => 28)
@key = @doc.id
- @value = User.store.read(@doc.id)
+ @value = User.adapter.read(@doc.id)
@doc.name = 'Bill'
@doc.accepted_terms = false
@doc.save
end
let(:doc) { @doc }
- it "stores in same key" do
+ it "does not change primary key" do
doc.id.should == @key
end
- it "updates value in store" do
- User.store.read(doc.id).should_not == @value
+ it "updates value in adapter" do
+ User.adapter.read(doc.id).should_not == @value
end
it "does not persist virtual attributes" do
- @doc.store.read(@doc.id).should_not include('accepted_terms')
+ @doc.adapter.read(@doc.id).should_not include('accepted_terms')
end
it "updates the attributes in the instance" do
@@ -211,7 +211,7 @@
end
describe "#delete" do
- it "should remove the instance from the store" do
+ it "should remove the instance" do
doc = User.create
doc.delete
User.key?(doc.id).should be_false
@@ -219,7 +219,7 @@
end
describe "#destroy" do
- it "should remove the instance from the store" do
+ it "should remove the instance" do
doc = User.create
doc.destroy
User.key?(doc.id).should be_false
View
4 spec/toy_spec.rb
@@ -4,7 +4,7 @@
uses_constants('User', 'Game', 'Move')
describe ".clear" do
- it "can clear all the stores in one magical moment" do
+ it "can clear all the adapters in one magical moment" do
Game.embedded_list(:moves)
user = User.create!
game = Game.create!(:moves => [Move.new])
@@ -13,7 +13,7 @@
Game.get(game.id).should be_nil
end
- it "does not raise error when no default store set" do
+ it "does not raise error when no default adapter set" do
klass = Class.new { include Toy::Store }
lambda { Toy.clear }.should_not raise_error
end
View
2  test/lint_test.rb
@@ -28,7 +28,7 @@ def clear
class User
include Toy::Store
- store :memory, {}
+ adapter :memory, {}
end
class LintTest < ActiveModel::TestCase
Please sign in to comment.
Something went wrong with that request. Please try again.