sheet_mapper 0.1.0 → 0.1.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.
- data/README.md +23 -6
- data/lib/sheet_mapper/base.rb +17 -1
- data/lib/sheet_mapper/version.rb +1 -1
- data/test/base_test.rb +47 -5
- metadata +4 -4
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SheetMapper
|
2
2
|
|
3
|
-
SheetMapper is about taking a google spreadsheet and converting a set of data rows into ruby objects.
|
3
|
+
SheetMapper is about taking a google spreadsheet and converting a set of data rows into simple ruby objects.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -14,6 +14,13 @@ gem 'sheet_mapper'
|
|
14
14
|
|
15
15
|
and then `require 'sheet_mapper'` and you are done!
|
16
16
|
|
17
|
+
## Rationale
|
18
|
+
|
19
|
+
You may ask why you would need to have an object mapper from a Google Spreadsheet. Consider though that spreadsheets are collaborative, have revision tracking, securely authenticated, are accessible anywhere and are familiar to non-technical people.
|
20
|
+
|
21
|
+
If you ever needed a dead simple admin interface, configuration document, or basic content management system, a spreadsheet is a pretty great solution that requires very little engineering overhead. Next time you are in a position where non-technical people
|
22
|
+
need to manage data, ask yourself if a spreadsheet might be a good first solution.
|
23
|
+
|
17
24
|
## Usage
|
18
25
|
|
19
26
|
First, you describe how to map a spreadsheet into data rows with a sheet object mapper:
|
@@ -21,16 +28,18 @@ First, you describe how to map a spreadsheet into data rows with a sheet object
|
|
21
28
|
```ruby
|
22
29
|
class SomeMapper < SheetMapper::Base
|
23
30
|
# Defines each column for a row and maps each column to an attribute
|
31
|
+
# Should be listed in the order the data appears in the spreadsheet
|
24
32
|
columns :foo, :bar, :baz
|
25
33
|
|
26
34
|
# Defines the condition for a row to be considered valid
|
35
|
+
# Also have access to `pos` which is the row number in the worksheet
|
27
36
|
def valid_row?
|
28
|
-
self[:foo].present?
|
37
|
+
self[:foo].present? && self.pos > 2
|
29
38
|
end
|
30
39
|
|
31
|
-
# Convert
|
32
|
-
# Any
|
33
|
-
def
|
40
|
+
# Convert bar column to a boolean from raw string
|
41
|
+
# Any method named after a column will override the default value
|
42
|
+
def bar
|
34
43
|
!!self[:bar].match(/true/i)
|
35
44
|
end
|
36
45
|
end
|
@@ -87,7 +96,15 @@ SheetMapper was created by [Nathan Esquenazi](http://github.com/nesquena) at Mis
|
|
87
96
|
|
88
97
|
## Tasks
|
89
98
|
|
90
|
-
SheetMapper is a new gem and I would love any feedback and/or pull requests.
|
99
|
+
SheetMapper is a new gem and I would love any feedback and/or pull requests. In particular:
|
100
|
+
|
101
|
+
* Inserting a data row into a collection
|
102
|
+
* Removing a data row from a collection
|
103
|
+
* Callbacks
|
104
|
+
* Validations
|
105
|
+
* Column Type Casting
|
106
|
+
|
107
|
+
Please fork if you are inspired to add any of these or any other improvements.
|
91
108
|
|
92
109
|
## Continuous Integration ##
|
93
110
|
|
data/lib/sheet_mapper/base.rb
CHANGED
@@ -8,11 +8,13 @@ module SheetMapper
|
|
8
8
|
@pos = pos
|
9
9
|
@data = data
|
10
10
|
@attrs = process_data
|
11
|
+
self.class.create_accessors!(@attrs)
|
11
12
|
end
|
12
13
|
|
13
14
|
# columns :offset_seconds, :body, :link_url, :category
|
14
15
|
def self.columns(*names)
|
15
|
-
|
16
|
+
@columns ||= []
|
17
|
+
names.any? ? @columns += names : @columns
|
16
18
|
end
|
17
19
|
|
18
20
|
# Returns the spreadsheet as a hash
|
@@ -55,6 +57,11 @@ module SheetMapper
|
|
55
57
|
|
56
58
|
protected
|
57
59
|
|
60
|
+
# Allows subclasses to inherit class ivars for columns
|
61
|
+
def self.inherited(subclass)
|
62
|
+
subclass.instance_variable_set("@columns", @columns)
|
63
|
+
end
|
64
|
+
|
58
65
|
# column_order => [:offset_seconds, :body, :link_url, :category]
|
59
66
|
def column_order
|
60
67
|
self.class.columns
|
@@ -88,5 +95,14 @@ module SheetMapper
|
|
88
95
|
val
|
89
96
|
end
|
90
97
|
|
98
|
+
# Creates getters and setters for all attributes
|
99
|
+
# Post.create_accessors!(:foo => 'bar', :baz => "foo")
|
100
|
+
def self.create_accessors!(attrs)
|
101
|
+
attrs.each do |name, value|
|
102
|
+
define_method(name) { self[name] } unless method_defined?(name)
|
103
|
+
define_method("#{name}=") { |val| self[name] = val } unless method_defined?("#{name}=")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
91
107
|
end
|
92
108
|
end
|
data/lib/sheet_mapper/version.rb
CHANGED
data/test/base_test.rb
CHANGED
@@ -1,17 +1,24 @@
|
|
1
1
|
require File.expand_path('../test_config.rb', __FILE__)
|
2
2
|
|
3
3
|
class TestBase < SheetMapper::Base
|
4
|
-
columns :name, :age
|
4
|
+
columns :name, :age
|
5
|
+
columns :color
|
5
6
|
|
6
7
|
def age
|
7
8
|
self[:age] * 2
|
8
9
|
end
|
9
10
|
|
11
|
+
def age=(val)
|
12
|
+
self[:age] = val / 2
|
13
|
+
end
|
14
|
+
|
10
15
|
def name
|
11
16
|
self[:name].upcase
|
12
17
|
end
|
13
18
|
end
|
14
19
|
|
20
|
+
class TestBaseExt < TestBase; end
|
21
|
+
|
15
22
|
describe "Base" do
|
16
23
|
setup do
|
17
24
|
@data = ["Bob", 21, "Red"]
|
@@ -19,14 +26,14 @@ describe "Base" do
|
|
19
26
|
|
20
27
|
should "support columns class accessor" do
|
21
28
|
assert_equal [:name, :age, :color], TestBase.columns
|
22
|
-
end
|
29
|
+
end # columns
|
23
30
|
|
24
31
|
should "support pos instances accessor" do
|
25
32
|
@test = TestBase.new(1, @data)
|
26
33
|
assert_equal 1, @test.pos
|
27
|
-
end
|
34
|
+
end # pos
|
28
35
|
|
29
|
-
context "for
|
36
|
+
context "for attribute methods" do
|
30
37
|
setup do
|
31
38
|
@test = TestBase.new(1, @data)
|
32
39
|
end
|
@@ -34,10 +41,23 @@ describe "Base" do
|
|
34
41
|
should "return attribute hash" do
|
35
42
|
assert_equal "BOB", @test.name
|
36
43
|
assert_equal 42, @test.age
|
37
|
-
assert_equal "Red", @test
|
44
|
+
assert_equal "Red", @test.color
|
38
45
|
end
|
39
46
|
end # attributes
|
40
47
|
|
48
|
+
context "for assignment of attributes" do
|
49
|
+
setup do
|
50
|
+
@test = TestBase.new(1, @data)
|
51
|
+
end
|
52
|
+
|
53
|
+
should "allow assignments" do
|
54
|
+
@test.age = 46
|
55
|
+
@test.color = "Black"
|
56
|
+
assert_equal 23, @test[:age]
|
57
|
+
assert_equal "Black", @test[:color]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
41
61
|
context "for accessing attribute" do
|
42
62
|
setup do
|
43
63
|
@test = TestBase.new(1, @data)
|
@@ -59,6 +79,28 @@ describe "Base" do
|
|
59
79
|
end
|
60
80
|
end # assign []=
|
61
81
|
|
82
|
+
context "for color attribute accessor" do
|
83
|
+
setup do
|
84
|
+
@test = TestBase.new(1, @data)
|
85
|
+
@test_ext = TestBaseExt.new(1, @data)
|
86
|
+
end
|
87
|
+
|
88
|
+
should "support color auto accessor" do
|
89
|
+
assert_equal "Red", @test.color
|
90
|
+
assert_equal "Red", @test_ext.color
|
91
|
+
end
|
92
|
+
|
93
|
+
should "support color auto assigner" do
|
94
|
+
@test.color = "Blue"
|
95
|
+
assert_equal "Blue", @test.color
|
96
|
+
end
|
97
|
+
|
98
|
+
should "respond to color auto accessor" do
|
99
|
+
assert_respond_to @test, :color
|
100
|
+
assert_respond_to @test_ext, :color
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
62
104
|
context "for attribute_values method" do
|
63
105
|
setup do
|
64
106
|
@test = TestBase.new(1, @data)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sheet_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nathan Esquenazi
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-24 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: google-spreadsheet-ruby
|