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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45b4476491dc99a617979e6a3de7a0ecdf92505b
4
- data.tar.gz: 63b96089d34abe4b12ec2bced19f9f634f0e4a84
3
+ metadata.gz: 57c88ea7accbfcd532ec5ebb0c5f6a368588d95d
4
+ data.tar.gz: 5ee07c422165ecad53924dd85bba824a9befc2a0
5
5
  SHA512:
6
- metadata.gz: e7736d676c52d70438455f844902a05dde151f83b341e888bb793b86c79018c70d859b0bda862921b96f8b6206419d830c64d4dc93407f3bad0bbc1e182a777e
7
- data.tar.gz: 83a80a42108a28d05a98b66e94fbc9a196e5f7f75615538293997ba4e86f92a1e150e9d7f45d0fad09f63f1140c7557ebe4660fbae6fd5a30ac11abc539aa67a
6
+ metadata.gz: 3775f93d578a0d5e99d0a1617753120f1ee771d37bbed6263b946261f1c8de8ba3f0b2dab651c3bf67865d010d5a04dc1cdbe604aa701d7a38bbeeec58293391
7
+ data.tar.gz: 621875e63bfc9bb6f65c72b95704099a8825e2bb9a72d7fc3567e26d5fc8ba832d6ae2a1dee557868048e69ea917a86f2361c099aa45bdb3d39c1328239e50ce
@@ -1,7 +1,7 @@
1
1
  class User < SheetsDB::Worksheet::Row
2
2
  attribute :id, type: Integer
3
3
  attribute :first_name
4
- attribute :last_name
4
+ attribute :last_name, column_name: "Last Name"
5
5
  attribute :pet_ids, multiple: true
6
6
 
7
7
  has_many :pets, from_collection: :pets, key: :pet_ids
@@ -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.
@@ -56,6 +56,12 @@ module SheetsDB
56
56
  other.google_drive_resource == google_drive_resource
57
57
  end
58
58
 
59
+ alias_method :eql?, :==
60
+
61
+ def hash
62
+ [self.class, google_drive_resource].hash
63
+ end
64
+
59
65
  def base_attributes
60
66
  {
61
67
  id: id,
@@ -1,3 +1,3 @@
1
1
  module SheetsDB
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -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
@@ -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.to_sym] = Column.new(name: name.to_sym, column_position: i + 1)
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 attribute_at_row_position(column_name, row_position)
39
- attribute_definition = attribute_definitions.fetch(column_name, {})
40
- column = columns[column_name]
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 |name, value|
63
- column = columns[name]
64
- definition = attribute_definitions[name]
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.3.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: 2016-11-04 00:00:00.000000000 Z
11
+ date: 2017-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google_drive