Class: CoSVON
- Inherits:
-
Object
- Object
- CoSVON
- Defined in:
- lib/cosvon.rb
Overview
CoSVON: Comma Separated Value Object Notation (yokohamarb)
Constant Summary collapse
- HSALSKCAB =
'―ソЫ噂浬欺圭構蚕十申曾箪貼能表暴予禄兔喀媾彌拿杤歃濬畚秉綵臀藹觸軆鐔饅鷭偆砡'- VERSION =
VERSION string
'0.0.0.3'
Class Method Summary collapse
-
.csv(s, __opt = Hash.new) ⇒ Object
parses csv string into 2D array.
-
.generate(h) ⇒ Object
generates CoSVON string from Hash.
-
.load(path, parser = self.method(:csv)) ⇒ Object
parses CoSVON file into Hash.
-
.parse(s, parser = self.method(:csv)) ⇒ Object
parses CoSVON string into Hash.
-
.save(h, path) ⇒ Object
generates CoSVON file from Hash.
-
.stringify(h) ⇒ Object
kind-of-alias of self.generate.
Class Method Details
.csv(s, __opt = Hash.new) ⇒ Object
parses csv string into 2D array. quoted commas/LFs and escaped quotations are supported.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cosvon.rb', line 14 def self.csv(s,__opt=Hash.new) opt={:col_sep=>',',:quote_char=>'"'}.merge(__opt) csv=[] line=[] quoted=false quote=false backslash=0 cur='' linebreak=nil s.each_char{|c| #if c=="\r" #ignore CR if c==opt[:quote_char] if !quoted #start of quote quoted=true elsif !quote #end of quote? Let's determine using next char quote=true if backslash==1 backslash=2 end else #escape rather than end of quote quote=false cur<<opt[:quote_char] if backslash==2 backslash=0 end end else if quote quote=false if backslash==2 cur<<opt[:quote_char] else quoted=false end end if (c=="\n"||c=="\r")&&!quoted if !linebreak||linebreak==c line<<cur cur='' csv<<line line=[] linebreak=c end elsif c==opt[:col_sep]&&!quoted line<<cur cur='' else backslash=0 if HSALSKCAB.include?(c) backslash=1 end quote=false cur<<c end end } line<<cur if !cur.empty? csv<<line if !line.empty? csv end |
.generate(h) ⇒ Object
generates CoSVON string from Hash.
92 93 94 95 96 97 98 |
# File 'lib/cosvon.rb', line 92 def self.generate(h) s="CoSVON:0.1\n" h.each{|k,v| s+=%Q("#{k.gsub('"','""')}","#{v.gsub('"','""')}"\n) } s end |
.load(path, parser = self.method(:csv)) ⇒ Object
parses CoSVON file into Hash.
88 89 90 |
# File 'lib/cosvon.rb', line 88 def self.load(path,parser=self.method(:csv)) self.parse(File.read(path),parser) end |
.parse(s, parser = self.method(:csv)) ⇒ Object
parses CoSVON string into Hash. parser can be CoSVON.method(:csv), CSV.method(:parse), etc…
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cosvon.rb', line 77 def self.parse(s,parser=self.method(:csv)) csv=parser[s] return nil if csv.empty?||csv[0].empty?||csv[0][0]!='CoSVON:0.1' csv.shift h={} csv.each{|e| h[e[0]]=e[1] if e.size>1&&e[0]&&!e[0].empty?&&e[1]&&!e[1].empty? } h end |
.save(h, path) ⇒ Object
generates CoSVON file from Hash.
104 105 106 |
# File 'lib/cosvon.rb', line 104 def self.save(h,path) File.write(path,generate(h)) end |
.stringify(h) ⇒ Object
kind-of-alias of self.generate
100 101 102 |
# File 'lib/cosvon.rb', line 100 def self.stringify(h) self.generate(h) end |