Class: SheetsDB::Worksheet
- Inherits:
-
Object
- Object
- SheetsDB::Worksheet
show all
- Includes:
- Enumerable
- Defined in:
- lib/sheets_db/worksheet.rb,
lib/sheets_db/worksheet/row.rb,
lib/sheets_db/worksheet/column.rb
Defined Under Namespace
Classes: Column, ColumnNotFoundError, Row
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(spreadsheet:, google_drive_resource:, type:) ⇒ Worksheet
Returns a new instance of Worksheet.
12
13
14
15
16
|
# File 'lib/sheets_db/worksheet.rb', line 12
def initialize(spreadsheet:, google_drive_resource:, type:)
@spreadsheet = spreadsheet
@google_drive_resource = google_drive_resource
@type = type
end
|
Instance Attribute Details
#google_drive_resource ⇒ Object
Returns the value of attribute google_drive_resource.
10
11
12
|
# File 'lib/sheets_db/worksheet.rb', line 10
def google_drive_resource
@google_drive_resource
end
|
#spreadsheet ⇒ Object
Returns the value of attribute spreadsheet.
10
11
12
|
# File 'lib/sheets_db/worksheet.rb', line 10
def spreadsheet
@spreadsheet
end
|
#type ⇒ Object
Returns the value of attribute type.
10
11
12
|
# File 'lib/sheets_db/worksheet.rb', line 10
def type
@type
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
18
19
20
21
22
|
# File 'lib/sheets_db/worksheet.rb', line 18
def ==(other)
other.is_a?(self.class) &&
other.google_drive_resource == google_drive_resource &&
other.type == type
end
|
#all ⇒ Object
94
95
96
|
# File 'lib/sheets_db/worksheet.rb', line 94
def all
to_a
end
|
#attribute_at_row_position(attribute_name, row_position) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/sheets_db/worksheet.rb', line 56
def attribute_at_row_position(attribute_name, row_position)
attribute_definition, column = get_definition_and_column(attribute_name)
raw_value = read_value_from_google_drive_resource(
dimensions: [row_position, column.column_position],
attribute_definition: attribute_definition
)
end
|
#attribute_definitions ⇒ Object
42
43
44
|
# File 'lib/sheets_db/worksheet.rb', line 42
def attribute_definitions
type.attribute_definitions
end
|
#columns ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/sheets_db/worksheet.rb', line 30
def columns
@columns ||= begin
{}.tap { |directory|
google_drive_resource.rows.first.each_with_index do |name, i|
unless name == ""
directory[name] = Column.new(name: name, column_position: i + 1)
end
end
}
end
end
|
#convert_value(raw_value, attribute_definition) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/sheets_db/worksheet.rb', line 125
def convert_value(raw_value, attribute_definition)
return nil if raw_value == ""
converted_value = case attribute_definition[:type].to_s
when "Integer"
raw_value.to_i
when "DateTime"
DateTime.strptime(raw_value, "%m/%d/%Y %H:%M:%S")
when "Boolean"
{ "TRUE" => true, "FALSE" => false }.fetch(raw_value.upcase, nil)
else
raw_value
end
attribute_definition[:transform] ?
attribute_definition[:transform].call(converted_value) :
converted_value
end
|
#each ⇒ Object
87
88
89
90
91
92
|
# File 'lib/sheets_db/worksheet.rb', line 87
def each
return to_enum(:each) unless block_given?
(google_drive_resource.num_rows - 1).times do |i|
yield type.new(worksheet: self, row_position: i + 2)
end
end
|
#find_by_attribute(attribute_name, value) ⇒ Object
113
114
115
116
117
118
119
|
# File 'lib/sheets_db/worksheet.rb', line 113
def find_by_attribute(attribute_name, value)
definition = attribute_definitions[attribute_name]
select { |item|
attribute = item.send(attribute_name)
definition[:multiple] ? attribute.include?(value) : attribute == value
}
end
|
#find_by_id(id) ⇒ Object
98
99
100
|
# File 'lib/sheets_db/worksheet.rb', line 98
def find_by_id(id)
find_by_ids([id]).first
end
|
#find_by_ids(ids) ⇒ Object
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/sheets_db/worksheet.rb', line 102
def find_by_ids(ids)
result = []
each do |model|
break if result.count == ids.count
if ids.include?(model.id)
result << model
end
end
result
end
|
#get_definition_and_column(attribute_name) ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'lib/sheets_db/worksheet.rb', line 46
def get_definition_and_column(attribute_name)
attribute_definition = attribute_definitions.fetch(attribute_name, {})
column_name = attribute_definition.fetch(:column_name, attribute_name.to_s)
raise ColumnNotFoundError, column_name if columns[column_name].nil?
[
attribute_definition,
columns[column_name]
]
end
|
#hash ⇒ Object
26
27
28
|
# File 'lib/sheets_db/worksheet.rb', line 26
def hash
[self.class, google_drive_resource, type].hash
end
|
#read_value_from_google_drive_resource(dimensions:, attribute_definition:) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/sheets_db/worksheet.rb', line 64
def read_value_from_google_drive_resource(dimensions:, attribute_definition:)
raw_value = case attribute_definition[:type].to_s
when "DateTime"
google_drive_resource.input_value(*dimensions)
else
google_drive_resource[*dimensions]
end
if attribute_definition[:multiple]
raw_value.split(/,\s*/).map { |value| convert_value(value, attribute_definition) }
else
convert_value(raw_value, attribute_definition)
end
end
|
#reload! ⇒ Object
121
122
123
|
# File 'lib/sheets_db/worksheet.rb', line 121
def reload!
google_drive_resource.reload
end
|
#update_attributes_at_row_position(staged_attributes, row_position:) ⇒ Object
78
79
80
81
82
83
84
85
|
# File 'lib/sheets_db/worksheet.rb', line 78
def update_attributes_at_row_position(staged_attributes, row_position:)
staged_attributes.each do |attribute_name, value|
attribute_definition, column = get_definition_and_column(attribute_name)
assignment_value = attribute_definition[:multiple] ? value.join(",") : value
google_drive_resource[row_position, column.column_position] = assignment_value
end
google_drive_resource.synchronize
end
|