Class: Scash
- Inherits:
-
Object
- Object
- Scash
- Includes:
- Enumerable
- Defined in:
- lib/scash.rb,
lib/scash/version.rb
Constant Summary collapse
- VERSION =
"0.3.2"
Instance Attribute Summary collapse
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
- #[](key, inverse = false) ⇒ Object
- #define_global_variable(key, value) ⇒ Object
- #define_global_variables(variables) ⇒ Object
-
#initialize(variables = {}, klass = HashWithIndifferentAccess) ⇒ Scash
constructor
A new instance of Scash.
- #scope(variables) ⇒ Object (also: #with)
- #to_hash ⇒ Object
- #to_inverse_hash ⇒ Object
Constructor Details
#initialize(variables = {}, klass = HashWithIndifferentAccess) ⇒ Scash
13 14 15 16 17 |
# File 'lib/scash.rb', line 13 def initialize(variables = {}, klass = HashWithIndifferentAccess) @klass = klass @hash = convert(variables) @inverse_hash = convert(variables) end |
Instance Attribute Details
#stack ⇒ Object (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
42 43 44 45 46 47 48 |
# File 'lib/scash.rb', line 42 def [](key, inverse = false) if inverse @inverse_hash[key] else @hash[key] end end |
#define_global_variable(key, value) ⇒ Object
50 51 52 |
# File 'lib/scash.rb', line 50 def define_global_variable(key, value) define_global_variables key => value end |
#define_global_variables(variables) ⇒ Object
54 55 56 57 |
# File 'lib/scash.rb', line 54 def define_global_variables(variables) @hash.merge! variables global_variables.merge! variables end |
#scope(variables) ⇒ Object Also known as: with
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/scash.rb', line 27 def scope(variables) previous_hash = @hash.select{|key|variables.key?(key.to_s) || variables.key?(key.to_sym)} previous_inverse_hash = @inverse_hash.select{|key|variables.key?(key.to_s) || variables.key?(key.to_sym)} @hash.merge! variables @inverse_hash.merge!(variables.reject{|key| @inverse_hash.key?(key)}) yield ensure variables.keys.each{|key| @hash.delete(key)} variables.keys.each{|key| @inverse_hash.delete(key)} @inverse_hash.merge!(previous_inverse_hash) @hash.merge!(previous_hash) @hash.merge!(global_variables) end |
#to_hash ⇒ Object
19 20 21 |
# File 'lib/scash.rb', line 19 def to_hash @hash end |
#to_inverse_hash ⇒ Object
23 24 25 |
# File 'lib/scash.rb', line 23 def to_inverse_hash @inverse_hash end |