Class: SheetsDB::Worksheet

Inherits:
Object
  • Object
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, Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spreadsheet:, google_drive_resource:, type:) ⇒ Worksheet

Returns a new instance of Worksheet.



10
11
12
13
14
# File 'lib/sheets_db/worksheet.rb', line 10

def initialize(spreadsheet:, google_drive_resource:, type:)
  @spreadsheet = spreadsheet
  @google_drive_resource = google_drive_resource
  @type = type
end

Instance Attribute Details

#google_drive_resourceObject (readonly)

Returns the value of attribute google_drive_resource.



8
9
10
# File 'lib/sheets_db/worksheet.rb', line 8

def google_drive_resource
  @google_drive_resource
end

#spreadsheetObject (readonly)

Returns the value of attribute spreadsheet.



8
9
10
# File 'lib/sheets_db/worksheet.rb', line 8

def spreadsheet
  @spreadsheet
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/sheets_db/worksheet.rb', line 8

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



16
17
18
19
20
# File 'lib/sheets_db/worksheet.rb', line 16

def ==(other)
  other.is_a?(self.class) &&
    other.google_drive_resource == google_drive_resource &&
    other.type == type
end

#allObject



78
79
80
# File 'lib/sheets_db/worksheet.rb', line 78

def all
  to_a
end

#attribute_at_row_position(column_name, row_position) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/sheets_db/worksheet.rb', line 38

def attribute_at_row_position(column_name, row_position)
  attribute_definition = attribute_definitions.fetch(column_name, {})
  column = columns[column_name]
  raw_value = read_value_from_google_drive_resource(
    dimensions: [row_position, column.column_position],
    attribute_definition: attribute_definition
  )
end

#attribute_definitionsObject



34
35
36
# File 'lib/sheets_db/worksheet.rb', line 34

def attribute_definitions
  type.attribute_definitions
end

#columnsObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sheets_db/worksheet.rb', line 22

def columns
  @columns ||= begin
    {}.tap { |directory|
      google_drive_resource.rows.first.each_with_index do |name, i|
        unless name == ""
          directory[name.to_sym] = Column.new(name: name.to_sym, column_position: i + 1)
        end
      end
    }
  end
end

#convert_value(raw_value, attribute_definition) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sheets_db/worksheet.rb', line 109

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

#eachObject



71
72
73
74
75
76
# File 'lib/sheets_db/worksheet.rb', line 71

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



97
98
99
100
101
102
103
# File 'lib/sheets_db/worksheet.rb', line 97

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



82
83
84
# File 'lib/sheets_db/worksheet.rb', line 82

def find_by_id(id)
  find_by_ids([id]).first
end

#find_by_ids(ids) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/sheets_db/worksheet.rb', line 86

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

#read_value_from_google_drive_resource(dimensions:, attribute_definition:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sheets_db/worksheet.rb', line 47

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



105
106
107
# File 'lib/sheets_db/worksheet.rb', line 105

def reload!
  google_drive_resource.reload
end

#update_attributes_at_row_position(staged_attributes, row_position:) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/sheets_db/worksheet.rb', line 61

def update_attributes_at_row_position(staged_attributes, row_position:)
  staged_attributes.each do |name, value|
    column = columns[name]
    definition = attribute_definitions[name]
    assignment_value = definition[:multiple] ? value.join(",") : value
    google_drive_resource[row_position, column.column_position] = assignment_value
  end
  google_drive_resource.synchronize
end