csv_sjis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58ea588e6774992840e7befa0f2e4340689c1c19
4
+ data.tar.gz: 26dff29c37941908f50f91841bd9e30f0f13a948
5
+ SHA512:
6
+ metadata.gz: d049e3d9282ed5959ac773369166f57d060a234daac9add0a1f9dd482fb063db69ebc2f29241ccbb4227f79f81cf7be0f84ce729914f69169c73e8719a18a578
7
+ data.tar.gz: 01e36b4263ba48f0d540b83a12f364d62366e6d1f658afa13487bfaf1c4b13cf56a8c3d31663e998b36b92ce48a857345b37dec1dcca9227cac7d2f8c902c370
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csv_sjis.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kozo yamagata
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,49 @@
1
+ # CSVSjis
2
+
3
+ This can safely save the csv for Excel in Japanese(Shift_JIS)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'csv_sjis'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install csv_sjis
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ file_path = Pathname.new(File.dirname(__FILE__) + '/new.csv')
23
+
24
+ # default encoding: 'Shift_JIS'
25
+ CSVSjis.open(file_path, 'w') do |csv|
26
+ csv << %w(言語 都市 記号)
27
+ csv << %w(日本語 東京 -~)
28
+ csv.close
29
+ end
30
+
31
+ file_path.read # => "言語,都市,記号\n日本語,東京,-~\n"
32
+
33
+ # you can change encoding: 'UTF-8'
34
+ CSVSjis.open(file_path, 'w', encoding: 'UTF-8') do |csv|
35
+ csv << %w(言語 都市 記号)
36
+ csv << %w(日本語 東京 -~)
37
+ csv.close
38
+ end
39
+
40
+ file_path.read # => "言語,都市,記号\n日本語,東京,-~\n"
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/halenohi/csv_sjis/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csv_sjis/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csv_sjis"
8
+ spec.version = CSVSjis::VERSION
9
+ spec.authors = ["kozo yamagata"]
10
+ spec.email = ["tune002@gmail.com"]
11
+ spec.summary = %q{Generate CSV for Shift_JIS}
12
+ spec.description = %q{Generate CSV for Shift_JIS}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,54 @@
1
+ require "csv_sjis/version"
2
+ require "csv"
3
+
4
+ module CSVSjis
5
+ SJIS_NAME = 'Shift_JIS'
6
+
7
+ class << self
8
+ def open(file, mode = 'w', options = {})
9
+ options = {
10
+ encoding: SJIS_NAME,
11
+ undef: :replace,
12
+ invalid: :replace
13
+ }.merge(options)
14
+
15
+ file = File.open(file, mode, options)
16
+ csv = CSV.new(file, encoding: options[:encoding])
17
+ builder = Builder.new(csv, options[:encoding] == SJIS_NAME)
18
+
19
+ yield(builder) if block_given?
20
+
21
+ builder
22
+ end
23
+ end
24
+
25
+ class Builder < Struct.new(:csv, :is_sjis)
26
+ def <<(row)
27
+ csv << row.map{ |col|
28
+ col.is_a?(String) && is_sjis ? sjis_safe(col) : col
29
+ }
30
+ end
31
+
32
+ alias_method :push, :<<
33
+
34
+ def sjis_safe(str)
35
+ [
36
+ ["FF5E", "007E"], # wave-dash
37
+ ["FF0D", "002D"], # full-width minus
38
+ ["00A2", "FFE0"], # cent as currency
39
+ ["00A3", "FFE1"], # lb(pound) as currency
40
+ ["00AC", "FFE2"], # not in boolean algebra
41
+ ["2014", "2015"], # hyphen
42
+ ["2016", "2225"], # double vertical lines
43
+ ].inject(str) do |s, (before, after)|
44
+ s.gsub(
45
+ before.to_i(16).chr('UTF-8'),
46
+ after.to_i(16).chr('UTF-8'))
47
+ end
48
+ end
49
+
50
+ def method_missing(method, *args, &block)
51
+ csv.send(method, *args, &block)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module CSVSjis
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe CSVSjis do
5
+ let(:file_path) do
6
+ RSpec.configuration.spec_root.join('tmp/sample.csv')
7
+ end
8
+
9
+ let(:charactors) do
10
+ %w(- ~)
11
+ end
12
+
13
+ let(:source_path) do
14
+ RSpec.configuration.spec_root.join('tmp/source.csv')
15
+ end
16
+
17
+ before :each do
18
+ FileUtils.mkdir_p(File.dirname(file_path))
19
+ end
20
+
21
+ after :each do
22
+ FileUtils.rm_rf(File.dirname(file_path))
23
+ end
24
+
25
+ describe '#open' do
26
+ subject do
27
+ -> {
28
+ CSVSjis.open(file_path, 'w+') do |csv|
29
+ csv << charactors
30
+ end
31
+ }
32
+ end
33
+
34
+ it 'not raise error' do
35
+ expect(subject).to_not raise_error
36
+ end
37
+
38
+ it 'generate valid csv' do
39
+ subject.call.close
40
+ expect(file_path.read).to eq("-,~\n")
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ require 'csv_sjis'
2
+
3
+ RSpec.configure do |config|
4
+ config.add_setting :spec_root, default: Pathname.new(File.dirname(__FILE__))
5
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_sjis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kozo yamagata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Generate CSV for Shift_JIS
56
+ email:
57
+ - tune002@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - csv_sjis.gemspec
68
+ - lib/csv_sjis.rb
69
+ - lib/csv_sjis/version.rb
70
+ - spec/csv_sjis_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Generate CSV for Shift_JIS
96
+ test_files:
97
+ - spec/csv_sjis_spec.rb
98
+ - spec/spec_helper.rb