github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

paul / dm-core forked from datamapper/dm-core

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 2
    • 66
  • Source
  • Commits
  • Network (66)
  • Issues (0)
  • Downloads (11)
  • Wiki (1)
  • Graphs
  • Tree: 27a0277

click here to add a description

click here to add a homepage

  • Branches (5)
    • better_setup
    • do-more
    • master
    • next
    • types
  • Tags (11)
    • 0.9.10
    • 0.9.9
    • 0.9.8
    • 0.9.7
    • 0.9.6
    • 0.9.5
    • 0.9.4
    • 0.9.3
    • 0.9.2
    • 0.9.1
    • 0.9.0.1
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

DataMapper - Core — Read more

  cancel

http://datamapper.org/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Remove a flag I used to disable some of the specs 
paul (author)
Mon Mar 30 14:19:11 -0700 2009
commit  27a0277c8b00aa9d5be67a25a4113c437e4a6b34
tree    fabaa9f5dbd7ea3fcf9b0568a1b5f4bb71ebbbd8
parent  b8aa088659f1438c4093b231eb9f4d339312f7cf
dm-core / lib / dm-core / adapters / in_memory_adapter.rb lib/dm-core/adapters/in_memory_adapter.rb
100644 127 lines (113 sloc) 4.392 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module DataMapper
  module Adapters
    # 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.
    class InMemoryAdapter < AbstractAdapter
      ##
      # Make a new instance of the adapter. The @model_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
      def initialize(name, uri_or_options)
        super
        @records = {}
      end
 
      ##
      # 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
      def create(resources)
        records = records_for(resources.first.model)
 
        resources.each do |resource|
          initialize_identity_field(resource, records.size.succ)
          records[resource.key] = resource.attributes(:field)
        end
      end
 
      ##
      # 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
      def read(query)
        model = query.model
 
        records = records_for(model)
        filter_records(records.values, query)
      end
 
      ##
      # 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 query that should be used to find the resource(s) to update.
      #
      # @api semipublic
      def update(attributes, collection)
        model = collection.model
        query = collection.query
 
        records = records_for(model)
        attributes = attributes_as_fields(attributes)
 
        updated = filter_records(records.values, query)
        updated.each { |r| r.update(attributes) }
      end
 
      ##
      # Destroys all the records matching the given query. "DELETE" in SQL.
      #
      # @param [Query] query
      # The query used to locate the resources to be deleted.
      #
      # @return [Integer]
      # The number of records that were deleted.
      #
      # @api semipublic
      def delete(collection)
        model = collection.model
        query = collection.query
 
        records = records_for(model)
        records_to_delete = filter_records(records.values, query).to_set
        records.delete_if { |_k,r| records_to_delete.include?(r) }
        records_to_delete
      end
 
      private
 
      ##
      # All the records we're storing. This method will look them up by model name
      #
      # @api private
      def records_for(model)
        @records[model] ||= {}
      end
 
    end # class InMemoryAdapter
 
    const_added(:InMemoryAdapter)
  end # module Adapters
end # module DataMapper
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server