Sha256: cad14c8100a99f8864a6af1b086719e4783027dfe3ca49ababe11ddd9fa3fbfd

Contents?: true

Size: 840 Bytes

Versions: 2

Compression:

Stored size: 840 Bytes

Contents

module SheetWrap
  class Row
    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

    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

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

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

    alias_method :to_hash, :to_h
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sheet_wrap-0.1.1 lib/sheet_wrap/row.rb
sheet_wrap-0.1.0 lib/sheet_wrap/row.rb