Module: Card::Env

Extended by:
LocationHistory, RequestAssignments, Serialization, SlotOptions
Defined in:
lib/card/env.rb,
lib/card/env/success.rb,
lib/card/env/location.rb,
lib/card/env/slot_options.rb,
lib/card/env/serialization.rb,
lib/card/env/location_history.rb,
lib/card/env/request_assignments.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, RequestAssignments, Serialization, SlotOptions Classes: Success

Constant Summary

Constants included from Serialization

Serialization::SERIALIZABLE_ATTRIBUTES

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

Methods included from SlotOptions

slot_opts

Methods included from Serialization

serialize, with

Class Method Details

.[](key) ⇒ Object



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

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

.[]=(key, value) ⇒ Object



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

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

.ajax?Boolean

Returns:

  • (Boolean)


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

def ajax?
  self[:ajax]
end

.hash(hashish) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/card/env.rb', line 48

def hash hashish
  case hashish
  when Hash then hashish.clone
  when ActionController::Parameters then hashish.to_unsafe_h
  else {}
  end
end

.html?Boolean

Returns:

  • (Boolean)


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

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

.localhost?Boolean

Returns:

  • (Boolean)


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

def localhost?
  self[:host]&.match?(/^localhost/)
end

.paramsObject



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

def params
  self[:params] ||= {} # .with_indifferent_access
end

.reset(args = {}) ⇒ Object



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

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



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

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

.sessionObject



56
57
58
# File 'lib/card/env.rb', line 56

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

.success(cardname = nil) ⇒ Object



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

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

.with_params(hash) ⇒ Object



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

def with_params hash
  old_params = params.clone
  params.merge! hash
  yield
ensure
  self[:params] = old_params
end