Class: Waw::FullState::Variable

Inherits:
Object
  • Object
show all
Includes:
ScopeUtils
Defined in:
lib/waw/fullstate/variable.rb

Instance Method Summary collapse

Methods included from ScopeUtils

#config, #find_kernel_context, #logger, #params, #rack_env, #real_session, #request, #resources, #response, #root_folder, #session

Constructor Details

#initialize(name, default_value = nil, &block) ⇒ Variable

Creates a variable instance with a default value

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/waw/fullstate/variable.rb', line 7

def initialize(name, default_value = nil, &block)
  raise ArgumentError, "Waw variables accept a default value or a block, but not both"\
    unless default_value.nil? or block.nil?
  @name, @default_value = name, (block ? block : default_value)
end

Instance Method Details

#resetObject

Resets to the default value



14
15
16
# File 'lib/waw/fullstate/variable.rb', line 14

def reset
  session.unset(@name)
end

#value(*args) ⇒ Object

Returns the current value of the variable



19
20
21
22
23
24
25
26
27
# File 'lib/waw/fullstate/variable.rb', line 19

def value(*args)
  if session.has_key?(@name)
    session.get(@name)
  elsif @default_value.is_a?(Proc)
    @default_value.call(*args)
  else
    @default_value
  end
end

#value=(val) ⇒ Object

Sets the value of the variable



30
31
32
# File 'lib/waw/fullstate/variable.rb', line 30

def value=(val)
  session.set(@name, val)
end