Module: ModelToGooglesheet::Export

Defined in:
lib/model_to_googlesheet/export.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
# File 'lib/model_to_googlesheet/export.rb', line 13

def self.included(base)
	base.extend ClassMethods
end

Instance Method Details

#delete_from_googlesheet(permethod_options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/model_to_googlesheet/export.rb', line 37

def delete_from_googlesheet permethod_options={}
	options = Configuration.merge_configs permethod_options, model_to_googlesheet_configuration

	session = GoogleDrive::Session.new_for_gs({
		client_id: options[:client_id], 
		client_secret: options[:client_secret], 
		refresh_token: options[:refresh_token]
	})
	ss = session.exact_ss options[:spreadsheet]
	return unless ss
	ws = ss.worksheet_by_title options[:worksheet]
	return unless ws

	record_hash = get_exportable_hash options[:convert_with]

	row = ws.row_with_hash record_hash, find_by: options[:find_by]
	return unless row

	row.clear 
	ws.save
end

#export_to_googlesheet(permethod_options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/model_to_googlesheet/export.rb', line 18

def export_to_googlesheet permethod_options={}
	options = Configuration.merge_configs permethod_options, model_to_googlesheet_configuration

	session = GoogleDrive::Session.new_for_gs({
		client_id: options[:client_id], 
		client_secret: options[:client_secret], 
		refresh_token: options[:refresh_token]
	})
	ss = session.get_or_create_ss options[:spreadsheet]
	ws = ss.get_or_create_ws options[:worksheet]

	record_hash = self.get_exportable_hash options[:convert_with]

	ws.export_hash record_hash, 
		update: options[:update], find_by: options[:find_by]

	ws.save
end

#get_exportable_hash(convert_with) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/model_to_googlesheet/export.rb', line 59

def get_exportable_hash convert_with
	ActiveSupport::HashWithIndifferentAccess.new(
		case convert_with 
		when nil 
			attributes
		when Symbol 
			self.send convert_with
		when Proc
			convert_with.call self
		end
	)
end