Module: Numo::Gnuplot::OptArg

Defined in:
lib/numo/gnuplot.rb

Constant Summary collapse

NEED_QUOTE_TIME =
/^timef(mt?)?/
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



476
477
478
479
480
481
482
# File 'lib/numo/gnuplot.rb', line 476

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

.parse(*opts) ⇒ Object



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
515
516
517
518
519
520
521
522
# File 'lib/numo/gnuplot.rb', line 484

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_TIME
        if opts.first.kind_of?(String)
          a << OptArg.quote_time(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
661
662
# 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_TIME
    "#{k} #{OptArg.quote_time(v)}"
  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



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

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

.quote_time(s) ⇒ Object



526
527
528
# File 'lib/numo/gnuplot.rb', line 526

def quote_time(s)
  "'#{s}'"
end