Class: Reviser::Reviser

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

Overview

This class is basically here to give the user a generic and comprehensive way to use and customize the behavior of our tool. The main idea is that the user should not instantiate components himself, nor worry about the data these components exchange. It is the API entry point.

Author:

  • Renan Strauss

Constant Summary collapse

@@setup =
false
@@loaded_components =
{}
@@registered_extensions =
[]

Class Method Summary collapse

Class Method Details

.load(data) ⇒ Object

Adds an entry with the specified data.

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reviser.rb', line 84

def self.load(data)
	raise ArgumentError unless data.has_key?(:component)

	data[:input_from] ||= nil
	data[:local] ||= false

	@@loaded_components.store data[:component],
	{
		:input_from => data[:input_from],
		:local => data[:local],
		:data => nil
	}
end

.register(data) ⇒ Object

Registers the specified extension (its methods will be available for analysis)

Raises:

  • (ArgumentError)


102
103
104
105
106
# File 'lib/reviser.rb', line 102

def self.register(data)
	raise ArgumentError unless data.has_key?(:extension)

	@@registered_extensions << data[:extension]
end

.registered_extensionsObject



77
78
79
# File 'lib/reviser.rb', line 77

def self.registered_extensions
	@@registered_extensions
end

.runObject

Basically runs each loaded component. The exection order is based on the loading order.

Raises:

  • (RuntimeError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/reviser.rb', line 120

def self.run
	raise RuntimeError unless @@setup

	if Cfg.has_key?(:options) && Cfg[:options].has_key?(:log_dir)
		FileUtils.mkdir Cfg[:options][:log_dir] unless Dir.exist? Cfg[:options][:log_dir]
	end

	#
	# Need to change this ASAP in order to
	# let users load their own components
	#
	@@loaded_components.each do |comp, conf|
		puts "[ " + "Running ".yellow + "#{Reviser.titleize comp}".blue + " ]"

		require_relative "reviser/components/#{comp}" unless conf[:local]

		namespace = conf[:local] && '' || 'Components::'
		param = ((conf[:input_from] != nil) && @@loaded_components[conf[:input_from]][:data]) || nil
		
		c = eval("#{namespace}#{Reviser.titleize comp}").new param

		begin
			@@loaded_components[comp][:data] = c.work
		rescue Interrupt => i
			puts 'Bye bye'
		rescue Gem::LoadError => e
			puts 'Missing gem'.light_red + "\t" + e.message
			exit
		rescue Exception => ex
			puts 'Error'.red + "\t" + ex.message
			exit
		end
		
		puts "[ " + "Done".green + " ]"
	end

	# To handle multiple loads
	# and calls to run
	@@loaded_components = {}
end

.setup(config_file) ⇒ Object

Loads the configuration from given config_file



111
112
113
114
# File 'lib/reviser.rb', line 111

def self.setup(config_file)
	Cfg.load config_file
	@@setup = true
end

.titleize(str) ⇒ Object

Quite handy



164
165
166
# File 'lib/reviser.rb', line 164

def self.titleize(str)
	str.split(/ |\_/).map(&:capitalize).join('')
end