Class: Balmora::Variables

Inherits:
Object
  • Object
show all
Defined in:
lib/balmora/variables.rb

Defined Under Namespace

Classes: Config, Error, Options, Variables

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extension, shell, state) ⇒ Variables

Returns a new instance of Variables.



9
10
11
12
13
# File 'lib/balmora/variables.rb', line 9

def initialize(extension, shell, state)
  @extension = extension
  @shell = shell
  @state = state
end

Class Method Details

.factory(state) ⇒ Object



5
6
7
# File 'lib/balmora/variables.rb', line 5

def self.factory(state)
  return self.new(state.extension, state.shell, state)
end

Instance Method Details

#_inject_array(value, string) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/balmora/variables.rb', line 63

def _inject_array(value, string)
  result = []

  value.each_with_index() { |item, index|
    if item.instance_of?(::Hash) && item.keys() == [:'extend-variable']
      result += get(item[:'extend-variable'])
    else
      result.push(inject(item, string))
    end
  }

  return result
end

#_inject_hash(value, string) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/balmora/variables.rb', line 77

def _inject_hash(value, string)
  result = {}
  value.each() { |key, item|
    if key == :'include-variable'
      result.merge!(get(item))
    else
      result[key] = inject(item, string)
    end
  }

  return result
end

#_inject_string(value) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/balmora/variables.rb', line 90

def _inject_string(value)
  value = value.gsub('\\\\', '{{__ESCAPED_SLASH__}}')

  if value.match(/(?<!\\)\#\{/)
    value = value.gsub('\\#', '#')
    value = eval('"' + value + '"')
  end

  value = value.gsub(/(?<!\\)\${(.*?)}/) { |string | get(string[2...-1]) }
  value = value.gsub(/(?<!\\)\%{(.*?)}/) { |string |
    command = @shell.expression(string[2...-1])
    @shell.run!([command], verbose: false, message: 'Executing embedded ' +
      'command: ').rstrip()
  }

  value = value.gsub('\\$', '$')
  value = value.gsub('\\%', '%')
  value = value.gsub('{{__ESCAPED_SLASH__}}', '\\')
end

#get(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/balmora/variables.rb', line 15

def get(name)
  if !name.instance_of?(::Array)
    name = name.split('.').collect() { |part| part.to_sym() }
  else
    name = name.clone()
  end

  parts = []
  provider = @extension.get(Balmora::Variables, name.shift()).factory(@state)

  result =
    name.
    inject(provider) { |current, part|
      parts.push(part)

      if current.instance_of?(::Hash)
        if !current.has_key?(part)
          raise Error.new("Unknown variable #{parts.join('.')}")
        end

        next current[part]
      end

      if !current.respond_to?(part)
        raise Error.new("Unknown variable #{parts.join('.')}")
      end

      next current.public_send(part)
    }

  return result
end

#inject(value, string = true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/balmora/variables.rb', line 48

def inject(value, string = true)
  result =
    if value.instance_of?(::Array)
      _inject_array(value, string)
    elsif value.instance_of?(::Hash)
      _inject_hash(value, string)
    elsif value.instance_of?(::String) && string
      _inject_string(value)
    else
      value
    end

  return result
end