Module: Card::Env

Extended by:
LocationHistory
Defined in:
lib/card/env.rb,
lib/card/env/success.rb,
lib/card/env/location.rb,
lib/card/env/location_history.rb

Overview

Card::Env is a module for containing the variable details of the environment in which Card operates.

Env can differ for each request; Card.config should not.

Defined Under Namespace

Modules: Location, LocationHistory Classes: Success

Constant Summary collapse

SERIALIZABLE_ATTRIBUTES =
::Set.new %i[
  main_name params ip ajax html host protocol salt
]

Class Method Summary collapse

Methods included from LocationHistory

discard_locations_for, interrupted_action, location_history, previous_location, save_interrupted_action, save_location, save_location?, url_key_for_location

Class Method Details

.[](key) ⇒ Object



29
30
31
# File 'lib/card/env.rb', line 29

def [] key
  @env[key.to_sym]
end

.[]=(key, value) ⇒ Object



33
34
35
# File 'lib/card/env.rb', line 33

def []= key, value
  @env[key.to_sym] = value
end

.ajax?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/card/env.rb', line 71

def ajax?
  self[:ajax]
end

.html?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/card/env.rb', line 75

def html?
  !self[:controller] || self[:html]
end

.localhost?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/card/env.rb', line 67

def localhost?
  self[:host] && self[:host] =~ /^localhost/
end

.paramsObject



37
38
39
# File 'lib/card/env.rb', line 37

def params
  self[:params] ||= {}
end

.reset(args = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/card/env.rb', line 14

def reset args={}
  @env = { main_name: nil }
  return self unless (c = args[:controller])

  self[:controller] = c
  self[:session]    = c.request.session
  self[:params]     = c.params
  self[:ip]         = c.request.remote_ip
  self[:ajax]       = assign_ajax(c)
  self[:html]       = assign_html(c)
  self[:host]       = assign_host(c)
  self[:protocol]   = assign_protocol(c)
  self
end

.reset_sessionObject



55
56
57
58
59
60
61
# File 'lib/card/env.rb', line 55

def reset_session
  if session.is_a? Hash
    self[:session] = {}
  else
    self[:controller]&.reset_session
  end
end

.serializeObject



79
80
81
# File 'lib/card/env.rb', line 79

def serialize
  @env.select { |k, _v| SERIALIZABLE_ATTRIBUTES.include?(k) }
end

.sessionObject



51
52
53
# File 'lib/card/env.rb', line 51

def session
  self[:session] ||= {}
end

.slot_optsObject



41
42
43
44
45
46
47
48
49
# File 'lib/card/env.rb', line 41

def slot_opts
  # FIXME:  upgrade to safe parameters
  self[:slot_opts] ||= begin
    opts = params[:slot]&.clone || {}
    opts = opts.to_unsafe_h if opts.is_a? ActionController::Parameters
    opts.merge! shortcut_slot_opts
    opts.deep_symbolize_keys.slice(*Card::View::Options.slot_keys)
  end
end

.success(cardname = nil) ⇒ Object



63
64
65
# File 'lib/card/env.rb', line 63

def success cardname=nil
  self[:success] ||= Env::Success.new(cardname, params[:success])
end

.with(serialized_env) ⇒ Object

Parameters:

  • serialized_env (Hash)


84
85
86
87
88
89
90
91
# File 'lib/card/env.rb', line 84

def with serialized_env
  tmp_env = serialize if @env
  @env ||= {}
  @env.update serialized_env
  yield
ensure
  @env.update tmp_env if tmp_env
end