Class: Crisp::ChainedEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/crisp/chained_env.rb

Overview

The ChainedEnv combines to given environments. This is useful to combine a global and a local environment for easy access

Instance Method Summary collapse

Constructor Details

#initialize(first, second) ⇒ ChainedEnv

creates new chained env with given envs It is used that the ‘first’ is the local and the ‘second’ the global env



8
9
10
11
# File 'lib/crisp/chained_env.rb', line 8

def initialize(first, second)
  @first = first
  @second = second
end

Instance Method Details

#[](key) ⇒ Object

Read access is performed preferred on the ‘first’ env. If the ‘first’ env is not holding a value for the key, the ‘second’ will be requested.



20
21
22
23
24
25
26
# File 'lib/crisp/chained_env.rb', line 20

def [](key)
  if @first.has_key?(key)
    @first[key]
  else
    @second[key]
  end
end

#[]=(key, val) ⇒ Object

Storing a value for a key will always be performed on the ‘second’ env.



29
30
31
# File 'lib/crisp/chained_env.rb', line 29

def []=(key, val)
  @second[key] = val
end

#alias(to, from) ⇒ Object

set alias to global/second env



44
45
46
# File 'lib/crisp/chained_env.rb', line 44

def alias(to, from)
  @second.alias(to, from)
end

#global_loop_dataObject

returns global loop data of global/second env



34
35
36
# File 'lib/crisp/chained_env.rb', line 34

def global_loop_data
  @second.global_loop_data
end

#global_loop_data=(data) ⇒ Object

set global loop data to global/second env



39
40
41
# File 'lib/crisp/chained_env.rb', line 39

def global_loop_data=(data)
  @second.global_loop_data = data
end

#has_key?(key) ⇒ Boolean

Returns boolean for existence of given key in one of the environments.

Returns:

  • (Boolean)


14
15
16
# File 'lib/crisp/chained_env.rb', line 14

def has_key?(key)
  @first.has_key?(key) or @second.has_key?(key)
end