csvmapper 0.0.1

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csvmapper.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 kimoto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # CSVMapper
2
+
3
+ CSV to Ruby Model
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'csvmapper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install csvmapper
18
+
19
+ ## Usage
20
+
21
+ class MyCSV < CSVMapper
22
+ column :name, 0, :integer
23
+ column :ipaddr, 1, :ipaddr
24
+ column :file_path, 2, :pathname
25
+ column :time, 3, :time
26
+ end
27
+
28
+ p MyCSV.load("name1,127.0.0.1,./tmp/path/to/file.txt,2012/01/01")
29
+ # =>
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/csvmapper/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["kimoto"]
6
+ gem.email = ["sub+peerler@gmail.com"]
7
+ gem.description = %q{CSV to Ruby class mapper}
8
+ gem.summary = %q{CSV to Ruby class mapper}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "csvmapper"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = CSVMapper::VERSION
17
+ end
@@ -0,0 +1,30 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+ # Author: kimoto
4
+ require 'csvmapper'
5
+
6
+ class MyCSV < CSVMapper
7
+ ignore_header_line
8
+ on_error_go_to_next_line
9
+ delimiter ','
10
+
11
+ column :name, 0, :string, &:upcase
12
+ column :age, 1, :numeric
13
+ column :path, 2, :pathname, &:to_s
14
+ column :time, 3, :time
15
+ column :epoch, 4, :unixtime
16
+ column :date, 5, :date
17
+ column :bool, 6, :boolean
18
+ column :ip, 7, :ipaddr
19
+ column :uri, 8, :uri
20
+ column :regexp, 9, :regexp
21
+ end
22
+
23
+ csv_data = <<EOT
24
+ name,age,path,time,epoch,date,bool,ip,uri,regexp
25
+ kimoto,19,/home/kimoto/test.rb,2012-12-24 00:00:00,1343714611,2012-01-01,true,127.0.0.1,http://www.google.co.jp,a-zA-Z
26
+ takumi,24,/var/lib/tmp,2012/01/01 12:34:56,1343714611,2012/01/02,false,10.0.0.1,ftp://localhost/,^[0-9]+$
27
+ hebo,33,/cygdrive/c/test.txt,,1343714611,2012/01/03,false,10.0.0.2,steam://127.0.0.1/connect,[12345]
28
+ EOT
29
+
30
+ p MyCSV.load(csv_data)
@@ -0,0 +1,15 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+ # Author: kimoto
4
+
5
+ require 'csvmapper'
6
+ class MyCSV < CSVMapper
7
+ column :name, 0, :string
8
+ column :ipaddr, 1, :ipaddr
9
+ column :file_path, 2, :pathname
10
+ column :time, 3, :time
11
+ end
12
+
13
+ puts MyCSV.load("name1,127.0.0.1,./tmp/path/to/file.txt,2012/01/01").to_json
14
+ # => [{"name":"name1","ipaddr":"127.0.0.1","file_path":"./tmp/path/to/file.txt","time":"2012-01-01 00:00:00 +0900"}]
15
+
@@ -0,0 +1,158 @@
1
+ require "csvmapper/version"
2
+
3
+ require 'csv'
4
+ require 'hashie'
5
+ require 'json'
6
+ require 'pathname'
7
+ require 'time'
8
+ require 'ipaddr'
9
+ require 'uri'
10
+
11
+ class CSVMapper
12
+ @@data = []
13
+ @@ignore_header_line = false
14
+ @@on_error_go_to_next_line = false
15
+ @@has_header = nil
16
+ @@delimiter = ','
17
+
18
+ def self.column(field, *args, &block)
19
+ value = args.shift
20
+ #value = block.call(value) if block_given?
21
+ record = {
22
+ field => value,
23
+ :options => args,
24
+ :filter => block
25
+ }
26
+ @@data << record
27
+ end
28
+
29
+ def self.has_header(at=0)
30
+ @@has_header = at
31
+ end
32
+
33
+ def self.load_file(path)
34
+ self.load(File.read(path))
35
+ end
36
+
37
+ def self.load(data)
38
+ buf = CSV.parse(data, :col_sep => @@delimiter)
39
+ if @@ignore_header_line
40
+ @@ignore_header_line_count.times{
41
+ buf.shift()
42
+ }
43
+ end
44
+
45
+ records = []
46
+ buf.each_with_index{ |line, index|
47
+ if @@has_header == index
48
+ line.each_with_index{ |name, i|
49
+ self.column(name.gsub(" ", "_").downcase, i, :string)
50
+ }
51
+ next
52
+ end
53
+
54
+ hash = {}
55
+ @@data.each{ |opt|
56
+ key = opt.keys.first
57
+ options = opt[:options]
58
+
59
+ if options.empty?
60
+ index = opt[key]
61
+ hash[key] = line[index]
62
+ else
63
+ klass = options.first
64
+ index = opt[key]
65
+
66
+ if line[index].nil?
67
+ hash[key] = nil
68
+ next
69
+ end
70
+
71
+ begin
72
+ case klass
73
+ when :string
74
+ hash[key] = line[index].to_s
75
+ when :numeric
76
+ hash[key] = line[index].to_i
77
+ when :pathname
78
+ hash[key] = Pathname.new(line[index])
79
+ when :time
80
+ hash[key] = Time.parse(line[index])
81
+ when :unixtime
82
+ hash[key] = Time.at(line[index].to_i)
83
+ when :date
84
+ hash[key] = Date.parse(line[index])
85
+ when :boolean
86
+ hash[key] = line[index].to_s == "true" ? true : false
87
+ when :ipaddr
88
+ hash[key] = IPAddr.new(line[index])
89
+ when :uri
90
+ hash[key] = URI.parse(line[index])
91
+ when :regexp
92
+ hash[key] = Regexp.compile(line[index])
93
+ else
94
+ raise "not implemented yet"
95
+ end
96
+ rescue => ex
97
+ puts ex
98
+ puts ex.backtrace.join($/)
99
+
100
+ if @@on_error_go_to_next_line
101
+ puts "========"
102
+ puts "Ignore error!!"
103
+ puts "========"
104
+ next
105
+ else
106
+ raise ex
107
+ end
108
+ end
109
+
110
+ ## filterは最後に適用
111
+ if opt[:filter]
112
+ hash[key] = opt[:filter].call(hash[key])
113
+ end
114
+ end
115
+ }
116
+ records << hash
117
+ }
118
+ self.new(records)
119
+ end
120
+
121
+ def self.ignore_header_line(line_count=1)
122
+ @@ignore_header_line = true
123
+ @@ignore_header_line_count = line_count
124
+ end
125
+
126
+ def self.on_error_go_to_next_line
127
+ @@on_error_go_to_next_line = true
128
+ end
129
+
130
+ def self.delimiter(val)
131
+ @@delimiter = val
132
+ end
133
+
134
+ def initialize(records=[])
135
+ @records = records.map{ |hash|
136
+ Hashie::Mash.new(hash)
137
+ }
138
+ end
139
+
140
+ def row(index)
141
+ @records[index]
142
+ end
143
+
144
+ def [](index)
145
+ row(index)
146
+ end
147
+
148
+ def to_hash
149
+ @records.map{ |record|
150
+ record.to_hash
151
+ }
152
+ end
153
+
154
+ def to_json
155
+ @records.to_json
156
+ end
157
+ end
158
+
@@ -0,0 +1,3 @@
1
+ class CSVMapper
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csvmapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - kimoto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: CSV to Ruby class mapper
15
+ email:
16
+ - sub+peerler@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - csvmapper.gemspec
27
+ - examples/basic.rb
28
+ - examples/basic2.rb
29
+ - lib/csvmapper.rb
30
+ - lib/csvmapper/version.rb
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: CSV to Ruby class mapper
55
+ test_files: []
56
+ has_rdoc: