Class: Checker::Data
- Inherits:
-
Object
- Object
- Checker::Data
- Defined in:
- lib/asker/checker.rb
Instance Attribute Summary collapse
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
Instance Method Summary collapse
- #check ⇒ Object
- #check_code(line, index) ⇒ Object
- #check_col(line, index) ⇒ Object
- #check_concept(line, index) ⇒ Object
- #check_def(line, index) ⇒ Object
- #check_empty_lines(line, index) ⇒ Object
- #check_features(line, index) ⇒ Object
- #check_map(line, index) ⇒ Object
- #check_names(line, index) ⇒ Object
- #check_path(line, index) ⇒ Object
- #check_row(line, index) ⇒ Object
- #check_table(line, index) ⇒ Object
- #check_tags(line, index) ⇒ Object
- #check_template(line, index) ⇒ Object
- #check_type(line, index) ⇒ Object
- #check_unknown(line, index) ⇒ Object
- #count_spaces(line) ⇒ Object
- #find_parent(index) ⇒ Object
-
#initialize(filepath) ⇒ Data
constructor
A new instance of Data.
- #is_ok? ⇒ Boolean
- #show ⇒ Object
- #show_errors ⇒ Object
Constructor Details
#initialize(filepath) ⇒ Data
Returns a new instance of Data.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/asker/checker.rb', line 27 def initialize(filepath) @inputs = File.read(filepath).split("\n") @outputs = [] @inputs.each_with_index do |line, index| output = { id: index, level: 0, state: :none, type: :none, source: line, msg: '' } @outputs << output end @ok = false end |
Instance Attribute Details
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
25 26 27 |
# File 'lib/asker/checker.rb', line 25 def inputs @inputs end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
26 27 28 |
# File 'lib/asker/checker.rb', line 26 def outputs @outputs end |
Instance Method Details
#check ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/asker/checker.rb', line 64 def check @ok = true @inputs.each_with_index do |line, index| check_empty_lines(line, index) check_map(line, index) check_concept(line, index) check_names(line, index) (line, index) check_def(line, index) check_table(line, index) check_row(line, index) check_col(line, index) check_template(line, index) check_code(line, index) check_type(line, index) check_path(line, index) check_features(line, index) check_unknown(line, index) @ok = false unless @outputs[index][:state] == :ok @ok = false if @outputs[index][:type] == :unkown end @ok end |
#check_code(line, index) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/asker/checker.rb', line 258 def check_code(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%code' @outputs[index][:type] = :code @outputs[index][:level] = 1 @outputs[index][:state] = :ok if find_parent(index) != :map @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(map) not found!' elsif line != ' %code' @outputs[index][:state] = :err @outputs[index][:msg] = 'Write 2 spaces before %code' end end |
#check_col(line, index) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/asker/checker.rb', line 218 def check_col(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%col' @outputs[index][:type] = :col @outputs[index][:state] = :ok if count_spaces(line) == 8 @outputs[index][:level] = 4 if find_parent(index) != :row @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(row) not found!' end elsif count_spaces(line) == 10 @outputs[index][:level] = 5 if find_parent(index) != :row @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(row) not found!' end else @outputs[index][:state] = :err @outputs[index][:msg] = "Write 8 or 10 spaces before %col" end end |
#check_concept(line, index) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/asker/checker.rb', line 112 def check_concept(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%concept' @outputs[index][:type] = :concept @outputs[index][:level] = 1 @outputs[index][:state] = :ok if find_parent(index) != :map @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(map) not found!' elsif line != ' %concept' @outputs[index][:state] = :err @outputs[index][:msg] = 'Write 2 spaces before %concept' end end |
#check_def(line, index) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/asker/checker.rb', line 160 def check_def(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%def' @outputs[index][:type] = :def @outputs[index][:level] = 2 @outputs[index][:state] = :ok if find_parent(index) != :concept @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(concept) not found!' elsif not line.start_with? ' %def' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 4 spaces before %def" end end |
#check_empty_lines(line, index) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/asker/checker.rb', line 88 def check_empty_lines(line, index) if line.strip.size.zero? or line.start_with? '#' @outputs[index][:type] = :empty @outputs[index][:level] = -1 @outputs[index][:state] = :ok end end |
#check_features(line, index) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/asker/checker.rb', line 306 def check_features(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%features' @outputs[index][:type] = :features @outputs[index][:level] = 2 @outputs[index][:state] = :ok if find_parent(index) != :code @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(code) not found!' elsif not line.start_with? ' %features' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 4 spaces before %features" end end |
#check_map(line, index) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/asker/checker.rb', line 96 def check_map(line, index) if index == 0 @outputs[index][:type] = :map if line.start_with?('%map{') @outputs[index][:state] = :ok else @outputs[index][:state] = :err @outputs[index][:msg] = "Start with '%map{'" end elsif index > 0 and line.include?('%map{') @outputs[index][:state] = :err @outputs[index][:type] = :map @outputs[index][:msg] = "Write '%map' on line 0" end end |
#check_names(line, index) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/asker/checker.rb', line 128 def check_names(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%names' @outputs[index][:type] = :names @outputs[index][:level] = 2 @outputs[index][:state] = :ok if find_parent(index) != :concept @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(concept) not found!' elsif not line.start_with? ' %names' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 4 spaces before %names" end end |
#check_path(line, index) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/asker/checker.rb', line 290 def check_path(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%path' @outputs[index][:type] = :path @outputs[index][:level] = 2 @outputs[index][:state] = :ok if find_parent(index) != :code @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(code) not found!' elsif not line.start_with? ' %path' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 4 spaces before %type" end end |
#check_row(line, index) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/asker/checker.rb', line 192 def check_row(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%row' @outputs[index][:type] = :row @outputs[index][:state] = :ok if count_spaces(line) == 6 @outputs[index][:level] = 3 parent = find_parent(index) unless [:table, :features].include? parent @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(table/features) not found!' end elsif count_spaces(line) == 8 @outputs[index][:level] = 4 if find_parent(index) != :template @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(template) not found!' end else @outputs[index][:state] = :err @outputs[index][:msg] = "Write 6 or 8 spaces before %row" end end |
#check_table(line, index) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/asker/checker.rb', line 176 def check_table(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%table' @outputs[index][:type] = :table @outputs[index][:level] = 2 @outputs[index][:state] = :ok if find_parent(index) != :concept @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(concept) not found!' elsif not line.start_with? ' %table' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 4 spaces before %table" end end |
#check_tags(line, index) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/asker/checker.rb', line 144 def (line, index) return unless @outputs[index][:state] == :none return unless line.include? '%tags' @outputs[index][:type] = :tags @outputs[index][:level] = 2 @outputs[index][:state] = :ok if find_parent(index) != :concept @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(concept) not found!' elsif not line.start_with? ' %tags' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 4 spaces before %tags" end end |
#check_template(line, index) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/asker/checker.rb', line 242 def check_template(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%template' @outputs[index][:type] = :template @outputs[index][:level] = 3 @outputs[index][:state] = :ok if find_parent(index) != :table @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(concept) not found!' elsif not line.start_with? ' %template' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 6 spaces before %template" end end |
#check_type(line, index) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/asker/checker.rb', line 274 def check_type(line, index) return unless @outputs[index][:state] == :none return unless line.include? '%type' @outputs[index][:type] = :type @outputs[index][:level] = 2 @outputs[index][:state] = :ok if find_parent(index) != :code @outputs[index][:state] = :err @outputs[index][:msg] = 'Parent(code) not found!' elsif not line.start_with? ' %type' @outputs[index][:state] = :err @outputs[index][:msg] = "Write 4 spaces before %type" end end |
#check_unknown(line, index) ⇒ Object
322 323 324 325 326 327 328 |
# File 'lib/asker/checker.rb', line 322 def check_unknown(line, index) return unless @outputs[index][:state] == :none @outputs[index][:type] = :unknown @outputs[index][:level] = count_spaces(line)/2 @outputs[index][:state] = :err @outputs[index][:msg] = "Unknown tag with parent(#{find_parent(index)})!" end |
#count_spaces(line) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/asker/checker.rb', line 341 def count_spaces(line) return 0 if line.start_with? '%' return 1 if line.start_with? ' %' return 2 if line.start_with? ' %' return 3 if line.start_with? ' %' return 4 if line.start_with? ' %' return 5 if line.start_with? ' %' return 6 if line.start_with? ' %' return 7 if line.start_with? ' %' return 8 if line.start_with? ' %' return 9 if line.start_with? ' %' return 10 if line.start_with? ' %' return -1 end |
#find_parent(index) ⇒ Object
330 331 332 333 334 335 336 337 338 339 |
# File 'lib/asker/checker.rb', line 330 def find_parent(index) current_level = @outputs[index][:level] return :noparent if current_level.zero? i = index - 1 while(i >= 0) return @outputs[i][:type] if @outputs[i][:level] == current_level - 1 i = i - 1 end return :noparent end |
#is_ok? ⇒ Boolean
42 43 44 |
# File 'lib/asker/checker.rb', line 42 def is_ok? @ok end |
#show ⇒ Object
46 47 48 49 50 |
# File 'lib/asker/checker.rb', line 46 def show @outputs.each do |i| puts "#{i[:id]}: #{i[:state]} [#{i[:type]}] #{i[:msg]}" end end |
#show_errors ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/asker/checker.rb', line 52 def show_errors errors = 0 @outputs.each do |i| next if i[:state] == :ok errors += 1 puts "%02d" % i[:id] + ": %s." % i[:msg] + " => #{i[:source][0,40]}" if errors < 11 puts "..." if errors == 11 end puts Rainbow("[ERROR] #{errors} errors from #{@inputs.size} lines!").red.bright if errors > 0 puts Rainbow("Syntax OK!").green if errors == 0 end |