Module: Numo::Gnuplot::OptArg

Defined in:
lib/numo/gnuplot.rb

Constant Summary collapse

NEED_QUOTE =
%w[
  background
  cblabel
  clabel
  commentschars
  dashtype
  decimalsign
  file
  fontpath
  format
  locale
  logfile
  missing
  name
  newhistogram
  output
  prefix
  print
  rgb
  separator
  table
  timefmt
  title
  x2label
  xlabel
  y2label
  ylabel
  zlabel
  xy
]
NONEED_QUOTE =
%w[
  for
  log
  smooth
]

Class Method Summary collapse

Class Method Details

.from_symbol(s) ⇒ Object


472
473
474
475
476
477
478
# File 'lib/numo/gnuplot.rb', line 472

def from_symbol(s)
  s = s.to_s
  if /^(.*)_(noquote|nq)$/ =~ s
    s = $1
  end
  s.tr('_',' ')
end

.parse(*opts) ⇒ Object


480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/numo/gnuplot.rb', line 480

def parse(*opts)
  sep = ","
  a = []
  while opt = opts.shift
    if !opt.kind_of?(Numeric)
      sep = " "
    end
    case opt
    when Symbol
      a << from_symbol(opt)
      case opt
      when :label
        if opts.first.kind_of?(Integer)
          a << opts.shift.to_s
        end
        if opts.first.kind_of?(String)
          a << OptArg.quote(opts.shift)
        end
      when NEED_QUOTE
        if opts.first.kind_of?(String)
          a << OptArg.quote(opts.shift)
        end
      end
    when Array
      a << parse(*opt)
    when Hash
      a << opt.map{|k,v| parse_kv(k,v)}.compact.join(" ")
    when Range
      a << "[#{opt.begin}:#{opt.end}]"
    else
      a << opt.to_s
    end
  end
  a.join(sep)
end

.parse_kv(s, v) ⇒ Object


584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/numo/gnuplot.rb', line 584

def parse_kv(s,v)
  k = from_symbol(s)
  case s.to_sym
  when :at
    case v
    when String
      "#{k} #{v}" # not quote
    when Array
      "#{k} #{v.map{|x| x.to_s}.join(",")}"
    else
      "#{k} #{parse(v)}"
    end
  when :label
    case v
    when String
      "#{k} #{OptArg.quote(v)}"
    when Array
      if v[0].kind_of?(Integer) && v[1].kind_of?(String)
        "#{k} #{parse(v[0],OptArg.quote(v[1]),*v[2..-1])}"
      elsif v[0].kind_of?(String)
        "#{k} #{parse(OptArg.quote(v[0]),*v[1..-1])}"
      else
        "#{k} #{parse(*v)}"
      end
    when TrueClass
      k
    else
      "#{k} #{parse(v)}"
    end
  when NEED_QUOTE
    case v
    when String
      "#{k} #{OptArg.quote(v)}"
    when TrueClass
      k
    when NilClass
      nil
    when FalseClass
      nil
    when Array
      case v[0]
      when String
        if v.size == 1
          "#{k} #{OptArg.quote(v[0])}"
        else
          "#{k} #{OptArg.quote(v[0])} #{parse(*v[1..-1])}"
        end
      else
        "#{k} #{parse(*v)}"
      end
    else
      "#{k} #{parse(v)}"
    end
  else
    case v
    when String
      "#{k} #{v}"
    when TrueClass
      k
    when NilClass
      nil
    when FalseClass
      nil
    when Array
      re = /^#{k}/
      if re =~ "using" || re =~ "every"
        "#{k} #{v.join(':')}"
      elsif v.empty?
        k
      else
        "#{k} #{parse(*v)}"
      end
    else
      "#{k} #{parse(v)}"
    end
  end
end

.quote(s) ⇒ Object


559
560
561
562
563
564
565
566
567
568
569
# File 'lib/numo/gnuplot.rb', line 559

def quote(s)
  case s
  when String
    if /^'(.*)'$/ =~ s || /^"(.*)"$/ =~ s
      s = $1
    end
    s.inspect
  else
    s
  end
end

.squote(s) ⇒ Object


571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/numo/gnuplot.rb', line 571

def squote(s)
  case s
  when String
    if /^'.*'$/ =~ s || /^".*"$/ =~ s
      s
    else
      "'#{s}'"
    end
  else
    s
  end
end