Class: Locomotive::Steam::Middlewares::ThreadSafe

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/middlewares/thread_safe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 6

def env
  @env
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 8

def call(env)
  threadsafed = dup
  threadsafed.env = env

  threadsafed._call # thread-safe purpose

  threadsafed.next
end

#decorate_entry(entry) ⇒ Object



86
87
88
89
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 86

def decorate_entry(entry)
  return nil if entry.nil?
  Locomotive::Steam::Decorators::I18nDecorator.new(entry, locale, default_locale)
end

#default_liquid_contextObject



91
92
93
94
95
96
97
98
99
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 91

def default_liquid_context
  ::Liquid::Context.new({ 'site' => site.to_liquid }, {}, {
    request:        request,
    locale:         locale,
    site:           site,
    services:       services,
    repositories:   services.repositories
  }, true)
end

#default_localeObject



56
57
58
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 56

def default_locale
  site.default_locale
end

#liquid_assignsObject



78
79
80
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 78

def liquid_assigns
  @liquid_assigns ||= env.fetch('steam.liquid_assigns')
end

#live_editing?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 82

def live_editing?
  !!env['steam.live_editing']
end

#localeObject



48
49
50
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 48

def locale
  @locale ||= env.fetch('steam.locale')
end

#localesObject



52
53
54
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 52

def locales
  site.locales
end

#merge_with_params(values) ⇒ Object



69
70
71
72
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 69

def merge_with_params(values)
  values.each { |name, value| self.request.params[name] = value }
  @params = nil
end

#nextObject



17
18
19
20
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 17

def next
  # avoid to be called twice
  @next_response || (@next_response = app.call(env))
end

#pageObject



40
41
42
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 40

def page
  @page ||= env.fetch('steam.page')
end

#paramsObject



60
61
62
63
64
65
66
67
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 60

def params
  @params ||= if request.content_type&.start_with?('application/json') && (request.post? || request.put?)
    request.body.rewind
    JSON.parse(request.body.read).with_indifferent_access
  else
    request.params.with_indifferent_access
  end
end

#pathObject



44
45
46
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 44

def path
  @path ||= env.fetch('steam.path')
end

#repositoriesObject



28
29
30
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 28

def repositories
  @repositories ||= services.repositories
end

#requestObject



32
33
34
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 32

def request
  @request ||= env.fetch('steam.request')
end

#servicesObject

Shortcuts =



24
25
26
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 24

def services
  @services ||= env.fetch('steam.services')
end

#sessionObject



74
75
76
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 74

def session
  env['rack.session']
end

#siteObject



36
37
38
# File 'lib/locomotive/steam/middlewares/thread_safe.rb', line 36

def site
  @site ||= env.fetch('steam.site')
end