Class: Normalizer

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

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Normalizer

Returns a new instance of Normalizer.



2
3
4
5
6
7
8
# File 'lib/normalizer.rb', line 2

def initialize(file, options = {})
	@source = file
	@options = options
	@env = environment

	parse
end

Instance Method Details

#parseObject



10
11
12
13
14
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
# File 'lib/normalizer.rb', line 10

def parse
	content = File.read(@source)

	# Scan the content of the file, and search for *unique* variable names
	# that we need to do a search and replace for.
	#
	variables = content.scan(/\{\w+\}/).uniq

	variables.each do |var|
	  # Since the place holders are in the file like such {MY_VAR}, we can
	  # assume that the first character is the open bracket, and the last
	  # is the closing bracket so lets just trim those off.
	  #
	  env_var = var[1..-2]

	  if @env.key? env_var
	    content.gsub! var, @env[env_var].chomp

	  else
	    # NOTE: mdelaney
	    # I'm not too sure if we should fail, i.e. return a non zero, or just
	    # print out a message to the console. For the moment, we'll just print
	    # out a message to the console. Should we want to fail the build that's
	    # a trivial change to make.
	    #
	    puts format('WARNING: No such environment variable %s', env_var)
	  end
	end

	output_file = @options[:output] ||= @source
	File.write(output_file, content)
end