Class: Hash

Inherits:
Object show all
Defined in:
lib/rwd/rwd.rb,
lib/rwd/ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.file(file) ⇒ Object



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/rwd/ruby.rb', line 661

def self.file(file)
  res	= new

  File.open(file) do |f|
    #f.readlines.chomp.each do |line|
    while line = f.gets do
      line.chomp!

      unless line.empty?
        k, v	= line.split(/\s*=\s*/, 2)
        res[k]	= v
      end
    end
  end

  res
end

Instance Method Details

#idsObject



679
680
681
# File 'lib/rwd/ruby.rb', line 679

def ids
  collect{|k, v| [k, v].ids}
end

#rwd_table(field = nil, joinwith = nil, headers = nil) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rwd/rwd.rb', line 209

def rwd_table(field=nil, joinwith=nil, headers=nil)
  res	= []

  res << "<table>"
  res << headers.rwd_headers((not field.nil?))	if not headers.nil?
  self.keys.numsort.each do |key|
    key2	= key
    value2	= self[key]

    key2	= key2.join(joinwith)	if key2.kind_of?(Array)
    value2	= [value2]		if value2.kind_of?(String)

    res << value2.rwd_row(field, key2)
  end
  res << "</table>"

  res.join("\n")
end

#save(file, append = false) ⇒ Object



583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/rwd/ruby.rb', line 583

def save(file, append=false)
  org	= {}
  org	= Hash.file(file)	if (append and File.file?(file))

  self.sort.each do |k, v|
    org[k]	= v
  end

  File.open(file, "w") do |f|
    org.sort.each do |k, v|
      f.puts "%s\t= %s" % [k, v]
    end
  end
end

#subset(fields, values, results = nil, exact = true, emptyline = nil, joinwith = nil) ⇒ Object



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
# File 'lib/rwd/ruby.rb', line 598

def subset(fields, values, results=nil, exact=true, emptyline=nil, joinwith=nil)
  fields	= [fields]		unless fields.kind_of? Array
  values	= [values]		unless values.kind_of? Array
  results	= [results]		unless results.kind_of? Array
  emptyline	= emptyline.downcase	unless emptyline.nil?
  res		= self.dup
  res.delete_if {true}

  self.each do |k, l|
    ok	= true

    case l.class.to_s
    when "String"
      c		= l.splitwords
      correction	= 1
      joinwith	= " "	if joinwith.nil?
    when "Array"
      c		= l
      correction	= 0
    end

    #catch :stop do
      values2	= values.dup
      fields.each do |f|
        v	= values2.shift
        v	= v.downcase	unless v.nil?
        if emptyline.nil? or (not v == emptyline)
          if exact
            unless (v.nil? or c[f-correction].downcase == v)
              ok	= false
              #throw :stop
            end
          else
            unless (v.nil? or c[f-correction].downcase.include?(v))
              ok	= false
              #throw :stop
            end
          end
        end
      end
    #end

    if ok
      res2	= []
      if results == [nil]
        res2	= c
      else
        results.each do |n|
          res2 << c[n-correction]
        end
      end
      res2	= res2.join(joinwith)	unless joinwith.nil?
      res[k]	= res2
    end
  end

  return res
end

#to_iObject



657
658
659
# File 'lib/rwd/ruby.rb', line 657

def to_i
  collect{|k, v| v.to_i}
end