Class: SheetsDB::Worksheet
- Inherits:
-
Object
- Object
- SheetsDB::Worksheet
- Includes:
- Enumerable
- Defined in:
- lib/sheets_db/worksheet.rb,
lib/sheets_db/worksheet/row.rb,
lib/sheets_db/worksheet/column.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#google_drive_resource ⇒ Object
readonly
Returns the value of attribute google_drive_resource.
-
#spreadsheet ⇒ Object
readonly
Returns the value of attribute spreadsheet.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #all ⇒ Object
- #attribute_at_row_position(attribute_name, row_position) ⇒ Object
- #attribute_definitions ⇒ Object
- #columns ⇒ Object
- #convert_value(raw_value, attribute_definition) ⇒ Object
- #each ⇒ Object
- #find_by_attribute(attribute_name, value) ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_by_ids(ids) ⇒ Object
- #get_definition_and_column(attribute_name) ⇒ Object
- #hash ⇒ Object
-
#initialize(spreadsheet:, google_drive_resource:, type:) ⇒ Worksheet
constructor
A new instance of Worksheet.
- #read_value_from_google_drive_resource(dimensions:, attribute_definition:) ⇒ Object
- #reload! ⇒ Object
- #update_attributes_at_row_position(staged_attributes, row_position:) ⇒ Object
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_resource ⇒ Object (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 |
#spreadsheet ⇒ Object (readonly)
Returns the value of attribute spreadsheet.
8 9 10 |
# File 'lib/sheets_db/worksheet.rb', line 8 def spreadsheet @spreadsheet end |
#type ⇒ Object (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 Also known as: eql?
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 |
#all ⇒ Object
90 91 92 |
# File 'lib/sheets_db/worksheet.rb', line 90 def all to_a end |
#attribute_at_row_position(attribute_name, row_position) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/sheets_db/worksheet.rb', line 52 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
40 41 42 |
# File 'lib/sheets_db/worksheet.rb', line 40 def attribute_definitions type.attribute_definitions end |
#columns ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sheets_db/worksheet.rb', line 28 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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sheets_db/worksheet.rb', line 121 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
83 84 85 86 87 88 |
# File 'lib/sheets_db/worksheet.rb', line 83 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
109 110 111 112 113 114 115 |
# File 'lib/sheets_db/worksheet.rb', line 109 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
94 95 96 |
# File 'lib/sheets_db/worksheet.rb', line 94 def find_by_id(id) find_by_ids([id]).first end |
#find_by_ids(ids) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sheets_db/worksheet.rb', line 98 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
44 45 46 47 48 49 50 |
# File 'lib/sheets_db/worksheet.rb', line 44 def get_definition_and_column(attribute_name) attribute_definition = attribute_definitions.fetch(attribute_name, {}) [ attribute_definition, columns[attribute_definition.fetch(:column_name, attribute_name.to_s)] ] end |
#hash ⇒ Object
24 25 26 |
# File 'lib/sheets_db/worksheet.rb', line 24 def hash [self.class, google_drive_resource, type].hash end |
#read_value_from_google_drive_resource(dimensions:, attribute_definition:) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sheets_db/worksheet.rb', line 60 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
117 118 119 |
# File 'lib/sheets_db/worksheet.rb', line 117 def reload! google_drive_resource.reload end |
#update_attributes_at_row_position(staged_attributes, row_position:) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/sheets_db/worksheet.rb', line 74 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 |