lib/toy/persistence.rb in toystore-0.8.3 vs lib/toy/persistence.rb in toystore-0.9.0
- old
+ new
@@ -1,19 +1,24 @@
module Toy
module Persistence
extend ActiveSupport::Concern
module ClassMethods
- def store(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
- end
+ def adapter(name=nil, client=nil, options={})
+ missing_client = !name.nil? && client.nil?
+ raise(ArgumentError, 'Client is required') if missing_client
- def has_store?
- !@store.nil?
+ needs_default_adapter = name.nil? && client.nil?
+ assigning_adapter = !name.nil? && !client.nil?
+
+ if needs_default_adapter
+ @adapter ||= Adapter[:memory].new({}, options)
+ elsif assigning_adapter
+ @adapter = Adapter[name].new(client, options)
+ end
+
+ @adapter
end
def create(attrs={})
new(attrs).tap { |doc| doc.save }
end
@@ -23,77 +28,82 @@
end
def destroy(*ids)
ids.each { |id| get(id).try(:destroy) }
end
+ end
- private
- def assert_client(name, client)
- raise(ArgumentError, 'Client is required') if !name.nil? && client.nil?
- end
+ def adapter
+ self.class.adapter
+ end
- def assert_store(name, client)
- raise(StandardError, "No store has been set") if name.nil? && client.nil? && !has_store?
- end
+ def initialize(attrs={})
+ @_new_record = true
+ super
end
- module InstanceMethods
- def store
- self.class.store
- end
+ def initialize_from_database(attrs={})
+ @_new_record = false
+ initialize_attributes_with_defaults
+ send("attributes=", attrs, false)
+ self
+ end
- def new_record?
- @_new_record == true
- end
+ def initialize_copy(other)
+ super
+ @_new_record = true
+ @_destroyed = false
+ end
- def destroyed?
- @_destroyed == true
- end
+ def new_record?
+ @_new_record == true
+ end
- def persisted?
- !new_record? && !destroyed?
- end
+ def destroyed?
+ @_destroyed == true
+ end
- def save(*)
- new_record? ? create : update
- end
+ def persisted?
+ !new_record? && !destroyed?
+ end
- def update_attributes(attrs)
- self.attributes = attrs
- save
- end
+ def save(*)
+ new_record? ? create : update
+ end
- def destroy
- delete
- end
+ def update_attributes(attrs)
+ self.attributes = attrs
+ save
+ end
- def delete
- @_destroyed = true
- log_operation(:del, self.class.name, store, id)
- store.delete(id)
- end
+ def destroy
+ delete
+ end
- private
- def create
- persist!
- end
+ def delete
+ @_destroyed = true
+ adapter.delete(id)
+ end
- def update
- persist!
- end
+ private
- def persist
- @_new_record = false
- end
+ def create
+ persist!
+ end
- 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)
- persist
- each_embedded_object { |doc| doc.send(:persist) }
- true
- end
+ def update
+ persist!
+ end
+
+ def persist
+ @_new_record = false
+ end
+
+ def persist!
+ attrs = persisted_attributes
+ attrs.delete('id') # no need to persist id as that is key
+ adapter.write(id, attrs)
+ persist
+ true
end
end
end
\ No newline at end of file