Class: Arel::Middleware::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/arel/middleware/chain.rb

Constant Summary collapse

MAX_RECURSION_DEPTH =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(internal_middleware = [], internal_context = {}, executor_class = Arel::Middleware::DatabaseExecutor, cache: nil) ⇒ Chain

Returns a new instance of Chain.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/arel/middleware/chain.rb', line 13

def initialize(
  internal_middleware = [],
  internal_context = {},
  executor_class = Arel::Middleware::DatabaseExecutor,
  cache: nil
)
  @internal_middleware = internal_middleware
  @internal_context = internal_context
  @executor = executor_class.new(internal_middleware)
  @executing_middleware_depth = 0
  @cache = cache || NoOpCache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



9
10
11
# File 'lib/arel/middleware/chain.rb', line 9

def cache
  @cache
end

#executing_middleware_depthObject (readonly)

Returns the value of attribute executing_middleware_depth.



7
8
9
# File 'lib/arel/middleware/chain.rb', line 7

def executing_middleware_depth
  @executing_middleware_depth
end

#executorObject (readonly)

Returns the value of attribute executor.



8
9
10
# File 'lib/arel/middleware/chain.rb', line 8

def executor
  @executor
end

Instance Method Details

#append(new_middleware, cache: @cache, &block) ⇒ Object



84
85
86
87
88
# File 'lib/arel/middleware/chain.rb', line 84

def append(new_middleware, cache: @cache, &block)
  new_middleware = Array.wrap(new_middleware)
  updated_middleware = internal_middleware + new_middleware
  continue_chain(updated_middleware, internal_context, cache: cache, &block)
end

#apply(middleware, cache: @cache, &block) ⇒ Object Also known as: only



48
49
50
51
# File 'lib/arel/middleware/chain.rb', line 48

def apply(middleware, cache: @cache, &block)
  new_middleware = Array.wrap(middleware)
  continue_chain(new_middleware, internal_context, cache: cache, &block)
end

#cache_accessorObject



26
27
28
# File 'lib/arel/middleware/chain.rb', line 26

def cache_accessor
  @cache_accessor ||= CacheAccessor.new @cache
end

#context(new_context = nil, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/arel/middleware/chain.rb', line 90

def context(new_context = nil, &block)
  if new_context.nil? && !block.nil?
    raise 'You cannot do a block statement while calling context without arguments'
  end

  return internal_context if new_context.nil?

  continue_chain(internal_middleware, new_context, cache: @cache, &block)
end

#currentObject



44
45
46
# File 'lib/arel/middleware/chain.rb', line 44

def current
  internal_middleware.dup
end

#except(without_middleware, cache: @cache, &block) ⇒ Object



58
59
60
61
62
# File 'lib/arel/middleware/chain.rb', line 58

def except(without_middleware, cache: @cache, &block)
  without_middleware = Array.wrap(without_middleware)
  new_middleware = internal_middleware - without_middleware
  continue_chain(new_middleware, internal_context, cache: cache, &block)
end

#execute(sql, binds = [], &execute_sql) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/arel/middleware/chain.rb', line 30

def execute(sql, binds = [], &execute_sql)
  return execute_sql.call(sql, binds).to_casted_result if internal_middleware.length.zero?

  if (cached_sql = cache_accessor.read(sql))
    return execute_sql.call(cached_sql, binds).to_casted_result
  end

  execute_with_middleware(sql, binds, execute_sql).to_casted_result
rescue ::PgQuery::ParseError
  execute_sql.call(sql, binds)
ensure
  @executing_middleware_depth -= 1
end

#insert_after(new_middleware, existing_middleware, cache: @cache, &block) ⇒ Object



77
78
79
80
81
82
# File 'lib/arel/middleware/chain.rb', line 77

def insert_after(new_middleware, existing_middleware, cache: @cache, &block)
  new_middleware = Array.wrap(new_middleware)
  index = internal_middleware.index(existing_middleware)
  updated_middleware = internal_middleware.insert(index + 1, *new_middleware)
  continue_chain(updated_middleware, internal_context, cache: cache, &block)
end

#insert_before(new_middleware, existing_middleware, cache: @cache, &block) ⇒ Object



64
65
66
67
68
69
# File 'lib/arel/middleware/chain.rb', line 64

def insert_before(new_middleware, existing_middleware, cache: @cache, &block)
  new_middleware = Array.wrap(new_middleware)
  index = internal_middleware.index(existing_middleware)
  updated_middleware = internal_middleware.insert(index, *new_middleware)
  continue_chain(updated_middleware, internal_context, cache: cache, &block)
end

#none(&block) ⇒ Object



54
55
56
# File 'lib/arel/middleware/chain.rb', line 54

def none(&block)
  continue_chain([], internal_context, cache: cache, &block)
end

#prepend(new_middleware, cache: @cache, &block) ⇒ Object



71
72
73
74
75
# File 'lib/arel/middleware/chain.rb', line 71

def prepend(new_middleware, cache: @cache, &block)
  new_middleware = Array.wrap(new_middleware)
  updated_middleware = new_middleware + internal_middleware
  continue_chain(updated_middleware, internal_context, cache: cache, &block)
end

#to_sql(type, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/arel/middleware/chain.rb', line 100

def to_sql(type, &block)
  middleware = Arel::Middleware::ToSqlMiddleware.new(type)

  new_chain = Arel::Middleware::Chain.new(
    internal_middleware + [middleware],
    internal_context,
    Arel::Middleware::ToSqlExecutor,
  )

  maybe_execute_block(new_chain, &block)

  middleware.sql
end