csvmapper 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Changes ADDED
@@ -0,0 +1,12 @@
1
+ 0.03
2
+ - Added CSVMapper#each method
3
+ - Added CSVMapper#before_filter method
4
+ - Included Enumerable mix-in module
5
+ - CSVMapper#load_file acceptable URI
6
+
7
+ 0.02
8
+ - fixed bug
9
+
10
+ 0.01
11
+ - First Release
12
+
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'hashie'
7
+
data/examples/yp.rb ADDED
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ require 'csvmapper'
3
+ require 'cgi'
4
+
5
+ class YPIndex < CSVMapper
6
+ before_filter do |v|
7
+ CGI.unescapeHTML(v) if v
8
+ end
9
+
10
+ delimiter '<>'
11
+ column :name, 0, :string
12
+ column :hash, 1
13
+ column :host, 2
14
+ column :contact, 3, :uri
15
+ column :genre, 4
16
+ column :description, 5
17
+ column :active_viewers, 6, :numeric
18
+ column :total_viewers, 7, :numeric
19
+ column :bitrate, 8, :numeric
20
+ column :filetype, 9
21
+ column :artist, 10
22
+ column :title, 11
23
+ column :album, 12
24
+ column :reserved, 13
25
+ column :hash2, 14
26
+ column :time, 15
27
+ column :type, 16
28
+ column :reservee, 17
29
+ column :nullfield, 18
30
+ end
31
+
32
+ csv = YPIndex.load_file("http://temp.orz.hm/yp/index.txt")
33
+ p csv.sort_by{ |e|
34
+ -e.active_viewers
35
+ }
36
+
data/lib/csvmapper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "csvmapper/version"
2
-
3
2
  require 'csv'
4
3
  require 'hashie'
5
4
  require 'json'
@@ -7,6 +6,7 @@ require 'pathname'
7
6
  require 'time'
8
7
  require 'ipaddr'
9
8
  require 'uri'
9
+ require 'open-uri'
10
10
 
11
11
  class CSVMapper
12
12
  @@data = []
@@ -14,6 +14,7 @@ class CSVMapper
14
14
  @@on_error_go_to_next_line = false
15
15
  @@has_header = nil
16
16
  @@delimiter = ','
17
+ @@before_filters = []
17
18
 
18
19
  def self.column(field, *args, &block)
19
20
  value = args.shift
@@ -30,8 +31,10 @@ class CSVMapper
30
31
  @@has_header = at
31
32
  end
32
33
 
33
- def self.load_file(path)
34
- self.load(File.read(path))
34
+ def self.load_file(path, encoding = nil)
35
+ data = open(path).read
36
+ data.force_encoding(encoding) if encoding
37
+ self.load(data)
35
38
  end
36
39
 
37
40
  def self.load(data)
@@ -51,6 +54,13 @@ class CSVMapper
51
54
  next
52
55
  end
53
56
 
57
+ line = line.map{ |item|
58
+ @@before_filters.each{ |filter|
59
+ item = filter.call(item)
60
+ }
61
+ item
62
+ }
63
+
54
64
  hash = {}
55
65
  @@data.each{ |opt|
56
66
  key = opt.keys.first
@@ -131,6 +141,10 @@ class CSVMapper
131
141
  @@delimiter = val
132
142
  end
133
143
 
144
+ def self.before_filter(&block)
145
+ @@before_filters << block
146
+ end
147
+
134
148
  def initialize(records=[])
135
149
  @records = records.map{ |hash|
136
150
  Hashie::Mash.new(hash)
@@ -145,6 +159,13 @@ class CSVMapper
145
159
  row(index)
146
160
  end
147
161
 
162
+ include Enumerable
163
+ def each(&block)
164
+ @records.each{ |record|
165
+ block.call(record)
166
+ }
167
+ end
168
+
148
169
  def to_hash
149
170
  @records.map{ |record|
150
171
  record.to_hash
@@ -1,3 +1,3 @@
1
1
  class CSVMapper
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
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.2
4
+ version: 0.0.3
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-07-31 00:00:00.000000000 Z
12
+ date: 2012-08-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: CSV to Ruby class mapper
15
15
  email:
@@ -19,6 +19,7 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
+ - Changes
22
23
  - Gemfile
23
24
  - LICENSE
24
25
  - README.md
@@ -26,6 +27,7 @@ files:
26
27
  - csvmapper.gemspec
27
28
  - examples/basic.rb
28
29
  - examples/basic2.rb
30
+ - examples/yp.rb
29
31
  - lib/csvmapper.rb
30
32
  - lib/csvmapper/version.rb
31
33
  homepage: ''