lib/rom/http/dataset.rb in rom-http-0.1.1 vs lib/rom/http/dataset.rb in rom-http-0.1.2
- old
+ new
@@ -1,14 +1,17 @@
+require 'rom/http/support/transformations'
+
module ROM
module HTTP
class Dataset
include Enumerable
include Equalizer.new(:config, :options)
include ROM::Options
attr_reader :config
+ option :projections, type: ::Array, default: [], reader: true
option :request_method, type: ::Symbol, default: :get, reader: true
option :path, type: ::String, default: ''
option :params, type: ::Hash, default: {}, reader: true
option :headers, type: ::Hash, default: {}
@@ -59,10 +62,18 @@
def with_options(opts)
__new__(config, options.merge(opts))
end
+ def project(*args)
+ projections = args.first.is_a?(::Array) ? args.first : args
+
+ with_options(
+ projections: (self.projections + projections)
+ )
+ end
+
def with_path(path)
with_options(path: path)
end
def append_path(path)
@@ -101,11 +112,13 @@
request_method: :delete
).response
end
def response
- response_handler.call(request_handler.call(self), self)
+ response_transformer.call(
+ response_handler.call(request_handler.call(self), self)
+ )
end
private
def response_handler
@@ -124,9 +137,17 @@
self.class.default_response_handler
end
def default_request_handler
self.class.default_request_handler
+ end
+
+ def response_transformer
+ if projections.empty?
+ ROM::HTTP::Support::Transformations[:noop]
+ else
+ ROM::HTTP::Support::Transformations[:project, projections]
+ end
end
def __new__(*args, &block)
self.class.new(*args, &block)
end