Open thedrow opened 8 years ago
Sorry, just looked into this and it seems that the dataset
no longer infers register_as
, you don't need a base view
unless you're using rom-repository
, try updating your relation to use register_as
:
class Users < ROM::Relation[:my_adapter]
dataset :users
register_as :users
def by_id(id)
with_path(id.to_s)
end
end
Full example:
require 'json'
require 'uri'
require 'net/http'
gemfile(true) do
gem 'rom'
gem 'rom-http'
end
module ROM
module MyAdapter
class Dataset < ROM::HTTP::Dataset
default_request_handler ->(dataset) do
uri = URI(dataset.uri)
uri.path = "/#{dataset.name}/#{dataset.path}"
uri.query = URI.encode_www_form(dataset.params)
http = Net::HTTP.new(uri.host, uri.port)
request_klass = Net::HTTP.const_get(ROM::Inflector.classify(dataset.request_method))
request = request_klass.new(uri.request_uri)
dataset.headers.each_with_object(request) do |(header, value), request|
request[header.to_s] = value
end
response = http.request(request)
end
default_response_handler ->(response, dataset) do
Array([JSON.parse(response.body)]).flatten
end
end
class Gateway < ROM::HTTP::Gateway; end
class Relation < ROM::HTTP::Relation
adapter :my_adapter
end
module Commands
class Create < ROM::HTTP::Commands::Create
adapter :my_adapter
end
class Update < ROM::HTTP::Commands::Update
adapter :my_adapter
end
class Delete < ROM::HTTP::Commands::Delete
adapter :my_adapter
end
end
end
end
ROM.register_adapter(:my_adapter, ROM::MyAdapter)
configuration = ROM::Configuration.new(:my_adapter, {
uri: 'http://jsonplaceholder.typicode.com',
headers: {
Accept: 'application/json'
}
})
class Users < ROM::Relation[:my_adapter]
dataset :users
register_as :users
def by_id(id)
with_path(id.to_s)
end
end
configuration.register_relation(Users)
container = ROM.container(configuration)
container.relation(:users).by_id(1).to_a
I am using rom-repository.
Here's a working example with rom-repository:
require 'json'
require 'uri'
require 'net/http'
gemfile(true) do
gem 'rom'
gem 'rom-http'
gem 'rom-repository'
end
module ROM
module MyAdapter
class Dataset < ROM::HTTP::Dataset
default_request_handler ->(dataset) do
uri = URI(dataset.uri)
uri.path = "/#{dataset.name}/#{dataset.path}"
uri.query = URI.encode_www_form(dataset.params)
http = Net::HTTP.new(uri.host, uri.port)
request_klass = Net::HTTP.const_get(ROM::Inflector.classify(dataset.request_method))
request = request_klass.new(uri.request_uri)
dataset.headers.each_with_object(request) do |(header, value), request|
request[header.to_s] = value
end
response = http.request(request)
end
default_response_handler ->(response, dataset) do
Array([JSON.parse(response.body, symbolize_names: true)]).flatten
end
end
class Gateway < ROM::HTTP::Gateway; end
class Relation < ROM::HTTP::Relation
adapter :my_adapter
end
module Commands
class Create < ROM::HTTP::Commands::Create
adapter :my_adapter
end
class Update < ROM::HTTP::Commands::Update
adapter :my_adapter
end
class Delete < ROM::HTTP::Commands::Delete
adapter :my_adapter
end
end
end
end
ROM.register_adapter(:my_adapter, ROM::MyAdapter)
configuration = ROM::Configuration.new(:my_adapter, {
uri: 'http://jsonplaceholder.typicode.com',
headers: {
Accept: 'application/json'
}
})
class Users < ROM::Relation[:my_adapter]
dataset :users
register_as :users
schema do
attribute :id, ROM::Types::Int
attribute :name, ROM::Types::String
attribute :username, ROM::Types::String
attribute :email, ROM::Types::String
attribute :phone, ROM::Types::String
attribute :website, ROM::Types::String
end
view(:base, %i(id name username email phone website)) do
self
end
def by_id(id)
with_path(id.to_s)
end
end
configuration.register_relation(Users)
container = ROM.container(configuration)
class UserRepository < ROM::Repository[:users]
def find(id)
users.by_id(id).one!
end
end
user_repo = UserRepository.new(container)
user_repo.find(1)
# => #<ROM::Struct[User] id=1 name="Leanne Graham" username="Bret" email="Sincere@april.biz" phone="1-770-736-8031 x56442" website="hildegard.org">
Closing due to inactivity
Why are you closing this issue? Has the documentation been updated and extended?
Fair point
I have the following adapter code:
and I'm getting to following exception:
After digging a bit it seems I need to set the option base to be an instance but I'm not sure of which class. So I did:
and now I get:
After adding fetch to the Relation class I get:
It's really not clear what I should be doing. Can you guys please help me and also document the process?