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)


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

def ajax?
  self[:ajax]
end

.html?Boolean

Returns:

  • (Boolean)


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

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

.localhost?Boolean

Returns:

  • (Boolean)


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

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

.serializeObject



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

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

.sessionObject



61
62
63
# File 'lib/card/env.rb', line 61

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

.shortcut_slot_optsObject



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

def shortcut_slot_opts
  opts = {}
  opts[:size] = params[:size].to_sym if params[:size]
  opts[:items] = { view: params[:item].to_sym } if params[:item].present?
  opts
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] || {}
    opts.merge shortcut_slot_opts
    opts = opts.to_unsafe_h if opts.is_a? ActionController::Parameters
    opts.deep_symbolize_keys
  end
end

.success(cardname = nil) ⇒ Object



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

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

.with(serialized_env) ⇒ Object

Parameters:

  • serialized_env (Hash)


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

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