Class: SheetWrap::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/sheet_wrap/row.rb

Instance Method Summary collapse

Constructor Details

#initialize(worksheet, row_num) ⇒ Row

Returns a new instance of Row.



3
4
5
6
7
8
9
# File 'lib/sheet_wrap/row.rb', line 3

def initialize(worksheet, row_num)
  @worksheet = worksheet
  @row_num = row_num
  @headers = worksheet.headers
  @getter_names = worksheet.headers.map(&:to_sym)
  @setter_names = worksheet.headers.map{|header| :"#{header}=" }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/sheet_wrap/row.rb', line 11

def method_missing(name, *args)
  if (index = @getter_names.index(name))
    return @worksheet[@row_num, index + 1]
  elsif (index = @setter_names.index(name))
    return @worksheet[@row_num, index + 1] = args.first
  end
  super
end

Instance Method Details

#save(args = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/sheet_wrap/row.rb', line 20

def save(args = {})
  args.each do |key, value|
    self.send("#{key}=", value)
  end
  @worksheet.save
end

#to_hObject Also known as: to_hash



27
28
29
# File 'lib/sheet_wrap/row.rb', line 27

def to_h
  @getter_names.each_with_object({}){|name, h| h[name] =  self.send(:"#{name}") }
end