Class: PodArray
- Inherits:
-
Array
- Object
- Array
- PodArray
- Defined in:
- lib/podcsv.rb
Instance Attribute Summary collapse
-
#_cache ⇒ Object
readonly
Returns the value of attribute _cache.
-
#_lazy_parser ⇒ Object
Returns the value of attribute _lazy_parser.
-
#_lazy_parser_default ⇒ Object
readonly
Returns the value of attribute _lazy_parser_default.
Instance Method Summary collapse
- #[](args) ⇒ Object
- #cached?(i) ⇒ Boolean
- #each ⇒ Object
- #first ⇒ Object
- #index(x) ⇒ Object
-
#initialize(args) ⇒ PodArray
constructor
PARSER_DEFAULT = lambda{|s,opt| CSV.parse_line(s,opt)} PARSER_DEFAULT = lambda{|s| CSV.parse_line(s,@opt)} PARSER_DEFAULT = lambda{|s| CSV.parse_line(s,@opt.to_h)}.
- #last ⇒ Object
- #map ⇒ Object
- #reverse ⇒ Object
- #select ⇒ Object
Constructor Details
#initialize(args) ⇒ PodArray
PARSER_DEFAULT = lambda{|s,opt| CSV.parse_line(s,opt)} PARSER_DEFAULT = lambda{|s| CSV.parse_line(s,@opt)} PARSER_DEFAULT = lambda{|s| CSV.parse_line(s,@opt.to_h)}
13 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 |
# File 'lib/podcsv.rb', line 13 def initialize(args) super # we can also distinguish which are cached by the type of elements # (String or not). # But the advantage of having cache structure is that # we can know which are the cached elements without traversing # whole array. # @_cache = {} if args.class == self.class @_cache = args._cache end # opt for CSV.parse. # default is {}, which means that dont change the default behaviour # of CSV.parse_line. #@opt = {} @_lazy_parser_default = lambda{|s| CSV.parse_line(s)} # # # #@_lazy_parser = lambda{|x,o| CSV.parse(x,o).first} @_lazy_parser = @_lazy_parser_default end |
Instance Attribute Details
#_cache ⇒ Object (readonly)
Returns the value of attribute _cache.
44 45 46 |
# File 'lib/podcsv.rb', line 44 def _cache @_cache end |
#_lazy_parser ⇒ Object
Returns the value of attribute _lazy_parser.
46 47 48 |
# File 'lib/podcsv.rb', line 46 def _lazy_parser @_lazy_parser end |
#_lazy_parser_default ⇒ Object (readonly)
Returns the value of attribute _lazy_parser_default.
45 46 47 |
# File 'lib/podcsv.rb', line 45 def _lazy_parser_default @_lazy_parser_default end |
Instance Method Details
#[](args) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/podcsv.rb', line 50 def [](args) ret = nil # . if args.is_a?(Integer) tmp = super # cache function. if self.cached?(args) # do nothing. else #tmp = super #$stderr.puts "*tmp (class:#{tmp.class}):#{tmp}" if tmp.class == String #@_cache[args] = @_lazy_parser.call(tmp, @opt) @_cache[args] = @_lazy_parser.call(tmp) self[args] = @_cache[args] else @_cache[args] = tmp unless self.cached?(args) end end #$stderr.puts "_cache[#{args}]: #{@_cache[args]}" @_cache[args] # return the value of self[args]. #self[args] #self[args] = @_cache[args] elsif args.is_a?(Range) # Range, etc. # $stderr.puts "[INFO] access for #{args} (#{args.class})." # copy partial array. #idxs = Array(args) #pary = idxs.map{|ii| self[ii]} b = args.begin e = args.end e += self.size if args.max.nil? rr = unless args.exclude_end? b..e else b...e end #$stderr.puts "{#{self.class}##{__method__}} args: #{args} / rr: #{rr}" pary = super(args) # `self.class` for derived classes. ret = self.class.new( # Integer pary ) # $stderr.puts "{#{self.class}##{__method__}}"+ # " @_cache in ret: #{ret.instance_eval{@_cache}}"+ # " (#{ret.instance_eval{@_cache}.class})}" # copy partial cache. ret.instance_eval{ @_cache = {} } _cache.keys.each do |k| tmp_k = (k>=0)? k : self.size+k val = _cache[k] # $stderr.puts "{#{self.class}##{__method__}}"+ # " k: #{k}, tmp_k: #{tmp_k} val: #{val} (#{_cache[k]})" if rr.include? tmp_k new_k = k - b ret.instance_eval{ @_cache[new_k] = val } end end ret else raise "Not yet implemented for #{args.class}" end end |
#cached?(i) ⇒ Boolean
41 42 43 |
# File 'lib/podcsv.rb', line 41 def cached?(i) not(@_cache[i].nil?) end |
#each ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/podcsv.rb', line 154 def each s = self a = Array(s.to_a) # cast to Array. #$stderr.puts "s: #{s.class}" #$stderr.puts "a: #{a.class}" #pp a # do cache. a.each.with_index {|_,i| # $stderr.puts "i: #{i}" self[i] # do cache. yield(self[i]) if block_given? } # return if block_given? self else self.to_enum end end |
#first ⇒ Object
133 134 135 136 137 |
# File 'lib/podcsv.rb', line 133 def first #$stderr.puts "#{self.class}##{__method__} ()" # self.index(0) self[0] end |
#index(x) ⇒ Object
128 129 130 131 |
# File 'lib/podcsv.rb', line 128 def index(x) #$stderr.puts "#{self.class}##{__method__} (#{x.class})" self[x] end |
#last ⇒ Object
139 140 141 142 |
# File 'lib/podcsv.rb', line 139 def last #$stderr.puts "#{self.class}##{__method__} ()" self[self.size-1] end |
#map ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/podcsv.rb', line 177 def map tmp = self.each ret = [] tmp.each{|e| ret << yield(e) if block_given? } # return if block_given? ret else tmp end end |
#reverse ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/podcsv.rb', line 144 def reverse #$stderr.puts "#{self.class}##{__method__} ()" ret = self.class.new(super) #$stderr.puts "#{self.class}##{__method__} ret: #{ret.class}" #$stderr.puts "#{self.class}##{__method__} ret.first: #{ret.first.class}" ret end |
#select ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/podcsv.rb', line 194 def select tmp = self.each ret = [] tmp.each{|e| # $stderr.puts "#{e} (#{e.class})" if block_given? ret << e if yield(e) end } # return if block_given? self.class.new(ret) else tmp end end |