Class: Google::Base
- Inherits:
-
Object
- Object
- Google::Base
- Defined in:
- lib/google/base.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Instance Method Summary collapse
-
#id_column ⇒ Object
Name of the column that’ll will be used as the ID column.
-
#initialize(doc_id, row_id = nil) ⇒ Base
constructor
The standard initialiser takes the spreadsheet ID and an optional record ID (check the Readme to see how records are mapped).
-
#new_record? ⇒ Boolean
Returns true if the record is not yet pushed to the spreadsheet.
-
#save ⇒ Object
Creates or updates the record.
-
#sync ⇒ Object
Maps all attributes specified in Base#sync_attributes, so that a subsequent Base#save will push the data to the spreadsheet columns.
-
#sync! ⇒ Object
Convenience method with executes Base#sync and then Base#save.
-
#sync_attributes ⇒ Object
specify how attributes are mapped to the spreadsheet.
-
#worksheet_name ⇒ Object
Name of the worksheet that’s mapped to the model.
Constructor Details
#initialize(doc_id, row_id = nil) ⇒ Base
The standard initialiser takes the spreadsheet ID and an optional record ID (check the Readme to see how records are mapped). Overwrite the initialiser in order to pass in other necessary information (eg. as in Log).
If a record ID is specified, then the associated row from the spreadsheet will be fetched, otherwise a new record is build.
24 25 26 27 28 29 30 31 |
# File 'lib/google/base.rb', line 24 def initialize(doc_id, row_id = nil) raise Google::MissingDocumentError unless doc_id @sheet = Spreadsheet.new(doc_id) @worksheet_id = @sheet.worksheet_id_for(worksheet_name) initialize_row row_id end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (protected)
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/google/base.rb', line 94 def method_missing(method, *args, &block) method = method.to_s if method[-1, 1] == "=" && args.size == 1 set method[0..-2], *args elsif args.empty? get method else raise NoMethodError end end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
16 17 18 |
# File 'lib/google/base.rb', line 16 def doc @doc end |
Instance Method Details
#id_column ⇒ Object
Name of the column that’ll will be used as the ID column.
Overwrite this in your sub-class!
return nil
in your subclass, if you want a push only model
59 60 61 |
# File 'lib/google/base.rb', line 59 def id_column raise "Abstract! Overwrite this method in your subclass" end |
#new_record? ⇒ Boolean
Returns true if the record is not yet pushed to the spreadsheet.
43 44 45 |
# File 'lib/google/base.rb', line 43 def new_record? !@doc.css("id").first end |
#save ⇒ Object
Creates or updates the record.
34 35 36 37 38 39 40 |
# File 'lib/google/base.rb', line 34 def save if new_record? @sheet.add_row @doc, @worksheet_id else @sheet.update_row @doc, @worksheet_id end end |
#sync ⇒ Object
Maps all attributes specified in Base#sync_attributes, so that a subsequent Base#save will push the data to the spreadsheet columns.
80 81 82 83 84 |
# File 'lib/google/base.rb', line 80 def sync sync_attributes.each do |field, value| set field, value end end |
#sync! ⇒ Object
Convenience method with executes Base#sync and then Base#save.
87 88 89 90 |
# File 'lib/google/base.rb', line 87 def sync! sync save end |
#sync_attributes ⇒ Object
specify how attributes are mapped to the spreadsheet.
Example
{
:timestamp => Time.now.to_s(:db),
:message => @message
}
The keys in the hash represent columns in the spreadsheet (check out the Readme, for more information about attribute mapping), the values will be written to the cells.
74 75 76 |
# File 'lib/google/base.rb', line 74 def sync_attributes { } end |
#worksheet_name ⇒ Object
Name of the worksheet that’s mapped to the model.
Overwrite this in your sub-class!
50 51 52 |
# File 'lib/google/base.rb', line 50 def worksheet_name raise "Abstract! Overwrite this method in your subclass" end |