rusk 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,4 @@
1
+ lib/**/*.rb
2
+ -
3
+ README.md
4
+ LICENSE
@@ -0,0 +1,19 @@
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
+ vendor/bundle
19
+ .DS_Store
@@ -0,0 +1,3 @@
1
+ --charset=UTF-8
2
+ --no-private
3
+ --markup markdown
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in goblin.gemspec
4
+ gemspec
@@ -0,0 +1,11 @@
1
+ # -*- mode: ruby -*-
2
+ # A sample Guardfile
3
+ # More info at https://github.com/guard/guard#readme
4
+
5
+ guard 'rspec', :version => 2, :cli => '--color', :all_after_pass => false, :all_on_start => false do
6
+ watch(%r{^spec/.+_spec\.rb$})
7
+ watch(%r{^lib/goblin/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
8
+ watch('lib/goblin.rb') { "spec" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
11
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 tomi
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,150 @@
1
+ # Rusk
2
+
3
+ Rusk is library that read and write Open Document Spreadsheet Format(ods).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rusk'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rusk
18
+
19
+ ## Usage
20
+
21
+ ### Open file ###
22
+
23
+ Read with block.
24
+
25
+ ```ruby
26
+ Rusk::Book.open('path_to_odsfile') do |book|
27
+ # do something
28
+ end
29
+ ```
30
+
31
+ Read without block.
32
+
33
+ ```ruby
34
+ book = Rusk::Book.open('path_to_odsfile')
35
+ book.close
36
+ ```
37
+
38
+ Rusk::Book.open can't create new file.
39
+
40
+ ### Save file ###
41
+
42
+ ```ruby
43
+ Rusk::Book.open('path_to_odsfile') do |book|
44
+ # do something
45
+ book.save
46
+ end
47
+ ```
48
+
49
+ ### access sheet ###
50
+
51
+ Rusk::Sheet object can access with Rusk::Book#[] method.
52
+
53
+ Rusk::Book#[] can access with sheet name or index.
54
+
55
+ ```ruby
56
+ sheet = book[0]
57
+ ```
58
+
59
+ or
60
+
61
+ ```ruby
62
+ sheet = book["Sheet1"]
63
+ ```
64
+
65
+ ### access cell ###
66
+
67
+ ### read cell ###
68
+
69
+ Rusk::Cell object can access with Rusk::Sheet#[] and Rusk::Sheet#each and Rusk::Sheet#each_row.
70
+
71
+ Rusk::Sheet#[]
72
+
73
+ ```ruby
74
+ cell = Rusk::Sheet[0, 0] # get Rusk::Cell at [A1]
75
+ ```
76
+
77
+ Rusk::Sheet#each
78
+
79
+ ```ruby
80
+ sheet.each do |cell|
81
+ # do something (cell is Rusk::Cell)
82
+ end
83
+ ```
84
+
85
+ Rusk::Sheet#each_row
86
+
87
+ ```ruby
88
+ sheet.each_row do |row|
89
+ # do something (row is Array of Rusk::Cell elements)
90
+ end
91
+ ```
92
+
93
+ Rusk::Sheet#each_column
94
+
95
+ ```ruby
96
+ sheet.each_column do |column|
97
+ # do something (column is Array of Rusk::Cell elements)
98
+ end
99
+ ```
100
+
101
+ #### cell's value ####
102
+
103
+ Rusk::Cell#value gets the value of the cell according to the format.
104
+
105
+ ```ruby
106
+ # when cell's value is string
107
+ cell.value # => "Matz"
108
+ # when cell's value is float
109
+ cell.value # => 1.0
110
+ # when cell's value is date
111
+ cell.value # => #<Date: 2012-01-01 ((2455928j,0s,0n),+0s,2299161j)>
112
+ # when cell's value is enter date of time
113
+ cell.value # => #<DateTime: 2012-01-01T00:00:00+00:00 ((2455928j,0s,0n),+0s,2299161j)>
114
+ # when cell's value is time
115
+ cell.value # => "12:30" (string not time)
116
+ # when cell's value is percentage(maybe application(example LibreOffice, OpenOffice) show '10%')
117
+ cell.value # => 0.1
118
+ # when cell's value is boolean
119
+ cell.value # => ture or false
120
+ ```
121
+
122
+ ### modify cell ###
123
+
124
+ To set value to cell, use Rusk::Cell#value=.
125
+
126
+ ```ruby
127
+ cell.value = "string" # => set "string" to cell and set string to value-type
128
+ cell.value = 1.0 # => set 1.0(float) to cell and set float to value-type
129
+ cell.value = Date.new(2012,4,29) # => set cell 2012-04-29 to cell and set date to value-type
130
+ ```
131
+
132
+ ## Contributing
133
+
134
+ 1. Fork it
135
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
136
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
137
+ 4. Push to the branch (`git push origin my-new-feature`)
138
+ 5. Create new Pull Request
139
+
140
+ ## Support ##
141
+
142
+ Report issues and feature requests to [github Issues](https://github.com/tomiacannondale/rusk/issues).
143
+
144
+ ## Author ##
145
+
146
+ [tomi](mailto:tomiacannondale@gmail.com)
147
+
148
+ ## License ##
149
+
150
+ MIT License. For more imformation, please see LICENSE.
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'zip/zip'
4
+ require 'nokogiri'
5
+
6
+ desc "Convert all spec/data/*.ods files to spec/data/*_content.xml files"
7
+ task :convert_ods do
8
+ dir = File.expand_path('spec/data', File.dirname(__FILE__))
9
+ Dir.glob("#{dir}/*.ods").each do |file|
10
+ output_file = File.join(dir, File.basename(file, '.ods') + '_content.xml')
11
+ File.open(output_file, 'w') do |f|
12
+ Zip::ZipFile.open(file) do |zip_files|
13
+ f.puts Nokogiri::XML(zip_files.read("content.xml"))
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "rusk/version"
3
+ require 'nokogiri'
4
+ require File.expand_path('rusk/book', File.dirname(__FILE__))
5
+ require File.expand_path('rusk/sheet', File.dirname(__FILE__))
6
+ require File.expand_path('rusk/cell', File.dirname(__FILE__))
7
+
8
+ module Rusk
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'zip/zip'
3
+
4
+ module Rusk
5
+ class Book
6
+ attr_reader :sheets
7
+
8
+ def initialize(file, &block)
9
+ @files = Zip::ZipFile.open(file)
10
+ @content = Nokogiri::XML(@files.read("content.xml"))
11
+ @sheets = @content.xpath("//table:table")
12
+
13
+ if block
14
+ begin
15
+ yield self
16
+ ensure
17
+ @files.close
18
+ end
19
+ end
20
+ end
21
+
22
+ def [](name_or_index)
23
+ if name_or_index.is_a? Numeric
24
+ sheet = @sheets[name_or_index]
25
+ else
26
+ sheet = @sheets.detect{ |i| i["table:name"] == name_or_index }
27
+ end
28
+ sheet && Rusk::Sheet.new(sheet)
29
+ end
30
+
31
+ def save
32
+ @files.get_output_stream("content.xml") { |f| f.puts @content }
33
+ end
34
+
35
+ def self.open(file, &block)
36
+ new(file, &block)
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,49 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'date'
3
+
4
+ module Rusk
5
+ class Cell
6
+ def initialize(content)
7
+ @content = content
8
+ end
9
+
10
+ def value
11
+ case value_type
12
+ when "date"
13
+ if @content["office:date-value"] =~ /^\d{4}-\d{2}-\d{2}$/
14
+ Date.strptime(@content["office:date-value"], "%Y-%m-%d")
15
+ else
16
+ DateTime.strptime(@content["office:date-value"], "%Y-%m-%dT%H:%M:%S")
17
+ end
18
+ when "float", "currency", "percentage"
19
+ @content["office:value"].to_f
20
+ when "boolean"
21
+ @content["office:boolean-value"] == 'true'
22
+ else
23
+ @content.xpath("text:p").text
24
+ end
25
+ end
26
+
27
+ def value=(value)
28
+ if value.is_a? Numeric
29
+ @content["office:value"] = value.to_s
30
+ @content["office:value-type"] = "float"
31
+ elsif value.is_a? Date
32
+ @content["office:date-value"] = value.strftime("%F")
33
+ @content["office:value-type"] = 'date'
34
+ else
35
+ @content["office:value-type"] = 'string'
36
+ end
37
+ @content.xpath("text:p").children.first.content = value
38
+ end
39
+
40
+ def value_type
41
+ @content["office:value-type"]
42
+ end
43
+
44
+ def to_s
45
+ @content.xpath("text:p").text
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Rusk
4
+ class Sheet
5
+ include Enumerable
6
+
7
+ def initialize(content)
8
+ @content = content
9
+ @cells = []
10
+ @content.xpath('.//table:table-row').each do |row_range|
11
+ row_cells = []
12
+ row_range.xpath(".//table:table-cell|.//table:covered-table-cell").each do |cell|
13
+ row_cells << Rusk::Cell.new(cell)
14
+ (cell["table:number-columns-repeated"].to_i - 1).times do
15
+ row_cells << Rusk::Cell.new(cell)
16
+ end
17
+ end
18
+ @cells << row_cells
19
+ end
20
+ end
21
+
22
+ def name
23
+ @content["table:name"]
24
+ end
25
+
26
+ def name= name
27
+ @content["table:name"] = name
28
+ end
29
+
30
+ def [](row, column)
31
+ return nil if row > @cells.size || column > @cells[0].size
32
+ @cells[row][column]
33
+ end
34
+
35
+ def each
36
+ @cells.each do |rows|
37
+ rows.each do |cell|
38
+ yield cell
39
+ end
40
+ end
41
+ end
42
+
43
+ def each_row
44
+ @cells.each do |rows|
45
+ yield rows
46
+ end
47
+ end
48
+
49
+ def each_column
50
+ @cells.transpose.each do |columns|
51
+ yield columns
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Rusk
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8; mode: ruby -*-
2
+ require File.expand_path('../lib/rusk/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["tomi"]
6
+ gem.email = ["tomiacannondale@gmail.com"]
7
+ gem.description = %q{Rusk is library that read and write Open Document Spreadsheet Format(ods).}
8
+ gem.summary = %q{Read and write Open Document Spreadsheet Format(ods).}
9
+ gem.homepage = "https://github.com/tomiacannondale/rusk"
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 = "rusk"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rusk::VERSION
17
+
18
+ gem.add_runtime_dependency 'rubyzip', '~> 0.9.0'
19
+ gem.add_runtime_dependency 'nokogiri', '~> 1.5.6'
20
+ gem.add_development_dependency "rake", '~> 0.9'
21
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
22
+ gem.add_development_dependency 'rb-fsevent', '~> 0.9.0'
23
+ gem.add_development_dependency 'listen', '~> 0.5.0'
24
+ gem.add_development_dependency 'growl', '~> 1.0.0'
25
+ gem.add_development_dependency "guard-rspec", '~> 2.1.0'
26
+ gem.add_development_dependency "pry", '~> 0.9'
27
+ end
@@ -0,0 +1,67 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
3
+
4
+ describe Rusk::Book do
5
+
6
+ describe ".open" do
7
+ context "without block" do
8
+ it { expect { Rusk::Book.open("#{dir}/general_datas.ods") }.to_not raise_error }
9
+ end
10
+
11
+ context "with block" do
12
+ it { expect {
13
+ Rusk::Book.open("#{dir}/general_datas.ods") do |book|
14
+ book[0]
15
+ end
16
+ }.to_not raise_error
17
+ }
18
+
19
+ it "should be able to use block parameter" do
20
+ name = nil
21
+ Rusk::Book.open("#{dir}/general_datas.ods") do |book|
22
+ name = book[0].name
23
+ end
24
+ name.should_not be_nil
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ describe "#[]" do
32
+ before do
33
+ @book = Rusk::Book.open("#{dir}/general_datas.ods")
34
+ end
35
+
36
+ context "with numeric" do
37
+ it { @book[0].name.should eq "Sheet1" }
38
+ end
39
+
40
+ context "with sheet name" do
41
+ it { @book["Sheet1"].name.should eq "Sheet1" }
42
+ end
43
+
44
+ context "with not exist sheet name" do
45
+ it { @book["not exist"].should be_nil }
46
+ end
47
+
48
+ context "with not exist index" do
49
+ it { @book[100000].should be_nil }
50
+ end
51
+
52
+ end
53
+
54
+ describe "#save" do
55
+ before do
56
+ @book = Rusk::Book.open(create_tmp)
57
+ end
58
+
59
+ after do
60
+ remove_tmp
61
+ end
62
+
63
+ it { expect { @book.save }.to_not raise_error }
64
+
65
+ end
66
+
67
+ end