forked from googleapis/google-api-ruby-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.rb
71 lines (59 loc) · 1.6 KB
/
sync.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'google/apis/sheets_v4'
class Mapping
def initialize
@columns = []
@primary = nil
end
def add_column(name, property: nil, key: nil, getter: nil, setter: nil, hidden: false, primary: false)
getter_fn = getter if getter
getter_fn = Proc.new { |obj| obj.send(property) } if property
getter_fn = Proc.new { |obj| obj[key] } if key
setter_fn = setter if setter
setter_fn = Proc.new { |obj, val| obj.send(property + '=') } if property
setter_fn = Proc.new { |obj, val| obj[key]= val } if key
@columns.push({
name: name,
getter: getter_fn,
setter: setter_fn,
hidden: hidden
})
if primary
fail "Only one column can be the primary key" unless @primary.nil?
@primary = @columns.last
end
end
def to_row(obj)
@columns.map { |column| column[:accessor].call(obj) }
end
end
class SheetSyncEngine
def initialize(mapping, sheet_id: nil)
@service = Google::Apis::SheetsV4::SheetsService.new
@sheet_id = sheet_id
@mapping = mapping
end
def credentials=(creds)
@service.authorization = creds
end
def append(items)
rows = items.map { |item| @mapping.to_row(item) }
puts rows
end
end
class Foo
attr_accessor :prop1
attr_accessor :prop2
def initialize(p1, p2)
@prop1 = p1
@prop2 = p2
end
end
mapping = Mapping.new
mapping.add_column('foo', property: :prop1)
mapping.add_column('bar', proc: Proc.new {|o| 'aaa' + o.prop2})
sync = SheetSyncEngine.new(mapping)
items = []
items.push(Foo.new('a', 'b'))
items.push(Foo.new('a1', 'b1'))
items.push(Foo.new('a2', 'b2'))
sync.append(items)