csvmapper 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Changes CHANGED
@@ -1,3 +1,11 @@
1
+ 0.04
2
+ - Added #columns method
3
+ columns :first_name, :last_name, :age
4
+ - Added #grouped_by method
5
+ grouped by field key
6
+ - Added #where method
7
+ arel style finder (testing)
8
+
1
9
  0.03
2
10
  - Added CSVMapper#each method
3
11
  - Added CSVMapper#before_filter method
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ require 'csvmapper'
3
+ require 'cgi'
4
+
5
+ class OmgCSV < CSVMapper
6
+ columns :first_name, :last_name, :age
7
+ end
8
+
9
+ csv = OmgCSV.load <<EOT
10
+ takumi,kimoto,99
11
+ super,urutoraman,11
12
+ EOT
13
+
14
+ p csv.map(&:age)
15
+ # => ["99", "11"]
16
+
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require 'csvmapper'
3
+ require 'cgi'
4
+
5
+ class MyCSV < CSVMapper
6
+ end
7
+
8
+ MyCSV.before_filter do |v|
9
+ CGI.unescapeHTML(v) if v
10
+ end
11
+
12
+ MyCSV.delimiter '<>'
13
+ MyCSV.column :name, 0, :string
14
+ MyCSV.column :hash, 1
15
+ MyCSV.column :host, 2
16
+ MyCSV.column :contact, 3, :uri
17
+ MyCSV.column :genre, 4
18
+ MyCSV.column :description, 5
19
+ MyCSV.column :active_viewers, 6, :numeric
20
+ MyCSV.column :total_viewers, 7, :numeric
21
+
22
+ csv = MyCSV.load_file("http://temp.orz.hm/yp/index.txt")
23
+ p csv
24
+
@@ -34,3 +34,5 @@ p csv.sort_by{ |e|
34
34
  -e.active_viewers
35
35
  }
36
36
 
37
+ puts csv.to_s
38
+
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require 'csvmapper'
3
+ require 'cgi'
4
+ require 'hashie'
5
+
6
+ class YPIndex < CSVMapper
7
+ before_filter do |v|
8
+ CGI.unescapeHTML(v) if v
9
+ end
10
+
11
+ delimiter '<>'
12
+ column :name, 0, :string
13
+ column :hash, 1
14
+ column :host, 2
15
+ column :contact, 3, :uri
16
+ column :genre, 4
17
+ column :description, 5
18
+ column :active_viewers, 6, :numeric
19
+ column :total_viewers, 7, :numeric
20
+ column :bitrate, 8, :numeric
21
+ column :filetype, 9
22
+ column :artist, 10
23
+ column :title, 11
24
+ column :album, 12
25
+ column :reserved, 13
26
+ column :hash2, 14
27
+ column :time, 15
28
+ column :type, 16
29
+ column :reservee, 17
30
+ column :nullfield, 18
31
+ end
32
+
33
+ p YPIndex.load_file("http://temp.orz.hm/yp/index.txt").where(:name => 'あくえり').where(:genre => 'DQ3 SFC').first
34
+
35
+
@@ -9,7 +9,7 @@ require 'uri'
9
9
  require 'open-uri'
10
10
 
11
11
  class CSVMapper
12
- @@data = []
12
+ @@schema = []
13
13
  @@ignore_header_line = false
14
14
  @@on_error_go_to_next_line = false
15
15
  @@has_header = nil
@@ -18,13 +18,18 @@ class CSVMapper
18
18
 
19
19
  def self.column(field, *args, &block)
20
20
  value = args.shift
21
- #value = block.call(value) if block_given?
22
21
  record = {
23
22
  field => value,
24
23
  :options => args,
25
24
  :filter => block
26
25
  }
27
- @@data << record
26
+ @@schema << record
27
+ end
28
+
29
+ def self.columns(*fields)
30
+ fields.each_with_index{ |field, index|
31
+ self.column(field, index, :string)
32
+ }
28
33
  end
29
34
 
30
35
  def self.has_header(at=0)
@@ -47,6 +52,10 @@ class CSVMapper
47
52
 
48
53
  records = []
49
54
  buf.each_with_index{ |line, index|
55
+ if line.empty?
56
+ next
57
+ end
58
+
50
59
  if @@has_header == index
51
60
  line.each_with_index{ |name, i|
52
61
  self.column(name.gsub(" ", "_").downcase, i, :string)
@@ -62,7 +71,7 @@ class CSVMapper
62
71
  }
63
72
 
64
73
  hash = {}
65
- @@data.each{ |opt|
74
+ @@schema.each{ |opt|
66
75
  key = opt.keys.first
67
76
  options = opt[:options]
68
77
 
@@ -155,17 +164,56 @@ class CSVMapper
155
164
  @records[index]
156
165
  end
157
166
 
167
+ ## size
168
+ def rows
169
+ @records.size
170
+ end
171
+
172
+ def cols
173
+ if @records.nil? or @records.empty?
174
+ return 0
175
+ else
176
+ return @records.first.size
177
+ end
178
+ end
179
+
158
180
  def [](index)
159
181
  row(index)
160
182
  end
161
183
 
162
184
  include Enumerable
163
185
  def each(&block)
164
- @records.each{ |record|
165
- block.call(record)
186
+ matched = []
187
+ if @where
188
+ @records.each{ |record|
189
+ @where.each{ |key, value|
190
+ if record.send(key) == value
191
+ matched << record
192
+ end
193
+ }
194
+ }
195
+ else
196
+ matched = @records
197
+ end
198
+
199
+ matched.each{ |e|
200
+ block.call(e)
166
201
  }
167
202
  end
168
203
 
204
+ ## arel style finder
205
+ def where(options={})
206
+ @where = Hashie::Clash.new(options) if @where.nil?
207
+ @where.merge! options
208
+ self
209
+ end
210
+
211
+ def order(options={})
212
+ @order = Hashie::Clash.new(options) if @order.nil?
213
+ @order.merge! options
214
+ self
215
+ end
216
+
169
217
  def to_hash
170
218
  @records.map{ |record|
171
219
  record.to_hash
@@ -175,5 +223,15 @@ class CSVMapper
175
223
  def to_json
176
224
  @records.to_json
177
225
  end
226
+
227
+ def grouped_by(field)
228
+ hash = {}
229
+ each{|e|
230
+ k = e[field]
231
+ hash[k] ||= []
232
+ hash[k] << e
233
+ }
234
+ hash
235
+ end
178
236
  end
179
237
 
@@ -1,3 +1,3 @@
1
1
  class CSVMapper
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvmapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-01 00:00:00.000000000 Z
12
+ date: 2012-08-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: CSV to Ruby class mapper
15
15
  email:
@@ -27,7 +27,10 @@ files:
27
27
  - csvmapper.gemspec
28
28
  - examples/basic.rb
29
29
  - examples/basic2.rb
30
+ - examples/columns.rb
31
+ - examples/no_dsl.rb
30
32
  - examples/yp.rb
33
+ - examples/yp_find.rb
31
34
  - lib/csvmapper.rb
32
35
  - lib/csvmapper/version.rb
33
36
  homepage: ''