Class: BoundlessGdata
- Inherits:
-
Object
- Object
- BoundlessGdata
- Defined in:
- lib/boundless_gdata.rb
Class Method Summary collapse
- .add_field(f_name, f_type) ⇒ Object
- .create(values) ⇒ Object
- .create_session ⇒ Object
- .create_table(name) ⇒ Object
- .field_index(f_name) ⇒ Object
- .initialize ⇒ Object
- .set_options(opts) ⇒ Object
- .set_options_file(opts) ⇒ Object
- .where(crit) ⇒ Object
Instance Method Summary collapse
Class Method Details
.add_field(f_name, f_type) ⇒ Object
24 25 26 27 |
# File 'lib/boundless_gdata.rb', line 24 def self.add_field(f_name, f_type) @fields ||= [] @fields << [f_name.to_sym, f_type] end |
.create(values) ⇒ Object
38 39 40 41 42 |
# File 'lib/boundless_gdata.rb', line 38 def self.create(values) fields = Hash[*@fields.flatten] raise "Bad parameters" unless values.map { |k, v| fields[k.to_sym] ? v.is_a?(fields[k.to_sym]) : false }.all? @table.add_record(values) end |
.create_session ⇒ Object
44 45 46 |
# File 'lib/boundless_gdata.rb', line 44 def self.create_session GoogleSpreadsheet.login(@options['user'], @options['password']) end |
.create_table(name) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/boundless_gdata.rb', line 52 def self.create_table(name) col = @fields.inject([]) { |t, x| t = t << x.first } wk = @sp.add_worksheet(name, 10, (col.length).to_i) mapping = col.zip(('A'..'Z').to_a + ('AA'..'ZZ').to_a).inject({}) { |h, p| h[p.last] = p.first; h } # Creates the hash {'A'=>title1 , 'B'=>title2} table = wk.add_table(name, ' ', mapping, @t_options) [wk, table] end |
.field_index(f_name) ⇒ Object
29 30 31 32 |
# File 'lib/boundless_gdata.rb', line 29 def self.field_index(f_name) @fields.each_with_index { |(k, t), i| return i + 1 if k == f_name.to_sym } nil end |
.initialize ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/boundless_gdata.rb', line 11 def self.initialize raise "options not specified" unless @options and !@options.empty? @session = create_session @sp = @session.spreadsheet_by_key(@options['URLkey']) worksheets = @sp.worksheets @wk = @sp.worksheets.find { |w| w.title == self.name } if @wk @table = @wk.tables.last else @wk, @table = create_table(self.name) end end |
.set_options(opts) ⇒ Object
34 35 36 |
# File 'lib/boundless_gdata.rb', line 34 def self.(opts) @t_options = {:header_row => 1, :num_rows => 1, :start_row => 2}.merge(opts) end |
.set_options_file(opts) ⇒ Object
7 8 9 |
# File 'lib/boundless_gdata.rb', line 7 def self.(opts) @options = opts end |
.where(crit) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/boundless_gdata.rb', line 61 def self.where(crit) all_rows = @table.records all_rows.each_with_index do |r, i| if crit.map { |k, v| r[k.to_s] == v.to_s }.all? return TableRecord.new(@wk, r, i + @t_options[:start_row], self) end end nil end |
Instance Method Details
#create_sp(name) ⇒ Object
48 49 50 |
# File 'lib/boundless_gdata.rb', line 48 def create_sp(name) @sp = @session.create_spreadsheet(name) end |