|
| 1 | +class Sequel::Dataset |
| 2 | + def self.===(obj) = obj.is_a?(DProxy) || super |
| 3 | +end |
| 4 | + |
| 5 | +class DProxy |
| 6 | + def initialize(dataset, model, path = [], &block) |
| 7 | + @model, @dataset, @final, @path = model, dataset, block, path |
| 8 | + end |
| 9 | + |
| 10 | + def indent(str) = puts "\t" * @path.size + str |
| 11 | + def method_missing(name, *args, **kwargs, &block) |
| 12 | + all_args = [*args, **kwargs, block:].reject(&:nil?) |
| 13 | + # indent "DProxy::#{@model} #{name}(#{all_args}) => IN" unless QUIET |
| 14 | + |
| 15 | + response = if @final && name.to_sym == :all |
| 16 | + @final.call self, *all_args |
| 17 | + else |
| 18 | + @dataset.send(name, *args, **kwargs, &block) |
| 19 | + end |
| 20 | + # indent "DProxy::#{@model} #{name}(#{all_args}) OUT => #{response.inspect}" unless QUIET |
| 21 | + wrap?(response) ? self.class.new(response, @model, @path + [name => all_args, prev: self], &@final) : response |
| 22 | + end |
| 23 | + |
| 24 | + def wrap?(r) = r.is_a?(Sequel::Dataset::PlaceholderLiteralizer) || r.is_a?(Sequel::Dataset) |
| 25 | + |
| 26 | + def respond_to_missing?(method_name, include_private = false) |
| 27 | + @dataset.respond_to?(method_name, include_private) |
| 28 | + end |
| 29 | + |
| 30 | + alias_method :_clone, :clone |
| 31 | + |
| 32 | + def clone( *args, **kwargs, &block) |
| 33 | + method_missing(:clone, *args, **kwargs, &block) |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | + |
0 commit comments