Class: Julewire::Rails::ContextBodyProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/julewire/rails/context_body_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(body, handle:, on_close:) ⇒ ContextBodyProxy

Returns a new instance of ContextBodyProxy.



6
7
8
9
10
11
# File 'lib/julewire/rails/context_body_proxy.rb', line 6

def initialize(body, handle:, on_close:)
  @body = body
  @handle = handle
  @on_close = on_close
  @closed = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/julewire/rails/context_body_proxy.rb', line 38

def method_missing(method_name, ...)
  case method_name
  when :to_str
    super
  when :to_ary
    begin
      @handle.with_context { @body.public_send(method_name, ...) }
    ensure
      close
    end
  else
    @handle.with_context { @body.public_send(method_name, ...) }
  end
end

Instance Method Details

#closeObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/julewire/rails/context_body_proxy.rb', line 21

def close
  return if @closed

  @closed = true
  begin
    @handle.with_context { @body.close if @body.respond_to?(:close) }
  ensure
    @on_close.call
  end
end

#closed?Boolean

Returns:

  • (Boolean)


32
# File 'lib/julewire/rails/context_body_proxy.rb', line 32

def closed? = @closed

#each(&block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/julewire/rails/context_body_proxy.rb', line 13

def each(&block)
  return enum_for(:each) unless block_given?

  @handle.with_context do
    @body.each { block.yield(it) }
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/julewire/rails/context_body_proxy.rb', line 34

def respond_to_missing?(method_name, include_private = false)
  (method_name != :to_str && @body.respond_to?(method_name, include_private)) || super
end