Class: Rgviz::MagicNamesVisitor
- Inherits:
-
Visitor
- Object
- Visitor
- Rgviz::MagicNamesVisitor
- Defined in:
- lib/rgviz_rails/view_helper.rb
Instance Method Summary collapse
- #append_after_source_type(type) ⇒ Object
- #append_before_source_type(type) ⇒ Object
- #append_selections(node, source) ⇒ Object
- #get_source(name, include_type = true) ⇒ Object
- #get_source_type(source, name) ⇒ Object
- #has_magic_name?(node) ⇒ Boolean
-
#initialize(html_prefix, js_prefix, param_prefix) ⇒ MagicNamesVisitor
constructor
A new instance of MagicNamesVisitor.
- #params ⇒ Object
- #print_columns(node) ⇒ Object
- #query_builder ⇒ Object
- #query_builder_var ⇒ Object
- #uses_rgviz_append? ⇒ Boolean
- #uses_rgviz_get_value? ⇒ Boolean
- #visit_aggregate_column(node) ⇒ Object
- #visit_binary_expression(node) ⇒ Object
- #visit_boolean_column(node) ⇒ Object
- #visit_date_column(node) ⇒ Object
- #visit_date_time_column(node) ⇒ Object
- #visit_format(node) ⇒ Object
- #visit_group_by(node) ⇒ Object
- #visit_id_column(node) ⇒ Object
- #visit_label(node) ⇒ Object
- #visit_logical_expression(node) ⇒ Object
- #visit_number_column(node) ⇒ Object
- #visit_order_by(node) ⇒ Object
- #visit_pivot(node) ⇒ Object
- #visit_query(node) ⇒ Object
- #visit_scalar_function_column(node) ⇒ Object
- #visit_select(node) ⇒ Object
- #visit_string_column(node) ⇒ Object
- #visit_time_of_day_column(node) ⇒ Object
- #visit_unary_expression(node) ⇒ Object
- #visit_where(node) ⇒ Object
Constructor Details
#initialize(html_prefix, js_prefix, param_prefix) ⇒ MagicNamesVisitor
Returns a new instance of MagicNamesVisitor.
290 291 292 293 294 295 296 |
# File 'lib/rgviz_rails/view_helper.rb', line 290 def initialize(html_prefix, js_prefix, param_prefix) @html_prefix = html_prefix @js_prefix = js_prefix @param_prefix = param_prefix @s = '' @params = [] end |
Instance Method Details
#append_after_source_type(type) ⇒ Object
604 605 606 607 608 609 610 611 |
# File 'lib/rgviz_rails/view_helper.rb', line 604 def append_after_source_type(type) case type when :number return else @s << "'\"" end end |
#append_before_source_type(type) ⇒ Object
589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/rgviz_rails/view_helper.rb', line 589 def append_before_source_type(type) case type when :number return when :string @s << "\"'" when :date @s << "date \"'" when :datetime @s << "datetime \"'" when :timeofday @s << "timeofday \"'" end end |
#append_selections(node, source) ⇒ Object
613 614 615 616 617 618 619 620 621 622 |
# File 'lib/rgviz_rails/view_helper.rb', line 613 def append_selections(node, source) @s << "q += rgviz_append(s, '"; node.left.accept self @s << " #{node.operator} " append_before_source_type source[:type] @s << ", " append_after_source_type source[:type] @s << "');\n" @uses_rgviz_append = true end |
#get_source(name, include_type = true) ⇒ Object
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/rgviz_rails/view_helper.rb', line 549 def get_source(name, include_type = true) if name.start_with?(@html_prefix) if include_type get_source_type :html, name[@html_prefix.length .. -1] else {:source => :html, :id => name[@html_prefix.length .. -1]} end elsif name.start_with?(@js_prefix) if include_type get_source_type :js, name[@js_prefix.length .. -1] else {:source => :js, :id => name[@js_prefix.length .. -1]} end elsif name.start_with?(@param_prefix) if include_type get_source_type :param, name[@param_prefix.length .. -1] else {:source => :param, :id => name[@param_prefix.length .. -1]} end else {} end end |
#get_source_type(source, name) ⇒ Object
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/rgviz_rails/view_helper.rb', line 573 def get_source_type(source, name) if name.start_with?('number_') {:source => source, :id => name[7 .. -1], :type => :number} elsif name.start_with?('string_') {:source => source, :id => name[7 .. -1], :type => :string} elsif name.start_with?('date_') {:source => source, :id => name[5 .. -1], :type => :date} elsif name.start_with?('datetime_') {:source => source, :id => name[9 .. -1], :type => :datetime} elsif name.start_with?('timeofday_') {:source => source, :id => name[10 .. -1], :type => :timeofday} else {:source => source, :id => name, :type => :string} end end |
#has_magic_name?(node) ⇒ Boolean
624 625 626 627 628 629 |
# File 'lib/rgviz_rails/view_helper.rb', line 624 def has_magic_name?(node) return false unless node.kind_of?(IdColumn) source = get_source node.name return false unless source[:source] return source end |
#params ⇒ Object
306 307 308 |
# File 'lib/rgviz_rails/view_helper.rb', line 306 def params @params end |
#print_columns(node) ⇒ Object
542 543 544 545 546 547 |
# File 'lib/rgviz_rails/view_helper.rb', line 542 def print_columns(node) node.columns.each_with_index do |c, i| @s << ', ' if i > 0 c.accept self end end |
#query_builder ⇒ Object
298 299 300 |
# File 'lib/rgviz_rails/view_helper.rb', line 298 def query_builder @s.strip end |
#query_builder_var ⇒ Object
302 303 304 |
# File 'lib/rgviz_rails/view_helper.rb', line 302 def query_builder_var 'q' end |
#uses_rgviz_append? ⇒ Boolean
314 315 316 |
# File 'lib/rgviz_rails/view_helper.rb', line 314 def uses_rgviz_append? @uses_rgviz_append end |
#uses_rgviz_get_value? ⇒ Boolean
310 311 312 |
# File 'lib/rgviz_rails/view_helper.rb', line 310 def uses_rgviz_get_value? @uses_rgviz_get_value end |
#visit_aggregate_column(node) ⇒ Object
535 536 537 538 539 540 |
# File 'lib/rgviz_rails/view_helper.rb', line 535 def visit_aggregate_column(node) @s << "#{node.function}(" node.argument.accept self @s << ")" false end |
#visit_binary_expression(node) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/rgviz_rails/view_helper.rb', line 427 def visit_binary_expression(node) if node.operator == BinaryExpression::Eq source = has_magic_name?(node.right) if source @s << "';\n" case source[:source] when :html @s << "var s = rgviz_get_value('#{source[:id]}');\n" append_selections node, source @uses_rgviz_get_value = true when :js @s << "var s = #{source[:id]}();\n" @s << "if (typeof(s) != 'object') s = [s];\n" append_selections node, source when :param @s << "var s = param_#{source[:id]};\n" @s << "if (typeof(s) != 'object') s = [s];\n" append_selections node, source @params << source[:id].to_i unless @params.include?(source[:id]) end @s << "q += '" return false end end node.left.accept self @s << " #{node.operator} " node.right.accept self false end |
#visit_boolean_column(node) ⇒ Object
501 502 503 |
# File 'lib/rgviz_rails/view_helper.rb', line 501 def visit_boolean_column(node) @s << node.value.to_s end |
#visit_date_column(node) ⇒ Object
505 506 507 |
# File 'lib/rgviz_rails/view_helper.rb', line 505 def visit_date_column(node) @s << "date \"#{node.value.to_s}\"" end |
#visit_date_time_column(node) ⇒ Object
509 510 511 |
# File 'lib/rgviz_rails/view_helper.rb', line 509 def visit_date_time_column(node) @s << "date \"#{node.value.strftime('%Y-%m-%d %H:%M:%S')}\"" end |
#visit_format(node) ⇒ Object
406 407 408 409 410 411 412 413 414 415 |
# File 'lib/rgviz_rails/view_helper.rb', line 406 def visit_format(node) node.column.accept self @s << ' ' if node.pattern.include?("'") @s << "\"#{node.pattern}\"" else @s << "\\'#{node.pattern}\\'" end false end |
#visit_group_by(node) ⇒ Object
368 369 370 371 372 373 |
# File 'lib/rgviz_rails/view_helper.rb', line 368 def visit_group_by(node) @s << "group by " print_columns node @s << " " false end |
#visit_id_column(node) ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/rgviz_rails/view_helper.rb', line 469 def visit_id_column(node) source = get_source node.name case source[:source] when nil @s << "`#{node.name}`" when :html append_before_source_type source[:type] @s << " + rgviz_get_value('#{source[:id]}') + " append_after_source_type source[:type] @uses_rgviz_get_value = true when :js append_before_source_type source[:type] @s << " + #{source[:id]}() + " append_after_source_type source[:type] when :param append_before_source_type source[:type] @s << " + param_#{source[:id]} + " append_after_source_type source[:type] @params << source[:id].to_i unless @params.include?(source[:id]) end end |
#visit_label(node) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/rgviz_rails/view_helper.rb', line 394 def visit_label(node) node.column.accept self @s << ' ' if node.label.include?("'") val = node.label.gsub("'", "\\'") @s << "\"#{val}\"" else @s << "\\'#{node.label}\\'" end false end |
#visit_logical_expression(node) ⇒ Object
417 418 419 420 421 422 423 424 425 |
# File 'lib/rgviz_rails/view_helper.rb', line 417 def visit_logical_expression(node) @s += "(" node.operands.each_with_index do |operand, i| @s += " #{node.operator} " if i > 0 operand.accept self end @s += ")" false end |
#visit_number_column(node) ⇒ Object
492 493 494 |
# File 'lib/rgviz_rails/view_helper.rb', line 492 def visit_number_column(node) @s << node.value.to_s end |
#visit_order_by(node) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/rgviz_rails/view_helper.rb', line 382 def visit_order_by(node) @s << "order by " node.sorts.each_with_index do |s, i| @s << ', ' if i > 0 s.column.accept self @s << ' ' @s << s.order.to_s end @s << " " false end |
#visit_pivot(node) ⇒ Object
375 376 377 378 379 380 |
# File 'lib/rgviz_rails/view_helper.rb', line 375 def visit_pivot(node) @s << "pivot " print_columns node @s << " " false end |
#visit_query(node) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/rgviz_rails/view_helper.rb', line 318 def visit_query(node) @s << "var q = '" if node.select && node.select.columns && node.select.columns.length > 0 node.select.accept self else @s << 'select * ' end node.where.accept self if node.where node.group_by.accept self if node.group_by node.pivot.accept self if node.pivot node.order_by.accept self if node.order_by @s << "limit #{node.limit} " if node.limit @s << "offset #{node.offset} " if node.offset if node.labels @s << "label " node.labels.each_with_index do |l, i| @s << ', ' if i > 0 l.accept self end end if node.formats @s << "format " node.formats.each_with_index do |f, i| @s << ', ' if i > 0 f.accept self end end if node. @s << "options " @s << "no_values " if node..no_values @s << "no_format " if node..no_format end @s << "';\n" false end |
#visit_scalar_function_column(node) ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/rgviz_rails/view_helper.rb', line 517 def visit_scalar_function_column(node) case node.function when ScalarFunctionColumn::Sum, ScalarFunctionColumn::Difference, ScalarFunctionColumn::Product, ScalarFunctionColumn::Quotient node.arguments[0].accept self @s << " #{node.function} " node.arguments[1].accept self else @s << "#{node.function}(" node.arguments.each_with_index do |a, i| @s << ', ' if i > 0 a.accept self end @s << ")" end false end |
#visit_select(node) ⇒ Object
354 355 356 357 358 359 |
# File 'lib/rgviz_rails/view_helper.rb', line 354 def visit_select(node) @s << "select "; print_columns node @s << " " false end |
#visit_string_column(node) ⇒ Object
496 497 498 499 |
# File 'lib/rgviz_rails/view_helper.rb', line 496 def visit_string_column(node) value = node.value.gsub('"', '\"') @s << "\"#{value}\"" end |
#visit_time_of_day_column(node) ⇒ Object
513 514 515 |
# File 'lib/rgviz_rails/view_helper.rb', line 513 def visit_time_of_day_column(node) @s << "date \"#{node.value.strftime('%H:%M:%S')}\"" end |
#visit_unary_expression(node) ⇒ Object
458 459 460 461 462 463 464 465 466 467 |
# File 'lib/rgviz_rails/view_helper.rb', line 458 def visit_unary_expression(node) if node.operator == UnaryExpression::Not @s << "not " node.operand.accept self else node.operand.accept self @s << " #{node.operator}" end false end |
#visit_where(node) ⇒ Object
361 362 363 364 365 366 |
# File 'lib/rgviz_rails/view_helper.rb', line 361 def visit_where(node) @s << "where " node.expression.accept self @s << " " false end |