Class: Paperize::Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/paperize/worksheet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, labels, rows) ⇒ Worksheet

Returns a new instance of Worksheet.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/paperize/worksheet.rb', line 11

def initialize(title, labels, rows)
  raise_on_duplicates(labels)

  @title = title
  @labels = labels
  @attributes = @labels.map do |label|
    unless label.nil?
      label.downcase.gsub(' ', '_').gsub('/', '_').to_sym
    end
  end

  @rows = rows.map do |row|
    card = OpenStruct.new

    @attributes.zip(row).each do |attribute_value_pair|
      attribute = attribute_value_pair[0]
      unless attribute.nil? or attribute.empty?
        card.send("#{attribute}=".to_sym, attribute_value_pair[1])
      end
    end

    card
  end      
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/paperize/worksheet.rb', line 10

def attributes
  @attributes
end

#labelsObject (readonly)

Returns the value of attribute labels.



10
11
12
# File 'lib/paperize/worksheet.rb', line 10

def labels
  @labels
end

#rowsObject (readonly)

Returns the value of attribute rows.



10
11
12
# File 'lib/paperize/worksheet.rb', line 10

def rows
  @rows
end

#titleObject (readonly)

Returns the value of attribute title.



10
11
12
# File 'lib/paperize/worksheet.rb', line 10

def title
  @title
end

Class Method Details

.find(options = {}) ⇒ Object



5
6
7
8
# File 'lib/paperize/worksheet.rb', line 5

def self.find(options={})
  spreadsheet = Paperize::Spreadsheet.find(options[:spreadsheet_key])
  spreadsheet.worksheet_by_title(options[:worksheet_title])
end

Instance Method Details

#as_json(option = nil) ⇒ Object



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

def as_json(option=nil)
  {
    title: @title,
    columns: @labels
  }
end

#cardsObject



36
37
38
# File 'lib/paperize/worksheet.rb', line 36

def cards
  rows
end