Class: PodArray

Inherits:
Array
  • Object
show all
Defined in:
lib/podcsv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#_cacheObject (readonly)

Returns the value of attribute _cache.



44
45
46
# File 'lib/podcsv.rb', line 44

def _cache
  @_cache
end

#_lazy_parserObject

Returns the value of attribute _lazy_parser.



46
47
48
# File 'lib/podcsv.rb', line 46

def _lazy_parser
  @_lazy_parser
end

#_lazy_parser_defaultObject (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
# 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]
  else
    # Range, etc.
    # $stderr.puts "[INFO] access for #{args} (#{args.class})."

    # copy partial array.
    idxs = Array(args)
    pary = idxs.map{|ii| self[ii]}
    ret = PodArray.new(
      # Integer
      pary
    )

    # copy partial cache.
    ini_cache = self._cache
    idxs.each {|ii|
      ret.instance_eval{ @_cache[ii] = ini_cache[ii] }
    }

    ret
  end
end

#cached?(i) ⇒ Boolean



41
42
43
# File 'lib/podcsv.rb', line 41

def cached?(i)
  not(@_cache[i].nil?)
end

#eachObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/podcsv.rb', line 128

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

#firstObject



107
108
109
110
111
# File 'lib/podcsv.rb', line 107

def first
  #$stderr.puts "#{self.class}##{__method__} ()"
  # self.index(0)
  self[0]
end

#index(x) ⇒ Object



102
103
104
105
# File 'lib/podcsv.rb', line 102

def index(x)
  #$stderr.puts "#{self.class}##{__method__} (#{x.class})"
  self[x]
end

#lastObject



113
114
115
116
# File 'lib/podcsv.rb', line 113

def last
  #$stderr.puts "#{self.class}##{__method__} ()"
  self[self.size-1]
end

#mapObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/podcsv.rb', line 151

def map
  tmp = self.each
  ret = []

  tmp.each{|e|
    ret << yield(e) if block_given?
  }

  # return
  if block_given?
    ret
  else
    tmp
  end

end

#reverseObject



118
119
120
121
122
123
124
125
# File 'lib/podcsv.rb', line 118

def reverse
  #$stderr.puts "#{self.class}##{__method__} ()"
  ret = PodArray.new(super)
  #$stderr.puts "#{self.class}##{__method__} ret: #{ret.class}"
  #$stderr.puts "#{self.class}##{__method__} ret.first: #{ret.first.class}"

  ret
end

#selectObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/podcsv.rb', line 168

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?
    PodArray.new(ret)
  else
    tmp
  end

end