Class: SplitPgDump::Rule
- Inherits:
-
Object
- Object
- SplitPgDump::Rule
- Defined in:
- lib/split_pgdump.rb
Defined Under Namespace
Classes: ParseError
Instance Attribute Summary collapse
-
#regex ⇒ Object
readonly
Returns the value of attribute regex.
-
#sort_keys ⇒ Object
readonly
Returns the value of attribute sort_keys.
-
#split_parts ⇒ Object
readonly
Returns the value of attribute split_parts.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(table_regex, split_expr, sort_keys) ⇒ Rule
constructor
A new instance of Rule.
- #parse_sort_keys(sort_keys) ⇒ Object
- #parse_split_expr(split_expr) ⇒ Object
Constructor Details
#initialize(table_regex, split_expr, sort_keys) ⇒ Rule
Returns a new instance of Rule.
170 171 172 173 174 |
# File 'lib/split_pgdump.rb', line 170 def initialize(table_regex, split_expr, sort_keys) @regex = Regexp.new table_regex parse_split_expr(split_expr) parse_sort_keys(sort_keys) end |
Instance Attribute Details
#regex ⇒ Object (readonly)
Returns the value of attribute regex.
157 158 159 |
# File 'lib/split_pgdump.rb', line 157 def regex @regex end |
#sort_keys ⇒ Object (readonly)
Returns the value of attribute sort_keys.
157 158 159 |
# File 'lib/split_pgdump.rb', line 157 def sort_keys @sort_keys end |
#split_parts ⇒ Object (readonly)
Returns the value of attribute split_parts.
157 158 159 |
# File 'lib/split_pgdump.rb', line 157 def split_parts @split_parts end |
Class Method Details
.parse(line) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/split_pgdump.rb', line 158 def self.parse(line) line = line.sub(%r{(;|#|//).*$},'').strip return if line.empty? if line =~ /^(\S+)(?:\s+split:(\S+))?(?:\s+sort:((?:(?:[^\s:]+)(?::[MbdfghinRrV]+)?(?:\s+|\s*$))+))?$/ puts "#$1 split:#$2 sort:#$3" if $debug new($1, $2, $3) else raise ParseError, "Wrong rule line #{line}" end end |
Instance Method Details
#parse_sort_keys(sort_keys) ⇒ Object
206 207 208 209 210 |
# File 'lib/split_pgdump.rb', line 206 def parse_sort_keys(sort_keys) @sort_keys = (sort_keys || '').scan(/([^\s:]+)(?::([MbdfghinRrV]+))?/).map do |key, flags| {:field => key, :flags => flags} end end |
#parse_split_expr(split_expr) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/split_pgdump.rb', line 176 def parse_split_expr(split_expr) s = StringScanner.new(split_expr || '') parts = [] while !s.eos? if field = s.scan(/\$[^\[%!]+/) field = field[1..-1] part = [field] while !s.eos? if range = s.scan(/\[[+-]?\d+\.\.\.?[+-]?\d+\]/) part << eval(range[1...-1]) elsif mod = s.scan(/%\d+/) mod = mod[1..-1] format = "%0#{mod.size}d" modi = mod.to_i part << [format, modi] else break end end parts << part next if s.scan(/!/) elsif sep = s.scan(/[^$\s#\\]+/) parts << sep next end raise ParseError, "Wrong format of split expr #{split_expr} (rest: '#{s.rest}')" end @split_parts = parts end |