wrap_excel 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -2,7 +2,7 @@
2
2
  # A sample Guardfile
3
3
  # More info at https://github.com/guard/guard#readme
4
4
 
5
- guard 'rspec', :version => 2 do
5
+ guard 'rspec', cli: "--color", all_after_pass: false, all_on_start: false do
6
6
  watch(%r{^spec/.+_spec\.rb$})
7
7
  watch(%r{^lib/wrap_excel/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
8
8
  watch('lib/wrap_excel.rb') { "spec" }
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 tomi
1
+ Copyright (c) 2011-2013 tomi
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
@@ -85,6 +85,15 @@ rangeオブジェクトからのアクセス。
85
85
  book.save
86
86
  book.close
87
87
 
88
+ 別のファイル名を保存することができます。
89
+
90
+ WrapExcel::Book.open('./sample.xls', :read_only => false) do |book|
91
+ # do something
92
+ book.save './another_file.xls'
93
+ end
94
+
95
+ another_file.xlsに保存します。
96
+
88
97
  新規ファイルの保存は出来ません。
89
98
 
90
99
  === Want to do more things
@@ -85,6 +85,15 @@ or
85
85
  book.save
86
86
  book.close
87
87
 
88
+ Can save an another file name.
89
+
90
+ WrapExcel::Book.open('./sample.xls', :read_only => false) do |book|
91
+ # do something
92
+ book.save './another_file.xls'
93
+ end
94
+
95
+ Save to another_file.xls
96
+
88
97
  Can not save new file.
89
98
 
90
99
  === Want to do more things
@@ -4,19 +4,27 @@ module WrapExcel
4
4
  class Book
5
5
  attr_reader :book
6
6
 
7
+ class << self
8
+ def open(file, options={ }, &block)
9
+ new(file, options, &block)
10
+ end
11
+ end
12
+
7
13
  def initialize(file, options={ }, &block)
14
+ unless caller[1] =~ /book.rb:\d+:in\s+`open'$/
15
+ warn "DEPRECATION WARNING: WrapExcel::Book.new and WrapExcel::Book.open will be split. If you open existing file, please use WrapExcel::Book.open.(call from #{caller[1]})"
16
+ end
17
+
8
18
  @options = {
9
19
  :read_only => true,
10
20
  :displayalerts => false,
11
21
  :visible => false
12
22
  }.merge(options)
13
- file = WrapExcel::Cygwin.cygpath('-w', file) if RUBY_PLATFORM =~ /cygwin/
14
- file = WIN32OLE.new('Scripting.FileSystemObject').GetAbsolutePathName(file)
15
23
  @winapp = WIN32OLE.new('Excel.Application')
16
24
  @winapp.DisplayAlerts = @options[:displayalerts]
17
25
  @winapp.Visible = @options[:visible]
18
26
  WIN32OLE.const_load(@winapp, WrapExcel) unless WrapExcel.const_defined?(:CONSTANTS)
19
- @book = @winapp.Workbooks.Open(file,{ 'ReadOnly' => @options[:read_only] })
27
+ @book = @winapp.Workbooks.Open(absolute_path(file),{ 'ReadOnly' => @options[:read_only] })
20
28
 
21
29
  if block
22
30
  begin
@@ -34,9 +42,20 @@ module WrapExcel
34
42
  @winapp.Quit
35
43
  end
36
44
 
37
- def save
45
+ def save(file = nil)
38
46
  raise IOError, "Not opened for writing(open with :read_only option)" if @options[:read_only]
39
- @book.save
47
+ return @book.save unless file
48
+
49
+ dirname, basename = File.split(file)
50
+ extname = File.extname(basename)
51
+ basename = File.basename(basename)
52
+ case extname
53
+ when '.xls'
54
+ file_format = WrapExcel::XlExcel8
55
+ when '.xlsx'
56
+ file_format = WrapExcel::XlOpenXMLWorkbook
57
+ end
58
+ @book.SaveAs(absolute_path(File.join(dirname, basename)), file_format)
40
59
  end
41
60
 
42
61
  def [] sheet
@@ -67,8 +86,11 @@ module WrapExcel
67
86
  new_sheet
68
87
  end
69
88
 
70
- def self.open(file, options={ }, &block)
71
- new(file, options, &block)
89
+ private
90
+ def absolute_path(file)
91
+ file = File.expand_path(file)
92
+ file = WrapExcel::Cygwin.cygpath('-w', file) if RUBY_PLATFORM =~ /cygwin/
93
+ WIN32OLE.new('Scripting.FileSystemObject').GetAbsolutePathName(file)
72
94
  end
73
95
  end
74
96
  end
@@ -1,3 +1,3 @@
1
1
  module WrapExcel
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -43,6 +43,33 @@ describe WrapExcel::Book do
43
43
  end
44
44
  end
45
45
  end
46
+
47
+ describe "WIN32OLE#GetAbsolutePathName" do
48
+ it "'~' should be HOME directory" do
49
+ path = '~/Abrakadabra.xlsx'
50
+ expected_path = Regexp.new(File.expand_path(path).gsub(/\//, "."))
51
+ expect {
52
+ WrapExcel::Book.open(path)
53
+ }.to raise_error(WIN32OLERuntimeError, expected_path)
54
+ end
55
+ end
56
+
57
+ it 'should not output deprecation warning' do
58
+ capture(:stderr) {
59
+ book = WrapExcel::Book.open(@simple_file)
60
+ book.close
61
+ }.should eq ""
62
+ end
63
+
64
+ end
65
+
66
+ describe ".new" do
67
+ it 'should output deprecation warning' do
68
+ capture(:stderr) {
69
+ book = WrapExcel::Book.new(@simple_file)
70
+ book.close
71
+ }.should match /DEPRECATION WARNING: WrapExcel::Book.new and WrapExcel::Book.open will be split. If you open existing file, please use WrapExcel::Book.open.\(call from #{File.expand_path(__FILE__)}:#{__LINE__ - 2}.+\)\n/
72
+ end
46
73
  end
47
74
 
48
75
  describe 'access sheet' do
@@ -160,7 +187,7 @@ describe WrapExcel::Book do
160
187
  end
161
188
  end
162
189
 
163
- describe ".save" do
190
+ describe "#save" do
164
191
  context "when open with read only" do
165
192
  before do
166
193
  @book = WrapExcel::Book.open(@simple_file)
@@ -174,6 +201,18 @@ describe WrapExcel::Book do
174
201
  }
175
202
  end
176
203
 
204
+ context "with argument" do
205
+ before do
206
+ WrapExcel::Book.open(@simple_file, :read_only => false) do |book|
207
+ book.save("#{@dir}/simple_save.xlsx")
208
+ end
209
+ end
210
+
211
+ it "should save to 'simple_save.xlsx'" do
212
+ File.exist?(@dir + "/simple_save.xlsx").should be_true
213
+ end
214
+ end
215
+
177
216
  end
178
217
 
179
218
  end
@@ -16,7 +16,7 @@ describe WrapExcel::Sheet do
16
16
  describe ".initialize" do
17
17
  context "when open sheet protected(with password is 'protect')" do
18
18
  before do
19
- @book_protect = WrapExcel::Book.new(@dir + '/protected_sheet.xls', :visible => true)
19
+ @book_protect = WrapExcel::Book.open(@dir + '/protected_sheet.xls', :visible => true)
20
20
  @protected_sheet = @book_protect['protect']
21
21
  end
22
22
 
@@ -4,12 +4,32 @@ require 'tmpdir'
4
4
  require "fileutils"
5
5
  require File.join(File.dirname(__FILE__), '../lib/wrap_excel')
6
6
 
7
- def create_tmpdir
8
- tmpdir = Dir.mktmpdir
9
- FileUtils.cp_r(File.join(File.dirname(__FILE__), 'data'), tmpdir)
10
- tmpdir + '/data'
7
+ module WrapExcel::SpecHelpers
8
+ def create_tmpdir
9
+ tmpdir = Dir.mktmpdir
10
+ FileUtils.cp_r(File.join(File.dirname(__FILE__), 'data'), tmpdir)
11
+ tmpdir + '/data'
12
+ end
13
+
14
+ def rm_tmp(tmpdir)
15
+ FileUtils.remove_entry_secure(File.dirname(tmpdir))
16
+ end
17
+
18
+ # This method is almost copy of wycats's implementation.
19
+ # http://pochi.hatenablog.jp/entries/2010/03/24
20
+ def capture(stream)
21
+ begin
22
+ stream = stream.to_s
23
+ eval "$#{stream} = StringIO.new"
24
+ yield
25
+ result = eval("$#{stream}").string
26
+ ensure
27
+ eval("$#{stream} = #{stream.upcase}")
28
+ end
29
+ result
30
+ end
11
31
  end
12
32
 
13
- def rm_tmp(tmpdir)
14
- FileUtils.remove_entry_secure(File.dirname(tmpdir))
33
+ RSpec.configure do |config|
34
+ config.include WrapExcel::SpecHelpers
15
35
  end
@@ -25,8 +25,8 @@ Gem::Specification.new do |s|
25
25
  s.require_paths = ["lib"]
26
26
  s.add_development_dependency "rake", '>= 0.9.2'
27
27
  s.add_development_dependency "rspec", '>= 2.6.0'
28
- s.add_development_dependency "rb-fchange"
29
- s.add_development_dependency "rb-notifu"
30
- s.add_development_dependency "win32console"
31
- s.add_development_dependency "guard-rspec"
28
+ s.add_development_dependency "rb-fchange", '>= 0.0.5'
29
+ s.add_development_dependency "wdm", '>= 0.0.3'
30
+ s.add_development_dependency "win32console", '>= 1.3.2'
31
+ s.add_development_dependency "guard-rspec", '>= 2.1.1'
32
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrap_excel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
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-10-16 00:00:00.000000000 Z
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 0.0.5
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,15 +58,15 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.0.5
62
62
  - !ruby/object:Gem::Dependency
63
- name: rb-notifu
63
+ name: wdm
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
- version: '0'
69
+ version: 0.0.3
70
70
  type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: 0.0.3
78
78
  - !ruby/object:Gem::Dependency
79
79
  name: win32console
80
80
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +82,7 @@ dependencies:
82
82
  requirements:
83
83
  - - ! '>='
84
84
  - !ruby/object:Gem::Version
85
- version: '0'
85
+ version: 1.3.2
86
86
  type: :development
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
@@ -90,7 +90,7 @@ dependencies:
90
90
  requirements:
91
91
  - - ! '>='
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: 1.3.2
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: guard-rspec
96
96
  requirement: !ruby/object:Gem::Requirement
@@ -98,7 +98,7 @@ dependencies:
98
98
  requirements:
99
99
  - - ! '>='
100
100
  - !ruby/object:Gem::Version
101
- version: '0'
101
+ version: 2.1.1
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ dependencies:
106
106
  requirements:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: 2.1.1
110
110
  description: WrapExcel is to wrap the win32ole, and easy to use Excel operations with
111
111
  ruby. Detailed description please see the README.
112
112
  email:
@@ -119,7 +119,6 @@ extra_rdoc_files:
119
119
  - LICENSE
120
120
  files:
121
121
  - .gitignore
122
- - .rspec
123
122
  - Gemfile
124
123
  - Guardfile
125
124
  - LICENSE
@@ -162,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
161
  version: '0'
163
162
  segments:
164
163
  - 0
165
- hash: 109616829
164
+ hash: 963983667
166
165
  required_rubygems_version: !ruby/object:Gem::Requirement
167
166
  none: false
168
167
  requirements:
@@ -171,10 +170,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
170
  version: '0'
172
171
  segments:
173
172
  - 0
174
- hash: 109616829
173
+ hash: 963983667
175
174
  requirements: []
176
175
  rubyforge_project: wrap_excel
177
- rubygems_version: 1.8.23
176
+ rubygems_version: 1.8.24
178
177
  signing_key:
179
178
  specification_version: 3
180
179
  summary: WrapExcel is a wrapper library that specializes in the operation of Excel
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --format documentation