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



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

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

.[]=(key, value) ⇒ Object



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

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

.ajax?Boolean

Returns:

  • (Boolean)


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

def ajax?
  self[:ajax]
end

.html?Boolean

Returns:

  • (Boolean)


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

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

.localhost?Boolean

Returns:

  • (Boolean)


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

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

.paramsObject



40
41
42
# File 'lib/card/env.rb', line 40

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

.reset(args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/card/env.rb', line 17

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



58
59
60
61
62
63
64
# File 'lib/card/env.rb', line 58

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

.serializeObject



82
83
84
# File 'lib/card/env.rb', line 82

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

.sessionObject



54
55
56
# File 'lib/card/env.rb', line 54

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

.slot_optsObject



44
45
46
47
48
49
50
51
52
# File 'lib/card/env.rb', line 44

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



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

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

.with(serialized_env) ⇒ Object

Parameters:

  • serialized_env (Hash)


87
88
89
90
91
92
93
94
# File 'lib/card/env.rb', line 87

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