Module: Parse

Includes:
Assertions, Constants
Defined in:
app/models/parse.rb

Defined Under Namespace

Modules: Assertions, Constants, Examples

Constant Summary

Constants included from Constants

Constants::CSV, Constants::LINE, Constants::LINES, Constants::LINES_cryptic, Constants::Line_terminator, Constants::Terminated_line, Constants::WORDS

Instance Method Summary collapse

Methods included from Assertions

#add_parse_message, #assert_parse, #assert_parse_repetition, #assert_parse_sequence, #newline_if_not_empty

Instance Method Details

#default_name(index, prefix = 'Col_') ⇒ Object

parse


72
73
74
# File 'app/models/parse.rb', line 72

def default_name(index, prefix='Col_')
	prefix+index.to_s
end

#parse(string_or_array, pattern = WORDS) ⇒ Object

parse takes an input string or possibly nested array of strings and returns an array of regexp captures per string. The array of captures replacing the input strings adds one additional layer of Array nesting.


63
64
65
66
67
68
69
70
71
# File 'app/models/parse.rb', line 63

def parse(string_or_array, pattern=WORDS)
	if string_or_array.instance_of?(String) then
		parse_string(string_or_array, pattern)
	elsif string_or_array.instance_of?(Array) then
		parse_array(string_or_array, pattern)
	else
		nil
	end #if
end

#parse_array(string_array, pattern = WORDS) ⇒ Object

parse_split


56
57
58
59
60
# File 'app/models/parse.rb', line 56

def parse_array(string_array, pattern=WORDS)
	string_array.map do |string|
		parse(string,pattern)
	end #map
end

#parse_name_values(array, pairs, new_names, pattern) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/parse.rb', line 75

def parse_name_values(array, pairs, new_names, pattern)
	ret={}
	next_pair=pairs.pop
	next_name=new_names.pop
	array.each_index do |string, i|
		if i==next_pair then
			ret[array[next_pair].to_sym]=array[next_pair+1]
			next_pair=pairs.pop
		else
			matchData=string.match(pattern)
			if matchData then
				ret[matchData[1].to_sym]=matchData[2]			
			else
				if next_name.nil? then
				else
					ret[next_name.to_sym]=string			
					next_name=new_names.pop
				end #if
			end #if
		end #if
	end #map
end

#parse_split(string, pattern = Terminated_line, ending = :optional) ⇒ Object

Splits pattern match captures into an array of parses Uses Regexp capture mechanism in String#split


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/parse.rb', line 38

def parse_split(string, pattern=Terminated_line, ending=:optional)
	ret=string.split(pattern)
	case ending
	when :optional then 
		if ret[-1].nil? then
			ret[0..-2] #drop empty
		else
			ret
		end #if 
	when :delimiter then string.split(pattern) 
	when :terminator then
		if ret[-1].nil? then
			ret[0..-2] #drop empty
		else
			ret
		end #if 
	end #case
end

#parse_string(string, pattern = LINES) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/parse.rb', line 21

def parse_string(string, pattern=LINES)
	matchData=string.match(pattern)
  if matchData.nil? then
    []
  elsif matchData.names==[] then
		matchData[1..-1] # return unnamed subexpressions
	else
#     named_captures for captures.size > names.size
		named_hash={}
		matchData.names.each do |n| # return named subexpressions
			named_hash[n.to_sym]=matchData[n]
		end # each
		named_hash
	end #if
end

#rows_and_columns(column_pattern = Parse::WORDS, row_pattern = Parse::LINES) ⇒ Object

parse_name_values


97
98
99
100
101
# File 'app/models/parse.rb', line 97

def rows_and_columns(column_pattern=Parse::WORDS, row_pattern=Parse::LINES)
	parse(@output, row_pattern).map  do |row| 
		parse(row, column_pattern)
	end #map
end