record_roo 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/{LICENSE.txt → LICENSE} +0 -0
- data/README.md +2 -2
- data/Rakefile +8 -0
- data/lib/record_roo.rb +5 -46
- data/lib/record_roo/data_source.rb +35 -0
- data/lib/record_roo/formats/csv.rb +11 -0
- data/lib/record_roo/formats/xls.rb +10 -0
- data/lib/record_roo/formats/xlsx.rb +10 -0
- data/lib/record_roo/version.rb +1 -1
- data/record_roo.gemspec +4 -3
- data/test/data/products.csv +1 -0
- data/test/data/products.xls +0 -0
- data/test/data/products.xlsx +0 -0
- data/test/test_recordroo.rb +58 -0
- metadata +33 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9399e4e2b97d91292fc9c0ecc04543c65f1bddd6
|
4
|
+
data.tar.gz: 61c245dc64484c8ac421ad113e56514a6bbead49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ac48d3d7002a06f07e47b8a48416020eead07dee8edb787208bb1e9d76a063fffde9e3193b69bc47c8a24539e2323518ad04ada5f1a21dd6c50dcaad94df0e1
|
7
|
+
data.tar.gz: badc46b8c2c268a5fd4a2157b96103b70f3970dd0b609dac59ffdfd0fec850c9585f730d3af10b30d7c7ee10f752078c2d74c9f28f728a038260b544d1d3f07e
|
data/.gitignore
CHANGED
data/{LICENSE.txt → LICENSE}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RecordRoo
|
2
2
|
|
3
|
-
|
3
|
+
Easy object mapping from spreadsheets
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -10,7 +10,7 @@ Add this line to your application's Gemfile:
|
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
13
|
-
$ bundle
|
13
|
+
$ bundle install
|
14
14
|
|
15
15
|
Or install it yourself as:
|
16
16
|
|
data/Rakefile
CHANGED
data/lib/record_roo.rb
CHANGED
@@ -1,53 +1,12 @@
|
|
1
1
|
require "csv"
|
2
2
|
require "roo"
|
3
|
-
require "record_roo/version"
|
4
3
|
|
5
4
|
module RecordRoo
|
6
|
-
|
7
|
-
def initialize filepath
|
8
|
-
@filepath = filepath
|
9
|
-
end
|
5
|
+
autoload :VERSION, 'record_roo/version'
|
10
6
|
|
11
|
-
|
12
|
-
@adapter = Class.instance_eval adapter
|
13
|
-
@adapter.parse @filepath
|
14
|
-
end
|
7
|
+
autoload :DataSource, 'record_roo/data_source'
|
15
8
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
object = Hash[model_with_attributes[:attributes].map.with_index(0) do |attr, index|
|
20
|
-
[attr, row[index]]
|
21
|
-
end]
|
22
|
-
model.new(object)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
def raw_extension
|
28
|
-
File.extname @filepath
|
29
|
-
end
|
30
|
-
|
31
|
-
def adapter
|
32
|
-
raw_extension.match(/\w+/).to_s.capitalize
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class Xlsx
|
37
|
-
def self.parse filepath
|
38
|
-
CSV.parse Roo::Excelx.new(filepath).to_csv
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class Xls
|
43
|
-
def self.parse filepath
|
44
|
-
CSV.parse Roo::Excel.new(filepath).to_csv
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class Csv
|
49
|
-
def self.parse filepath
|
50
|
-
CSV.read filepath
|
51
|
-
end
|
52
|
-
end
|
9
|
+
autoload :Csv, 'record_roo/formats/csv'
|
10
|
+
autoload :Xlsx, 'record_roo/formats/xlsx'
|
11
|
+
autoload :Xls, 'record_roo/formats/xls'
|
53
12
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# This class wraps a spreadsheet file.
|
2
|
+
# It provides two methods:
|
3
|
+
# * **parse** for reading the file content as an array of arrays,
|
4
|
+
# * **model_generator** for create a collection of class instances based on a given class.
|
5
|
+
class RecordRoo::DataSource
|
6
|
+
def initialize filepath
|
7
|
+
@filepath = filepath
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns an array of rows
|
11
|
+
def parse
|
12
|
+
@adapter = Class.instance_eval "RecordRoo::#{adapter}"
|
13
|
+
@adapter.parse @filepath
|
14
|
+
end
|
15
|
+
|
16
|
+
# Given a class and an attribute list, it produces an array of class instances.
|
17
|
+
def model_generator model_with_attributes
|
18
|
+
parse.drop(1).map do |row|
|
19
|
+
model = model_with_attributes[:model]
|
20
|
+
object = Hash[model_with_attributes[:attributes].map.with_index(0) do |attr, index|
|
21
|
+
[attr, row[index]]
|
22
|
+
end]
|
23
|
+
model.new(object)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def raw_extension
|
29
|
+
File.extname @filepath
|
30
|
+
end
|
31
|
+
|
32
|
+
def adapter
|
33
|
+
raw_extension.match(/\w+/).to_s.capitalize
|
34
|
+
end
|
35
|
+
end
|
data/lib/record_roo/version.rb
CHANGED
data/record_roo.gemspec
CHANGED
@@ -6,11 +6,11 @@ require 'record_roo/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "record_roo"
|
8
8
|
spec.version = RecordRoo::VERSION
|
9
|
-
spec.authors = ["Daniel Martinez"]
|
10
|
-
spec.email = ["eduardodaniel2@gmail.com"]
|
9
|
+
spec.authors = ["Daniel Martinez", "Uriel Molina"]
|
10
|
+
spec.email = ["eduardodaniel2@gmail.com", "uriel85@gmail.com"]
|
11
11
|
spec.description = %q{Easy object mapping from spreadsheets}
|
12
12
|
spec.summary = %q{It provides a way to obtain an object that wraps a row within a spreadsheet}
|
13
|
-
spec.homepage = "
|
13
|
+
spec.homepage = "https://github.com/edmt/record_roo"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "roo", "~> 1.11.2"
|
23
24
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Key,Name,Price,Category
|
Binary file
|
Binary file
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'record_roo'
|
3
|
+
|
4
|
+
class Product
|
5
|
+
attr_accessor :key, :name, :price, :category
|
6
|
+
def initialize fake
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class RecordTest < Test::Unit::TestCase
|
11
|
+
include RecordRoo
|
12
|
+
|
13
|
+
def setup
|
14
|
+
config_path = File.expand_path("../data", __FILE__)
|
15
|
+
@filepath = "#{config_path}/products"
|
16
|
+
@csv_datasource = RecordRoo::DataSource.new "#{@filepath}.csv"
|
17
|
+
@xlsx_datasource = RecordRoo::DataSource.new "#{@filepath}.xlsx"
|
18
|
+
@xls_datasource = RecordRoo::DataSource.new "#{@filepath}.xls"
|
19
|
+
@model = Product
|
20
|
+
@attributes = [:key, :name, :price, :category]
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_csv_file_parsing
|
24
|
+
collection = @csv_datasource.parse
|
25
|
+
assert_instance_of Array, collection
|
26
|
+
assert_instance_of Array, collection.first
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_xlsx_file_parsing
|
30
|
+
collection = @xlsx_datasource.parse
|
31
|
+
assert_instance_of Array, collection
|
32
|
+
assert_instance_of Array, collection.first
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_xls_file_parsing
|
36
|
+
collection = @xls_datasource.parse
|
37
|
+
assert_instance_of Array, collection
|
38
|
+
assert_instance_of Array, collection.first
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_csv_model_generator
|
42
|
+
collection = @csv_datasource.model_generator({model: @model, attributes: @attributes})
|
43
|
+
assert !collection.empty?
|
44
|
+
assert_instance_of @model, collection.first
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_xlsx_model_generator
|
48
|
+
collection = @xlsx_datasource.model_generator({model: @model, attributes: @attributes})
|
49
|
+
assert !collection.empty?
|
50
|
+
assert_instance_of @model, collection.first
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_xls_model_generator
|
54
|
+
collection = @xls_datasource.model_generator({model: @model, attributes: @attributes})
|
55
|
+
assert !collection.empty?
|
56
|
+
assert_instance_of @model, collection.first
|
57
|
+
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: record_roo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Martinez
|
8
|
+
- Uriel Molina
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: bundler
|
@@ -38,22 +39,45 @@ dependencies:
|
|
38
39
|
- - '>='
|
39
40
|
- !ruby/object:Gem::Version
|
40
41
|
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: roo
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.11.2
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.11.2
|
41
56
|
description: Easy object mapping from spreadsheets
|
42
57
|
email:
|
43
58
|
- eduardodaniel2@gmail.com
|
59
|
+
- uriel85@gmail.com
|
44
60
|
executables: []
|
45
61
|
extensions: []
|
46
62
|
extra_rdoc_files: []
|
47
63
|
files:
|
48
64
|
- .gitignore
|
49
65
|
- Gemfile
|
50
|
-
- LICENSE
|
66
|
+
- LICENSE
|
51
67
|
- README.md
|
52
68
|
- Rakefile
|
53
69
|
- lib/record_roo.rb
|
70
|
+
- lib/record_roo/data_source.rb
|
71
|
+
- lib/record_roo/formats/csv.rb
|
72
|
+
- lib/record_roo/formats/xls.rb
|
73
|
+
- lib/record_roo/formats/xlsx.rb
|
54
74
|
- lib/record_roo/version.rb
|
55
75
|
- record_roo.gemspec
|
56
|
-
|
76
|
+
- test/data/products.csv
|
77
|
+
- test/data/products.xls
|
78
|
+
- test/data/products.xlsx
|
79
|
+
- test/test_recordroo.rb
|
80
|
+
homepage: https://github.com/edmt/record_roo
|
57
81
|
licenses:
|
58
82
|
- MIT
|
59
83
|
metadata: {}
|
@@ -77,4 +101,8 @@ rubygems_version: 2.0.5
|
|
77
101
|
signing_key:
|
78
102
|
specification_version: 4
|
79
103
|
summary: It provides a way to obtain an object that wraps a row within a spreadsheet
|
80
|
-
test_files:
|
104
|
+
test_files:
|
105
|
+
- test/data/products.csv
|
106
|
+
- test/data/products.xls
|
107
|
+
- test/data/products.xlsx
|
108
|
+
- test/test_recordroo.rb
|