Class: ThriftGenerator

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

Constant Summary collapse

GEN_FOLDER =
"gen-rb"

Instance Method Summary collapse

Constructor Details

#initialize(module_name) ⇒ ThriftGenerator

Returns a new instance of ThriftGenerator.



5
6
7
8
9
# File 'lib/thrift_generator.rb', line 5

def initialize(module_name)
	@base_dir = File.expand_path Dir.getwd
	@module_name = module_name
	@idl = "#{module_name.downcase}.thrift"
end

Instance Method Details

#generate_if_needed(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/thrift_generator.rb', line 11

def generate_if_needed(&block)
	# puts "Generating Ruby classes for all available thrift files in #{@base_dir}/#{GEN_FOLDER}"
	Dir.glob("*.thrift").each { |f|
		# puts "Compiling #{f}"
		`thrift -r --gen rb #{f}`
	}

	service_file = @base_dir + "/#{GEN_FOLDER}/#{@module_name.downcase}.rb"
	
	if !File.exists?(service_file)
		puts "Could not generate code for module #{@module_name} - can't find .thrift file"
		exit 1
	end

	load_module(service_file)
end

#load_module(service_file) ⇒ Object



28
29
30
31
32
# File 'lib/thrift_generator.rb', line 28

def load_module(service_file)
$LOAD_PATH.unshift(GEN_FOLDER)
require service_file
eval(@module_name)
end