lib/persevere_adapter.rb in dm-persevere-adapter-0.16.0 vs lib/persevere_adapter.rb in dm-persevere-adapter-0.17.0

- old
+ new

@@ -1,9 +1,10 @@ require 'rubygems' require 'dm-core' require 'extlib' require 'json' + require 'persevere' module DataMapper module Adapters class PersevereAdapter < AbstractAdapter @@ -24,26 +25,19 @@ # @api semipublic def create(resources) connect if @persevere.nil? created = 0 resources.each do |resource| + serial = resource.model.serial(self.name) + # # 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 + tblname = resource.model.storage_name - 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) @@ -59,11 +53,11 @@ if !value.nil? rsrc_hash[prop.field.to_s] = prop.typecast(value) end end - resource.id = rsrc_hash["id"] + serial.set!(resource, rsrc_hash["id"]) unless serial.nil? created += 1 else return false end @@ -99,16 +93,16 @@ else resources = read_many(query) end resources.each do |resource| - tblname = Extlib::Inflection.classify(resource.class).pluralize + tblname = resource.model.storage_name path = "/#{tblname}/#{resource.id}" result = @persevere.update(path, resource.attributes) - if result # good: + if result.code == "200" updated += 1 else return false end end @@ -151,11 +145,11 @@ connect if @persevere.nil? resources = Array.new json_query = make_json_query(query) - tblname = Extlib::Inflection.classify(query.model) + tblname = query.model.storage_name path = "/#{tblname}/#{json_query}" response = @persevere.retrieve(path) if response.code == "200" @@ -197,17 +191,19 @@ resources = [query].flatten else resources = read_many(query) end + puts resources.inspect + resources.each do |resource| - tblname = Extlib::Inflection.classify(resource.class).pluralize + tblname = resource.model.storage_name path = "/#{tblname}/#{resource.id}" result = @persevere.delete(path) - if result # ok + if result.code == "204" # ok deleted += 1 end end return deleted end @@ -236,10 +232,32 @@ else return nil end end + def put_schema(schema_hash, project = nil) + path = "/Class/" + + if ! project.nil? + if schema_hash.has_key?("id") + if ! schema_hash['id'].index(project) + schema_hash['id'] = "#{project}/#{schema_hash['id']}" + end + else + puts "You need an id key/value in the hash" + end + end + + response = @persevere.create(path, schema_hash) + + if response.code == "201" + return JSON.parse(response.body) + else + return nil + end + end + private ## # Make a new instance of the adapter. The @model_records ivar is # the 'data-store' for this adapter. It is not shared amongst @@ -343,18 +361,18 @@ else value = "#{bind_value}" end query_terms << case operator - when :eql then "#{property.field()}=#{value}" - when :lt then "#{property.field()}<#{value}" - when :gt then "#{property.field()}>#{value}" - when :lte then "#{property.field()}<=#{value}" - when :gte then "#{property.field()}=>#{value}" - when :not then "#{property.field()}!=#{value}" - when :like then "#{property.field()}~'*#{value}*'" - else puts "Unknown condition: #{operator}" - end + when :eql then "#{property.field()}=#{value}" + when :lt then "#{property.field()}<#{value}" + when :gt then "#{property.field()}>#{value}" + when :lte then "#{property.field()}<=#{value}" + when :gte then "#{property.field()}=>#{value}" + when :not then "#{property.field()}!=#{value}" + when :like then "#{property.field()}~'*#{value}*'" + else puts "Unknown condition: #{operator}" + end end end if query_terms.length != 0 query = "?#{query_terms.join("&")}"