Permalink
Browse files

First commit

  • Loading branch information...
1 parent a7bef5b commit eedcaeb31e74d7f9d7000038d36d78a0a6b43ec0 Paul Sadauskas committed Aug 1, 2008
Showing with 113 additions and 15 deletions.
  1. +94 −0 lib/active_resource_adapter.rb
  2. +19 −15 test.rb
@@ -0,0 +1,94 @@
+require 'rubygems'
+require 'resourceful'
+require 'rexml/document'
+gem 'dm-core', '>=0.9.4'
+
+require 'pathname'
+
+module DataMapper
+ module Adapters
+
+ class HttpAdapter < AbstractAdapter
+ attr_reader :http
+
+ def initialize(name, uri_or_options)
+ super
+
+ @http = Resourceful::HttpAccessor.new
+ @http.cache_manager = Resourceful::InMemoryCacheManager.new
+ @http.logger = Resourceful::StdOutLogger.new
+ end
+ end
+
+ class ActiveResourceAdapter < HttpAdapter
+ MIME_TYPE = 'application/xml'
+
+ def initialize(name, uri_or_options)
+ super
+
+ @site_uri = uri_or_options[:site]
+ end
+
+ def read_many(query)
+ unless query.model.collection_resource
+ collection_uri = @site_uri + query.model.storage_name(query.repository.name)
+ query.model.collection_resource = @http.resource(collection_uri, :accept => MIME_TYPE)
+ end
+
+ doc = REXML::Document.new(query.model.collection_resource.get.body)
+
+ Collection.new(query) do |collection|
+ doc.elements.each('people/person') do |item|
+ collection.load(
+ query.fields.map { |prop| item.elements[prop.field.to_s.gsub('_', '-')].text }
+ )
+ end
+ end
+ end
+
+ def read_one(query)
+ uri = query.model.collection_resource.uri + '/' + query.conditions.first[2].to_s
+
+ resource = @http.resource(uri, :accept => MIME_TYPE)
+
+ doc = REXML::Document.new(resource.get.body)
+
+ query.model.load(query.fields.map { |prop| doc.root.elements[prop.field.to_s.gsub('_', '-')].text }, query)
+ end
+
+ require 'pp'
+ require 'active_support'
+ def create(resources)
+ resources.each do |resource|
+ doc = resource.attributes.to_xml({:root => Extlib::Inflection.underscore(resource.model.to_s)})
+
+ result = resource.model.collection_resource.post(doc, :content_type => MIME_TYPE)
+ if result.was_successful?
+ resource.model.key(resource.model.repository.name).first.set!(resource, result.header['Location'].first.split('/').last)
+ end
+ end
+ 1
+ end
+
+ def update(attributes, query)
+ doc = attributes.to_xml({:root => query.model.to_s.downcase})
+
+ uri = query.model.collection_resource.uri + '/' + query.conditions.first[2].to_s
+ resource = @http.resource(uri, :accept => MIME_TYPE)
+ result = resource.put(doc, :content_type => MIME_TYPE)
+ end
+
+ def delete(query)
+
+ end
+
+ end
+
+ end
+
+ module Model
+ attr_accessor :collection_resource
+
+ end
+end
+
View
@@ -2,28 +2,32 @@
require 'dm-core'
require 'dm-types'
require 'pathname'
-require 'uuidtools'
-require Pathname(__FILE__).dirname + 'lib/ssbe_adapter'
+require Pathname(__FILE__).dirname + 'lib/active_resource_adapter'
-DataMapper.setup(:default, :adapter => :ssbe, :service_descriptor_uri => 'http://core.ssbe.localhost/service_descriptors')
+DataMapper.setup(:default, :adapter => :active_resource, :site => "http://localhost:3000/")
-class Client
+class Person
include DataMapper::Resource
- set_service_type :kernel
- set_resource_name "AllClients"
- set_parser DataMapper::Adapters::SsbeAdapter::SSJParser.new
+ property :id, Integer, :key => true
property :name, String
- property :href, URI, :key => true
- property :id, UUID
- property :longname, String
- property :active, Boolean
+ property :created_at, DateTime
+ property :updated_at, DateTime
end
-clients = Client.all
-puts clients.inspect
+people = Person.all
+puts people.inspect
+
+person = Person.get(1)
+puts person.inspect
+
+new_person = Person.new(:name => 'Eric')
+new_person.save
+puts new_person.inspect
+
+new_person.name = "Erik"
+new_person.save
+puts new_person.inspect
-client = Client.get(clients.first.href)
-puts client.inspect

0 comments on commit eedcaeb

Please sign in to comment.