lib/persevere_adapter.rb in dm-persevere-adapter-0.48.2 vs lib/persevere_adapter.rb in dm-persevere-adapter-0.50.0

- old
+ new

@@ -189,11 +189,11 @@ module Adapters class PersevereAdapter < AbstractAdapter extend Chainable extend Deprecate - RESERVED_CLASSNAMES = ['User','Transaction','Capability','File','Class', 'Object'] + RESERVED_CLASSNAMES = ['User','Transaction','Capability','File','Class', 'Object', 'Versioned'] include Migrations::PersevereAdapter # Default types for all data object based adapters. # @@ -475,10 +475,11 @@ else puts "You need an id key/value in the hash" end end schema_hash['properties'].delete('id') if schema_hash['properties'].has_key?('id') + schema_hash['extends'] = { "$ref" => "/Class/Versioned" } if @options[:versioned] result = @persevere.create(path, schema_hash) if result.code == '201' return JSON.parse(result.body) else return false @@ -553,11 +554,11 @@ @options = Hash.new uri_or_options.each do |k,v| @options[k.to_sym] = v end - + @options[:scheme] = @options[:adapter] @options.delete(:scheme) @resource_naming_convention = NamingConventions::Resource::Underscored @identity_maps = {} @@ -565,11 +566,11 @@ @persevere = nil @prepped = false connect end - + def connect if ! @prepped uri = URI::HTTP.build(@options).to_s @persevere = Persevere.new(uri) prep_persvr unless @prepped @@ -590,11 +591,51 @@ end @prepped = true else puts "Error retrieving existing tables: ", result end + + # + # If the user specified a versioned datastore load the versioning REST code + # + if ! @classes.include?("Versioned") && @options[:versioned] + json_contents = <<-EOF + { + id:"Versioned", + prototype: { + getVersionMethod: function() { + return java.lang.Class.forName("org.persvr.data.Persistable").getMethod("getVersion"); + }, + isCurrentVersion: function() { return this.getVersionMethod.invoke(this).isCurrent(); }, + getVersion: function() { return this.getVersionMethod.invoke(this).getVersionNumber(); }, + getPreviousVersion: function() { + var prev = getVersionMethod.invoke(this).getPreviousVersion(); + return prev; + }, + "representation:application/json+versioned": { + quality:0.2, + output:function(object) { + var prev = object.getPreviousVersion(); + response.setContentType("application/json+versioned"); + response.getOutputStream().print(serialize({ + version: object.getVersion(), + "parent-versions": prev ? [prev] : [], + content: object + })); + } + } + } + } + EOF + begin + response = @persevere.persevere.send_request('POST', URI.encode('/Class/'), json_contents, { 'Content-Type' => 'application/javascript' } ) + rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, + Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e + puts "Persevere Create Failed: #{e}, Trying again." + end + end end - + ## # Convert a DataMapper Resource to a JSON. # # @param [Query] query # The DataMapper query object passed in \ No newline at end of file