Release 0.9.11

This commit is contained in:
Steve Bazyl 2016-07-06 14:16:06 -07:00
parent cba9d79b52
commit 7c7691ccc0
2 changed files with 79 additions and 0 deletions

View File

@ -40,6 +40,14 @@ platforms :ruby do
end
end
platforms :mri_21, :mri_22, :mri_23 do
group :development do
gem 'derailed_benchmarks'
gem 'stackprof'
gem 'rack-test'
end
end
if ENV['RAILS_VERSION']
gem 'rails', ENV['RAILS_VERSION']
end

71
sync.rb
View File

@ -0,0 +1,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)