Class: Shift::ShiftBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/shift-lang/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShiftBuilder

Returns a new instance of ShiftBuilder.



10
11
12
13
14
# File 'lib/shift-lang/builder.rb', line 10

def initialize()
	@models = []
	@handlers = []
	@clean = true
end

Instance Attribute Details

#cleanObject

Returns the value of attribute clean.



8
9
10
# File 'lib/shift-lang/builder.rb', line 8

def clean
  @clean
end

#handlersObject

Returns the value of attribute handlers.



8
9
10
# File 'lib/shift-lang/builder.rb', line 8

def handlers
  @handlers
end

#modelsObject

Returns the value of attribute models.



8
9
10
# File 'lib/shift-lang/builder.rb', line 8

def models
  @models
end

Instance Method Details

#add_handler(handler) ⇒ Object



121
122
123
# File 'lib/shift-lang/builder.rb', line 121

def add_handler(handler)
	@handlers.push handler
end

#add_model(model) ⇒ Object



117
118
119
# File 'lib/shift-lang/builder.rb', line 117

def add_model(model)
	@models.push model
end

#get_data_from_file(file_name) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/shift-lang/builder.rb', line 16

def get_data_from_file(file_name)
	parser = Shift::ShiftParser.new

	model = nil

	url_handler = nil

	last_statement = ""
	this_statement = ""

	url = ""

	line_number = 0

	File.open(file_name) do |file|
		while line = file.gets
			parts = line.split("\t")
			num_tabs = parts.length - 1
			line = line.strip
			line_number += 1
			if line != ""
				begin
				statement = parser.parse(line)

				this_statement = statement.keys[0]

				case this_statement

				when :model_definition_statement
					if num_tabs != 0
						@clean = false
						puts "Error at line #{line_number} character 1 : #{line}"
					end
					model = Builder::DBModel.new(statement[:model_definition_statement][:model_name])

				when :model_attribute_definition_statement
					if num_tabs != 1
						@clean = false
						puts "Error at line #{line_number} character 1 : #{line}"
					end
					model_attribute = statement[:model_attribute_definition_statement]
					model.add_attribute(model_attribute[:attribute_name], model_attribute[:attribute_type])

				when :url
					if (last_statement == :model_attribute_definition_statement)
						add_model model
					end

					if num_tabs != 0
						@clean = false
						puts "Error at line #{line_number} character 1 : #{line}"
					end
					
					url = statement[:url]
				when :url_method
					if url_handler
						add_handler url_handler
					end

					if num_tabs != 1
						@clean = false
						puts "Error at line #{line_number} character 1 : #{line}"
					end

					url_handler = Builder::UrlHandler.new(url, statement[:url_method].to_s)
				else
					if num_tabs < 2
						@clean = false
						puts "Error at line #{line_number} character 1 : #{line}"
					end
					stmt = {
						:statement => statement, 
						:num_tabs => num_tabs
					}
					if !url_handler 
						url_handler = Builder::UrlHandler.new("", "")
					end	
					url_handler.add_statement stmt
				end			

				last_statement = this_statement

				rescue Parslet::ParseFailed => parse_error
					error = parse_error.to_s
					error =  error.split(" ")
					puts "Error at line #{line_number} character #{error[error.length - 1]} : #{line}"
					@clean = false
				end
			end
		end
	end

	if (last_statement == :model_attribute_definition_statement)
		add_model model
	else
		add_handler url_handler
	end

	self
end