Class: Scash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scash.rb,
lib/scash/version.rb

Constant Summary collapse

VERSION =
"0.4.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}, klass = HashWithIndifferentAccess) ⇒ Scash

Returns a new instance of Scash.



14
15
16
17
18
# File 'lib/scash.rb', line 14

def initialize(variables = {}, klass = HashWithIndifferentAccess)
  @klass = klass
  @hash = convert(variables)
  @inverse_hash = convert(variables)
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



12
13
14
# File 'lib/scash.rb', line 12

def stack
  @stack
end

Instance Method Details

#[](key, inverse = false) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/scash.rb', line 43

def [](key, inverse = false)
  if inverse
    @inverse_hash[key]
  else
    @hash[key]
  end
end

#define_global_variable(key, value) ⇒ Object



51
52
53
# File 'lib/scash.rb', line 51

def define_global_variable(key, value)
  define_global_variables key => value
end

#define_global_variables(variables) ⇒ Object



55
56
57
58
# File 'lib/scash.rb', line 55

def define_global_variables(variables)
  @hash.merge! variables
  global_variables.merge! variables
end

#scope(variables) ⇒ Object Also known as: with



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/scash.rb', line 28

def scope(variables)
  previous_hash = @hash.slice *variables.keys.select{|key| @hash.key?(key)}
  previous_inverse_hash = @inverse_hash.slice *variables.keys.select{|key| @inverse_hash.key?(key)}
  @hash.merge! variables
  @inverse_hash.merge!(variables.reject{|key| @inverse_hash.key?(key)})
  yield
ensure
  variables.each_key{|key| @hash.delete(key)}
  variables.each_key{|key| @inverse_hash.delete(key)}
  @inverse_hash.merge!(previous_inverse_hash)
  @hash.merge!(previous_hash)
  @hash.merge!(global_variables.reject { |k| @hash.has_key?(k) })
end

#to_hashObject



20
21
22
# File 'lib/scash.rb', line 20

def to_hash
  @hash
end

#to_inverse_hashObject



24
25
26
# File 'lib/scash.rb', line 24

def to_inverse_hash
  @inverse_hash
end