Class: DataMapper::Adapters::InMemoryAdapter

### Home Classes Methods

### In Files

- lib/dm-core/adapters/in_memory_adapter.rb

### Parent

DataMapper::Adapters::AbstractAdapter

### Methods

- ::new
- #create
- #delete
- #read
- #reset
- #update

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Adapters
- DataMapper::Adapters::AbstractAdapter
- DataMapper::Adapters::InMemoryAdapter
- DataMapper::Assertions
- DataMapper::Associations
- DataMapper::Associations::ManyToMany
- DataMapper::Associations::ManyToMany::Collection
- DataMapper::Associations::ManyToMany::Relationship
- DataMapper::Associations::ManyToOne
- DataMapper::Associations::ManyToOne::Relationship
- DataMapper::Associations::OneToMany
- DataMapper::Associations::OneToMany::Collection
- DataMapper::Associations::OneToMany::Relationship
- DataMapper::Associations::OneToOne
- DataMapper::Associations::OneToOne::Relationship
- DataMapper::Associations::Relationship
- DataMapper::Chainable
- DataMapper::Collection
- DataMapper::Deprecate
- DataMapper::DescendantSet
- DataMapper::Equalizer
- DataMapper::Ext
- DataMapper::Ext::Array
- DataMapper::Ext::Hash
- DataMapper::Ext::Module
- DataMapper::Ext::Object
- DataMapper::Ext::String
- DataMapper::Hook
- DataMapper::Hook::ClassMethods
- DataMapper::IdentityMap
- DataMapper::ImmutableDeletedError
- DataMapper::ImmutableError
- DataMapper::IncompleteModelError
- DataMapper::Inflector
- DataMapper::Inflector::Inflections
- DataMapper::LocalObjectSpace
- DataMapper::Logger
- DataMapper::Mash
- DataMapper::Model
- DataMapper::Model::Hook
- DataMapper::Model::Hook::MethodCommand
- DataMapper::Model::Hook::Methods
- DataMapper::Model::Hook::ProcCommand
- DataMapper::Model::Is
- DataMapper::Model::Property
- DataMapper::Model::Relationship
- DataMapper::Model::Scope
- DataMapper::NamingConventions
- DataMapper::NamingConventions::Field
- DataMapper::NamingConventions::Field::Underscored
- DataMapper::NamingConventions::Field::UnderscoredAndPluralized
- DataMapper::NamingConventions::Field::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Field::Yaml
- DataMapper::NamingConventions::Resource
- DataMapper::NamingConventions::Resource::Underscored
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralized
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutLeadingModule
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Resource::Yaml
- DataMapper::ObjectNotFoundError
- DataMapper::OrderedSet
- DataMapper::OrderedSet::Cache
- DataMapper::OrderedSet::Cache::API
- DataMapper::PersistenceError
- DataMapper::PluginNotFoundError
- DataMapper::Property
- DataMapper::Property::Binary
- DataMapper::Property::Boolean
- DataMapper::Property::Class
- DataMapper::Property::Date
- DataMapper::Property::DateTime
- DataMapper::Property::Decimal
- DataMapper::Property::Discriminator
- DataMapper::Property::Discriminator::Model
- DataMapper::Property::Float
- DataMapper::Property::Integer
- DataMapper::Property::Lookup
- DataMapper::Property::Numeric
- DataMapper::Property::Object
- DataMapper::Property::PassThroughLoadDump
- DataMapper::Property::Serial
- DataMapper::Property::String
- DataMapper::Property::Text
- DataMapper::Property::Time
- DataMapper::Property::Typecast
- DataMapper::Property::Typecast::Numeric
- DataMapper::Property::Typecast::Time
- DataMapper::PropertySet
- DataMapper::Query
- DataMapper::Query::Conditions
- DataMapper::Query::Conditions::AbstractComparison
- DataMapper::Query::Conditions::AbstractOperation
- DataMapper::Query::Conditions::AndOperation
- DataMapper::Query::Conditions::Comparison
- DataMapper::Query::Conditions::EqualToComparison
- DataMapper::Query::Conditions::FlattenOperation
- DataMapper::Query::Conditions::GreaterThanComparison
- DataMapper::Query::Conditions::GreaterThanOrEqualToComparison
- DataMapper::Query::Conditions::InclusionComparison
- DataMapper::Query::Conditions::LessThanComparison
- DataMapper::Query::Conditions::LessThanOrEqualToComparison
- DataMapper::Query::Conditions::LikeComparison
- DataMapper::Query::Conditions::NotOperation
- DataMapper::Query::Conditions::NullOperation
- DataMapper::Query::Conditions::Operation
- DataMapper::Query::Conditions::OrOperation
- DataMapper::Query::Conditions::RegexpComparison
- DataMapper::Query::Conditions::RelationshipHandler
- DataMapper::Query::Direction
- DataMapper::Query::Operator
- DataMapper::Query::Path
- DataMapper::Query::Sort
- DataMapper::RelationshipSet
- DataMapper::Repository
- DataMapper::RepositoryNotSetupError
- DataMapper::Resource
- DataMapper::Resource::PersistenceState
- DataMapper::Resource::PersistenceState::Clean
- DataMapper::Resource::PersistenceState::Deleted
- DataMapper::Resource::PersistenceState::Dirty
- DataMapper::Resource::PersistenceState::Immutable
- DataMapper::Resource::PersistenceState::Persisted
- DataMapper::Resource::PersistenceState::Transient
- DataMapper::SaveFailureError
- DataMapper::Spec
- DataMapper::Spec::Adapters
- DataMapper::Spec::Adapters::Adapter
- DataMapper::Spec::Adapters::Helpers
- DataMapper::Spec::Adapters::InMemoryAdapter
- DataMapper::Spec::CollectionHelpers
- DataMapper::Spec::CollectionHelpers::GroupMethods
- DataMapper::Spec::Helpers
- DataMapper::Spec::PendingHelpers
- DataMapper::Subject
- DataMapper::SubjectSet
- DataMapper::SubjectSet::NameCache
- DataMapper::Undefined
- DataMapper::UnknownRelationshipError
- DataMapper::UnsavedParentError
- DataMapper::UpdateConflictError
- Blog
- Blog::Article
- ChildSubType
- CounterAdapter
- Heffalump
- Kernel
- LazyArray
- Object
- OtherComparison
- Pathname
- SubType
- Symbol

No matching classes.

# DataMapper::Adapters::InMemoryAdapter

This is probably the simplest functional adapter possible. It simply stores and queries from a hash containing the model classes as keys, and an array of hashes. It is not persistent whatsoever; when the Ruby process finishes, everything that was stored it lost. However, it doesn't require any other external libraries, such as data_objects, so it is ideal for writing specs against. It also serves as an excellent example for budding adapter developers, so it is critical that it remains well documented and up to date.

### Public Class Methods

new(name, options = {}) click to toggle source

Make a new instance of the adapter. The @records ivar is the 'data-store' for this adapter. It is not shared amongst multiple incarnations of this adapter, eg DataMapper.setup(:default, :adapter => :in_memory); DataMapper.setup(:alternate, :adapter => :in_memory) do not share the data-store between them.

@param [String, Symbol] name

    The name of the Repository using this adapter.

@param [String, Hash] uri_or_options

    The connection uri string, or a hash of options to set up
    the adapter

@api semipublic

    # File lib/dm-core/adapters/in_memory_adapter.rb, line 97
    def initialize(name, options = {})
      super
      @records = {}
    end

### Public Instance Methods

create(resources) click to toggle source

Used by DataMapper to put records into a data-store: "INSERT" in SQL-speak. It takes an array of the resources (model instances) to be saved. Resources each have a key that can be used to quickly look them up later without searching, if the adapter supports it.

@param [Enumerable(Resource)] resources

    The set of resources (model instances)

@api semipublic

    # File lib/dm-core/adapters/in_memory_adapter.rb, line 21
    def create(resources)
      records = records_for(resources.first.model)

      resources.each do |resource|
        initialize_serial(resource, records.size.succ)
        records << attributes_as_fields(resource.attributes(nil))
      end
    end

delete(collection) click to toggle source

Destroys all the records matching the given query. "DELETE" in SQL.

@param [DataMapper::Collection] resources

    The collection of resources to delete.

@return [Integer]

    The number of records that were deleted.

@api semipublic

    # File lib/dm-core/adapters/in_memory_adapter.rb, line 70
    def delete(collection)
      records = records_for(collection.model)
      records_to_delete = collection.query.filter_records(records.dup)
      records.replace(records - records_to_delete)
      records_to_delete.size
    end

read(query) click to toggle source

Looks up one record or a collection of records from the data-store: "SELECT" in SQL.

@param [Query] query

    The query to be used to seach for the resources

@return [Array]

    An Array of Hashes containing the key-value pairs for
    each record

@api semipublic

    # File lib/dm-core/adapters/in_memory_adapter.rb, line 41
    def read(query)
      query.filter_records(records_for(query.model).dup)
    end

reset() click to toggle source

TODO consider proper automigrate functionality

    # File lib/dm-core/adapters/in_memory_adapter.rb, line 78
    def reset
      @records = {}
    end

update(attributes, collection) click to toggle source

Used by DataMapper to update the attributes on existing records in a data-store: "UPDATE" in SQL-speak. It takes a hash of the attributes to update with, as well as a collection object that specifies which resources should be updated.

@param [Hash] attributes

    A set of key-value pairs of the attributes to update the resources with.

@param [DataMapper::Collection] resources

    The collection of resources to update.

@api semipublic

    # File lib/dm-core/adapters/in_memory_adapter.rb, line 56
    def update(attributes, collection)
      attributes = attributes_as_fields(attributes)
      read(collection.query).each { |record| record.update(attributes) }.size
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.