Module: VarBlock::Globals

Included in:
Object, VarHash
Defined in:
lib/var_block/globals.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/var_block/globals.rb', line 3

def self.included(base)
  # fail if there is already same-name methods to prevent breaking dependencies
  # thanks to Jack, https://stackoverflow.com/questions/44156150/how-to-raise-error-when-including-a-module-that-already-has-same-name-methods
  overrides = instance_methods.reject { |method| base.instance_method(method).owner == self }
  raise "#{name} overrides #{overrides.join(', ')}" if overrides.any?

  base.extend self
end

Instance Method Details

#getvar(var_hash, index, *options) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/var_block/globals.rb', line 12

def getvar(var_hash, index, *options)
  raise ArgumentError, '1st argument should be a VarHash object!' unless var_hash.is_a? VarHash

  value = var_hash[index]

  return_value = case value
                 when VarArray
                   VarBlock::GetvarHandlers.handle_var_array(value, self)
                 when Proc
                   VarBlock::GetvarHandlers.handle_proc(value, self)
                 else
                   VarBlock::GetvarHandlers.handle_default(value, self)
                 end

  unless options.empty?
    return_value = VarBlock::GetvarHandlers.handle_options(return_value, self, options)
  end

  return_value
end

#with(var_hash = nil, **variables) {|var_hash| ... } ⇒ Object

Yields:

  • (var_hash)


33
34
35
36
37
38
39
40
41
# File 'lib/var_block/globals.rb', line 33

def with(var_hash = nil, **variables)
  var_hash = VarHash.new(var_hash: var_hash)

  variables.each do |key, value|
    var_hash[key] = value
  end

  yield var_hash
end