Class: Scash

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

Constant Summary collapse

VERSION =
"0.2.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = nil, klass = HashWithIndifferentAccess) ⇒ Scash

Returns a new instance of Scash.



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

def initialize(variables = nil, klass = HashWithIndifferentAccess)
  @klass = klass
  @stack = [convert(variables), global_variables].compact
  build!
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



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

def stack
  @stack
end

Instance Method Details

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



38
39
40
41
42
43
44
# File 'lib/scash.rb', line 38

def [](key, inverse = false)
  if inverse
    to_inverse_hash[key]
  else
    to_hash[key]
  end
end

#define_global_variable(key, value) ⇒ Object



46
47
48
# File 'lib/scash.rb', line 46

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

#define_global_variables(variables) ⇒ Object



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

def define_global_variables(variables)
  global_variables.merge! convert(variables)
  build!
end

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



27
28
29
30
31
32
33
34
35
# File 'lib/scash.rb', line 27

def scope(variables)
  @stack.unshift convert(variables)
  added = true
  build!
  yield
ensure
  @stack.shift if added
  build!
end

#to_hashObject



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

def to_hash
  @hashes.first
end

#to_inverse_hashObject



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

def to_inverse_hash
  @inverse_hashes.first
end