activeresource-google_spreadsheets 0.0.3 → 0.0.4

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: 532729ef07bce877223dd5be8b7b9ca86d2759f6
4
- data.tar.gz: 0418b8eb8fc8bea6d0c0600bf930f0641d29340a
3
+ metadata.gz: 0ea1d38e730233fa7f2f4458cc08fe708fcd5e84
4
+ data.tar.gz: e1dacea922cdaf99b35e14fee4a01fd60d28f6c2
5
5
  SHA512:
6
- metadata.gz: 0382a122b75e1c62c5a85ed38e58dc35907a7db1e6cf8d43c2280d139e117972534a6f941e47a8640ea1e3eac3aad6a9ebaa62827e70734e489829ddd999f706
7
- data.tar.gz: d5684ee8736054c9816b41bfa30b2c097d99b774774dc9f6bebdea8e71557a856aafce95ee8fa895bf94e3193e4080d63c718add570e6b47bec7e9d2043c00dd
6
+ metadata.gz: ff2927902dfd953d8f91430546ee62c192ca1b9865bd95de630135556aa1a2e4210205ead6052324b999278706c6f9fc14a2501d2f23b01dc2a5a3a4e0644859
7
+ data.tar.gz: 5112a41c9a8df397c5c88ceaab92d9cbcd74666042970290bf3cf006d8dc79efd0e1cbfea4b9dd0f3483a3df7fbbf76cd4ab5f2875b53ea14a7d87ab894af32c
@@ -0,0 +1,131 @@
1
+ module GoogleSpreadsheets
2
+ module Enhanced
3
+ module Syncing
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :synchronizers
8
+ self.synchronizers = {}
9
+ end
10
+
11
+ module ClassMethods
12
+ # === Example
13
+ # class User < ActiveRecord::Base
14
+ # include GoogleSpreadsheets::Enhanced::Syncing
15
+ # sync_with :user_rows, spreadsheet_id: 'xxxx',
16
+ # worksheet_title: 'users'
17
+ # after_commit :sync_user_row
18
+ def sync_with(rows_name, options)
19
+ options.assert_valid_keys(:spreadsheet_id, :worksheet_title, :class_name)
20
+ options[:worksheet_title] ||= rows_name.to_s
21
+ synchronizer = Synchronizer.new(self, options[:spreadsheet_id], options[:worksheet_title])
22
+ self.synchronizers.merge!(rows_name => synchronizer)
23
+
24
+ # rows accessor
25
+ define_singleton_method(rows_name) do
26
+ synchronizer = self.synchronizers[rows_name]
27
+ synchronizer.all_rows
28
+ end
29
+
30
+ # inbound sync all (import)
31
+ define_singleton_method("sync_with_#{rows_name}") do
32
+ synchronizer = self.synchronizers[rows_name]
33
+ synchronizer.sync_with_rows
34
+ end
35
+
36
+ # outbound sync one (export)
37
+ define_method("sync_#{rows_name.to_s.singularize}") do
38
+ synchronizer = self.class.synchronizers[rows_name]
39
+ synchronizer.sync_row(self)
40
+ end
41
+ end
42
+ end
43
+
44
+ class Synchronizer
45
+ attr_reader :klass, :spreadsheet_id, :worksheet_title
46
+
47
+ def initialize(klass, spreadsheet_id, worksheet_title)
48
+ @klass = klass
49
+ @spreadsheet_id = spreadsheet_id
50
+ @worksheet_title = worksheet_title
51
+ end
52
+
53
+ def all_rows
54
+ worksheet.rows
55
+ end
56
+
57
+ def sync_with_rows
58
+ reset
59
+ records = all_rows.map do |row|
60
+ @klass.find_or_initialize_by(id: row.id).tap do |record|
61
+ row.aliased_attributes.each do |attr|
62
+ record.send("#{attr}=", row.send(attr))
63
+ end
64
+ end
65
+ end
66
+ skipping_outbound_sync_of(records) do |records_with_skipped_outbound|
67
+ transaction_if_possible(@klass) do
68
+ records_with_skipped_outbound.each(&:save)
69
+ end
70
+ end
71
+ end
72
+
73
+ def sync_row(record)
74
+ return if record.instance_variable_defined?(:@_skip_outbound_sync) &&
75
+ record.instance_variable_get(:@_skip_outbound_sync)
76
+ row = all_rows.find(record.id)
77
+ if record.destroyed?
78
+ row.destroy
79
+ else
80
+ # TODO: separate by AttributeAssignment
81
+ row.aliased_attributes.each do |attr|
82
+ value = (record.respond_to?(:attributes_before_type_cast) && record.attributes_before_type_cast[attr]) ||
83
+ record.send(attr)
84
+ row.send("#{attr}=", value)
85
+ end
86
+ row.save
87
+ end
88
+ end
89
+
90
+ def worksheet
91
+ @worksheet ||= spreadsheet_class_name.constantize
92
+ .find(@spreadsheet_id)
93
+ .worksheets
94
+ .find_by!(title: @worksheet_title)
95
+ end
96
+
97
+ def reset
98
+ @worksheet = nil
99
+ self
100
+ end
101
+
102
+ private
103
+ def spreadsheet_class_name(rel_name = 'http://schemas.google.com/spreadsheets/2006#spreadsheet')
104
+ LinkRelations.class_name_mappings[rel_name].classify
105
+ end
106
+
107
+ def transaction_if_possible(origin = self, &block)
108
+ if origin.respond_to?(:transaction)
109
+ origin.transaction(&block)
110
+ elsif defined?(ActiveRecord)
111
+ ActiveRecord::Base.transaction(&block)
112
+ else
113
+ yield # no transaction
114
+ end
115
+ end
116
+
117
+ def skipping_outbound_sync_of(records)
118
+ records = Array(records) unless records.is_a?(Enumerable)
119
+ records.each do |record|
120
+ record.instance_variable_set(:@_skip_outbound_sync, true)
121
+ end
122
+ yield records
123
+ ensure
124
+ records.each do |record|
125
+ record.remove_instance_variable(:@_skip_outbound_sync) if record.instance_variable_defined?(:@_skip_outbound_sync)
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -5,6 +5,6 @@ module GoogleSpreadsheets
5
5
  autoload :Row, 'google_spreadsheets/enhanced/row'
6
6
  autoload :Collection, 'google_spreadsheets/enhanced/collection'
7
7
  autoload :NamespacePreservable, 'google_spreadsheets/enhanced/namespace_preservable'
8
- autoload :Synchronizer, 'google_spreadsheets/enhanced/synchronizer'
8
+ autoload :Syncing, 'google_spreadsheets/enhanced/syncing'
9
9
  end
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheets
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
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.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chihiro Ito
@@ -9,62 +9,62 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-06 00:00:00.000000000 Z
12
+ date: 2014-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activeresource
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - '>='
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - '>='
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activesupport
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - '>='
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - '>='
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: bundler
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - "~>"
46
+ - - ~>
47
47
  - !ruby/object:Gem::Version
48
48
  version: '1.3'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - "~>"
53
+ - - ~>
54
54
  - !ruby/object:Gem::Version
55
55
  version: '1.3'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rake
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ">="
60
+ - - '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ">="
67
+ - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  description: Google Spreadsheets accessor with ActiveResource
@@ -74,7 +74,7 @@ executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
- - ".gitignore"
77
+ - .gitignore
78
78
  - Gemfile
79
79
  - LICENSE.txt
80
80
  - README.md
@@ -89,7 +89,7 @@ files:
89
89
  - lib/google_spreadsheets/enhanced/namespace_preservable.rb
90
90
  - lib/google_spreadsheets/enhanced/row.rb
91
91
  - lib/google_spreadsheets/enhanced/spreadsheet.rb
92
- - lib/google_spreadsheets/enhanced/synchronizer.rb
92
+ - lib/google_spreadsheets/enhanced/syncing.rb
93
93
  - lib/google_spreadsheets/enhanced/worksheet.rb
94
94
  - lib/google_spreadsheets/g_data_format.rb
95
95
  - lib/google_spreadsheets/link_relations.rb
@@ -107,17 +107,17 @@ require_paths:
107
107
  - lib
108
108
  required_ruby_version: !ruby/object:Gem::Requirement
109
109
  requirements:
110
- - - ">="
110
+ - - '>='
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ">="
115
+ - - '>='
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
119
  rubyforge_project:
120
- rubygems_version: 2.2.0
120
+ rubygems_version: 2.1.11
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: Google Spreadsheets accessor with ActiveResource
@@ -1,54 +0,0 @@
1
- module GoogleSpreadsheets
2
- module Enhanced
3
- module Synchronizer
4
-
5
- # === Example
6
- # class User < ActiveRecord::Base
7
- # extend GoogleSpreadsheets::Enhanced::Synchronizer
8
- # sync_with :user_rows, spreadsheet_id: 'xxxx',
9
- # worksheet_title: 'users'
10
- # after_commit :sync_user_row
11
- def sync_with(rows_name, options)
12
- options.assert_valid_keys(:spreadsheet_id, :worksheet_title, :class_name)
13
- options[:worksheet_title] ||= rows_name.to_s
14
- spreadsheet_class_name = LinkRelations.class_name_mappings['http://schemas.google.com/spreadsheets/2006#spreadsheet'].classify
15
- define_singleton_method(rows_name) do
16
- @worksheet ||= spreadsheet_class_name.constantize
17
- .find(options[:spreadsheet_id])
18
- .worksheets
19
- .find_by!(title: options[:worksheet_title])
20
- @worksheet.rows
21
- end
22
-
23
- # import all
24
- define_singleton_method("sync_with_#{rows_name}") do
25
- rows = send(rows_name)
26
- rows.each do |row|
27
- record = self.find_or_initialize_by(id: row.id)
28
- row.aliased_attributes.each do |attr|
29
- record.send("#{attr}=", row.send(attr))
30
- end
31
- record.instance_variable_set(:@_skip_outbound_sync, true)
32
- record.save
33
- record.remove_instance_variable(:@_skip_outbound_sync)
34
- end
35
- end
36
-
37
- # export one
38
- define_method("sync_#{rows_name.to_s.singularize}") do
39
- return if @_skip_outbound_sync
40
- row = self.class.send(rows_name).find(self.id)
41
- if destroyed?
42
- row.destroy
43
- else
44
- # TODO: separate by AttributeAssignment
45
- self.attributes_before_type_cast.each do |attr, value|
46
- row.send("#{attr}=", value)
47
- end
48
- row.save
49
- end
50
- end
51
- end
52
- end
53
- end
54
- end