activeresource-google_spreadsheets 0.1.1 → 0.1.2

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: 3dca8fdf8c0b4b8098cb273093e2105e1787f7d1
4
- data.tar.gz: 1ef5641b07c1e1396382a8fcd8e7a4b2237588e1
3
+ metadata.gz: 626c5911caadf7c19fbdd573adfbc1b9ae543086
4
+ data.tar.gz: a248679186714cddcd97a03e3fbe0dbee0059f70
5
5
  SHA512:
6
- metadata.gz: 18622284721fdc4bbdbffb035f26bc00567acc21c877085e13cae44253973843fd097872faad4eee3e15709cc927ecf56a26f52580f42a80d68d6a4edcfd2d7f
7
- data.tar.gz: df53e2409d044333c83fd7d0c94911c69a1749b111a1e8eaacf1cb03cd06198ec28b2277aeec3a13b8308c0639dfb789738a20820305b3ad2124f7622353877d
6
+ metadata.gz: 1d9420f2494b146e3da65248c05eec4fdb1c7d32f6142b2f23f987ef8cfbe2c004954cc36666f0caeada9478431046c7513d2998b87b3b1a226b34933d6bfa31
7
+ data.tar.gz: c7e8afd0c1ab9e86f8a074eb7c30e7b6230a6223fd5bb4c11c2e839f92668b406f151180c514169f977a60de8e8e0aba86df11c62a8359fbc806e6a6fec2b78f
@@ -19,10 +19,12 @@ module GoogleSpreadsheets
19
19
  # worksheet_title: 'users'
20
20
  # after_commit :sync_user_row
21
21
  def sync_with(rows_name, options)
22
- options.assert_valid_keys(:spreadsheet_id, :worksheet_title, :class_name)
23
- options[:worksheet_title] ||= rows_name.to_s
24
- options[:class_name] ||= rows_name.to_s.classify
25
- synchronizer = Synchronizer.new(self, options[:class_name].safe_constantize, options[:spreadsheet_id], options[:worksheet_title])
22
+ options.assert_valid_keys(:spreadsheet_id, :worksheet_title, :class_name, :assigner, :include_blank)
23
+ opts = options.dup
24
+ spreadsheet_id = opts.delete(:spreadsheet_id)
25
+ worksheet_title = opts.delete(:worksheet_title) || rows_name.to_s
26
+ class_name = opts.delete(:class_name) || rows_name.to_s.classify
27
+ synchronizer = Synchronizer.new(self, class_name.safe_constantize, spreadsheet_id, worksheet_title, opts)
26
28
  self.synchronizers = self.synchronizers.merge(rows_name => synchronizer) # not share parent class attrs
27
29
 
28
30
  # rows accessor
@@ -32,9 +34,9 @@ module GoogleSpreadsheets
32
34
  end
33
35
 
34
36
  # inbound sync all (import)
35
- define_singleton_method("sync_with_#{rows_name}") do |options = {}|
37
+ define_singleton_method("sync_with_#{rows_name}") do
36
38
  synchronizer = self.synchronizers[rows_name]
37
- synchronizer.sync_with_rows(options)
39
+ synchronizer.sync_with_rows
38
40
  end
39
41
 
40
42
  # outbound sync one (export)
@@ -48,11 +50,12 @@ module GoogleSpreadsheets
48
50
  class Synchronizer
49
51
  attr_reader :record_class, :row_class, :spreadsheet_id, :worksheet_title
50
52
 
51
- def initialize(record_class, row_class, spreadsheet_id, worksheet_title)
53
+ def initialize(record_class, row_class, spreadsheet_id, worksheet_title, options = {})
52
54
  @record_class = record_class
53
55
  @row_class = row_class || default_class_for(REL_NAME_ROW)
54
56
  @spreadsheet_id = spreadsheet_id
55
57
  @worksheet_title = worksheet_title
58
+ @options = options
56
59
  end
57
60
 
58
61
  def all_rows
@@ -66,20 +69,22 @@ module GoogleSpreadsheets
66
69
  end
67
70
  end
68
71
 
69
- def sync_with_rows(options)
72
+ def sync_with_rows
70
73
  reset
71
74
  records = all_rows.map do |row|
72
75
  record_class.find_or_initialize_by(id: row.id).tap do |record|
73
76
  if row.all_values_empty?
74
77
  # Due to destroy if exists
75
78
  record.instance_variable_set(:@due_to_destroy, true)
79
+ next
80
+ end
81
+
82
+ row_attributes = Hash[row.aliased_attributes.map{|attr| [attr, row.send(attr)] }]
83
+ row_attributes.reject!{|_, v| v.blank? } unless @options[:include_blank]
84
+ if @options[:assigner]
85
+ record.send(@options[:assigner], row_attributes)
76
86
  else
77
- row.aliased_attributes.each do |attr|
78
- value = row.send(attr)
79
- if options[:include_blank] || value.present?
80
- record.send("#{attr}=", value)
81
- end
82
- end
87
+ assign_row_attributes(record, row_attributes)
83
88
  end
84
89
  end
85
90
  end
@@ -126,10 +131,17 @@ module GoogleSpreadsheets
126
131
  end
127
132
 
128
133
  private
134
+
129
135
  def default_class_for(rel_name)
130
136
  LinkRelations.class_name_mappings[rel_name].classify.constantize
131
137
  end
132
138
 
139
+ def assign_row_attributes(record, row_attributes)
140
+ row_attributes.each do |attr, value|
141
+ record.public_send("#{attr}=", value)
142
+ end
143
+ end
144
+
133
145
  def transaction_if_possible(origin = self, &block)
134
146
  if origin.respond_to?(:transaction)
135
147
  origin.transaction(&block)
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheets
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeresource-google_spreadsheets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chihiro Ito
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-19 00:00:00.000000000 Z
12
+ date: 2014-12-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activeresource