sheets_db 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/documentation/example_models/user.rb +1 -1
- data/documentation/usage.md +3 -1
- data/lib/sheets_db/resource.rb +6 -0
- data/lib/sheets_db/version.rb +1 -1
- data/lib/sheets_db/worksheet/row.rb +14 -2
- data/lib/sheets_db/worksheet.rb +20 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57c88ea7accbfcd532ec5ebb0c5f6a368588d95d
|
4
|
+
data.tar.gz: 5ee07c422165ecad53924dd85bba824a9befc2a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3775f93d578a0d5e99d0a1617753120f1ee771d37bbed6263b946261f1c8de8ba3f0b2dab651c3bf67865d010d5a04dc1cdbe604aa701d7a38bbeeec58293391
|
7
|
+
data.tar.gz: 621875e63bfc9bb6f65c72b95704099a8825e2bb9a72d7fc3567e26d5fc8ba832d6ae2a1dee557868048e69ea917a86f2361c099aa45bdb3d39c1328239e50ce
|
data/documentation/usage.md
CHANGED
@@ -148,7 +148,7 @@ end
|
|
148
148
|
class User < SheetsDB::Worksheet::Row
|
149
149
|
attribute :id, type: Integer
|
150
150
|
attribute :first_name
|
151
|
-
attribute :last_name
|
151
|
+
attribute :last_name, column_name: "Last Name"
|
152
152
|
attribute :pet_ids, multiple: true
|
153
153
|
|
154
154
|
has_many :pets, from_collection: :pets, key: :pet_ids
|
@@ -175,6 +175,8 @@ The only required argument to the `attribute` class method is the name of the co
|
|
175
175
|
|
176
176
|
`transform`: If you supply a Proc (or lambda) to the `transform` option, reading this column from the spreadsheet will cause it to first be sent through this transformation Proc. For `multiple` columns, each element will be transformed independently.
|
177
177
|
|
178
|
+
`column_name`: By default, SheetsDB will use the attribute name as the name of the column to look for in the header row (first row) of the worksheet. You can override this, however, by specifying the `column_name` option.
|
179
|
+
|
178
180
|
##### `has_many` and `has_one` associations
|
179
181
|
|
180
182
|
For associations in which the key exists in the specifying row, use `has_many` or `has_one`. The `from_collection` parameter should reference the `has_many` entry on the Spreadsheet model that maps to the target worksheet, and the `key` parameter is the name of the local column that maps to the foreign worksheet's `id` column.
|
data/lib/sheets_db/resource.rb
CHANGED
data/lib/sheets_db/version.rb
CHANGED
@@ -13,8 +13,8 @@ module SheetsDB
|
|
13
13
|
subclass.instance_variable_set(:@association_definitions, @association_definitions)
|
14
14
|
end
|
15
15
|
|
16
|
-
def attribute(name, type: String, multiple: false, transform: nil)
|
17
|
-
register_attribute(name, type: type, multiple: multiple, transform: transform, association: false)
|
16
|
+
def attribute(name, type: String, multiple: false, transform: nil, column_name: nil)
|
17
|
+
register_attribute(name, type: type, multiple: multiple, transform: transform, column_name: column_name || name.to_s, association: false)
|
18
18
|
|
19
19
|
define_method(name) do
|
20
20
|
begin
|
@@ -227,6 +227,18 @@ module SheetsDB
|
|
227
227
|
end
|
228
228
|
attributes.merge(hashed_associations)
|
229
229
|
end
|
230
|
+
|
231
|
+
def ==(other)
|
232
|
+
other.is_a?(self.class) &&
|
233
|
+
other.worksheet == worksheet &&
|
234
|
+
other.row_position == row_position
|
235
|
+
end
|
236
|
+
|
237
|
+
alias_method :eql?, :==
|
238
|
+
|
239
|
+
def hash
|
240
|
+
[self.class, worksheet, row_position].hash
|
241
|
+
end
|
230
242
|
end
|
231
243
|
end
|
232
244
|
end
|
data/lib/sheets_db/worksheet.rb
CHANGED
@@ -19,12 +19,18 @@ module SheetsDB
|
|
19
19
|
other.type == type
|
20
20
|
end
|
21
21
|
|
22
|
+
alias_method :eql?, :==
|
23
|
+
|
24
|
+
def hash
|
25
|
+
[self.class, google_drive_resource, type].hash
|
26
|
+
end
|
27
|
+
|
22
28
|
def columns
|
23
29
|
@columns ||= begin
|
24
30
|
{}.tap { |directory|
|
25
31
|
google_drive_resource.rows.first.each_with_index do |name, i|
|
26
32
|
unless name == ""
|
27
|
-
directory[name
|
33
|
+
directory[name] = Column.new(name: name, column_position: i + 1)
|
28
34
|
end
|
29
35
|
end
|
30
36
|
}
|
@@ -35,9 +41,16 @@ module SheetsDB
|
|
35
41
|
type.attribute_definitions
|
36
42
|
end
|
37
43
|
|
38
|
-
def
|
39
|
-
attribute_definition = attribute_definitions.fetch(
|
40
|
-
|
44
|
+
def get_definition_and_column(attribute_name)
|
45
|
+
attribute_definition = attribute_definitions.fetch(attribute_name, {})
|
46
|
+
[
|
47
|
+
attribute_definition,
|
48
|
+
columns[attribute_definition.fetch(:column_name, attribute_name.to_s)]
|
49
|
+
]
|
50
|
+
end
|
51
|
+
|
52
|
+
def attribute_at_row_position(attribute_name, row_position)
|
53
|
+
attribute_definition, column = get_definition_and_column(attribute_name)
|
41
54
|
raw_value = read_value_from_google_drive_resource(
|
42
55
|
dimensions: [row_position, column.column_position],
|
43
56
|
attribute_definition: attribute_definition
|
@@ -59,10 +72,9 @@ module SheetsDB
|
|
59
72
|
end
|
60
73
|
|
61
74
|
def update_attributes_at_row_position(staged_attributes, row_position:)
|
62
|
-
staged_attributes.each do |
|
63
|
-
column =
|
64
|
-
|
65
|
-
assignment_value = definition[:multiple] ? value.join(",") : value
|
75
|
+
staged_attributes.each do |attribute_name, value|
|
76
|
+
attribute_definition, column = get_definition_and_column(attribute_name)
|
77
|
+
assignment_value = attribute_definition[:multiple] ? value.join(",") : value
|
66
78
|
google_drive_resource[row_position, column.column_position] = assignment_value
|
67
79
|
end
|
68
80
|
google_drive_resource.synchronize
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sheets_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ravi Gadad
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google_drive
|