Class: DataMapper::Adapters::PersevereAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/persevere_adapter.rb

Instance Method Summary collapse

Instance Method Details

#create(resources) ⇒ Integer

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.

Parameters:

  • resources (Array<DataMapper::Resource>)

    The set of resources (model instances)

Returns:

  • (Integer)

    The number of records that were actually saved into the data-store



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
# File 'lib/persevere_adapter.rb', line 25

def create(resources)
  created = 0
  resources.each do |resource|
    #
    # This isn't the best solution but for an adapter, it'd be nice
    # to support objects being in *tables* instead of in one big icky
    # sort of table.
    #
    tblname = Extlib::Inflection.classify(resource.class).pluralize

    if ! @classes.include?(tblname)
      payload = {
        'id' => tblname,
        'extends' => { "$ref" => "/Class/Object" }
      }

      response = @persevere.create("/Class/", payload)
    end

    path = "/#{tblname}/"
    payload = resource.attributes
    payload.delete(:id)

    response = @persevere.create(path, payload)

    # Check the response, this needs to be more robust and raise
    # exceptions when there's a problem

    if response.code == "201"# good:
      rsrc_hash = JSON.parse(response.body)
      # Typecast attributes, DM expects them properly cast
      resource.model.properties.each do |prop|
        value = rsrc_hash[prop.field.to_s]
        if !value.nil?
          rsrc_hash[prop.field.to_s] = prop.typecast(value)
        end
      end

      resource.id = rsrc_hash["id"]

      created += 1
    else
      return false
    end
  end

  # Return the number of resources created in persevere.
  return created
end

#delete(query) ⇒ Integer

Destroys all the records matching the given query. “DELETE” in SQL.

Parameters:

  • query (DataMapper::Query)

    The query used to locate the resources to be deleted.

Returns:

  • (Integer)

    The number of records that were deleted.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/persevere_adapter.rb', line 189

def delete(query)
  deleted = 0

  if ! query.is_a?(DataMapper::Query)
    resources = [query].flatten
  else
    resources = read_many(query)
  end

  resources.each do |resource|
    tblname = Extlib::Inflection.classify(resource.class).pluralize
    path = "/#{tblname}/#{resource.id}"

    result = @persevere.delete(path)

    if result # ok
      deleted += 1
    end
  end
  return deleted
end

#read_many(query) ⇒ DataMapper::Collection Also known as: read

Looks up a collection of records from the data-store: “SELECT” in SQL. Used by Model#all to search for a set of records; that set is in a DataMapper::Collection object.

Parameters:

  • query (DataMapper::Query)

    The query to be used to seach for the resources

Returns:

  • (DataMapper::Collection)

    A collection of all the resources found by the query.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/persevere_adapter.rb', line 148

def read_many(query)
  resources = Array.new
  json_query = make_json_query(query)

  tblname = Extlib::Inflection.classify(query.model).pluralize
  path = "/#{tblname}/#{json_query}"

  response = @persevere.retrieve(path)

  if response.code == "200"
    results = JSON.parse(response.body)
    results.each do |rsrc_hash|
      # Typecast attributes, DM expects them properly cast
      query.model.properties.each do |prop|
        value = rsrc_hash[prop.field.to_s]
        if !value.nil?
          rsrc_hash[prop.field.to_s] = prop.typecast(value)
        end
      end
    end

    resources = query.model.load(results, query)
  else
    return false
  end

  query.filter_records(resources)
end

#read_one(query) ⇒ DataMapper::Resource

Look up a single record from the data-store. “SELECT … LIMIT 1” in SQL. Used by Model#get to find a record by its identifier(s), and Model#first to find a single record by some search query.

Parameters:

  • query (DataMapper::Query)

    The query to be used to locate the resource.

Returns:

  • (DataMapper::Resource)

    A Resource object representing the record that was found, or nil for no matching records.



131
132
133
134
# File 'lib/persevere_adapter.rb', line 131

def read_one(query)
  results = read_many(query)
  results[0,1]
end

#update(attributes, query) ⇒ Integer

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 query object that specifies which resources should be updated.

Parameters:

  • attributes (Hash)

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

  • query (DataMapper::Query)

    The query that should be used to find the resource(s) to update.

Returns:

  • (Integer)

    the number of records that were successfully updated



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/persevere_adapter.rb', line 92

def update(attributes, query)
  updated = 0

  if ! query.is_a?(DataMapper::Query)
    resources = [query].flatten
  else
    resources = read_many(query)
  end

  resources.each do |resource|
    tblname = Extlib::Inflection.classify(resource.class).pluralize
    path = "/#{tblname}/#{resource.id}"

    result = @persevere.update(path, resource.attributes)

    if result # good:
      updated += 1
    else
      return false
    end
  end
  return updated
end