lib/workbook/row.rb in workbook-0.4.6.0 vs lib/workbook/row.rb in workbook-0.4.7

- old
+ new

@@ -1,8 +1,11 @@ # -*- encoding : utf-8 -*- + module Workbook class Row < Array + include Workbook::Modules::Cache + alias_method :compare_without_header, :<=> attr_accessor :placeholder # The placeholder attribute is used in compares (corresponds to newly created or removed lines (depending which side you're on) attr_accessor :format # Initialize a new row @@ -55,46 +58,84 @@ @table = t table.push(self) #unless table.index(self) and self.placeholder? end end + # Add cell + # @param [Workbook::Cell, Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] cell or value to add + def push(cell) + cell = Workbook::Cell.new(cell) unless cell.class == Workbook::Cell + super(cell) + end + + # Add cell + # @param [Workbook::Cell, Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] cell or value to add + def <<(cell) + cell = Workbook::Cell.new(cell) unless cell.class == Workbook::Cell + super(cell) + end + + # plus + # @param [Workbook::Row, Array] row to add + # @return [Workbook::Row] a new row, not linked to the table + def +(row) + rv = super(row) + rv = Workbook::Row.new(rv) unless rv.class == Workbook::Row + return rv + end + + # concat + # @param [Workbook::Row, Array] row to add + # @return [self] self + def concat(row) + row = Workbook::Row.new(row) unless row.class == Workbook::Row + super(row) + end + + # Overrides normal Array's []-function with support for symbols that identify a column based on the header-values # # @example Lookup using fixnum or header value encoded as symbol # row[1] #=> <Cell value="a"> # row[:a] #=> <Cell value="a"> # - # @param [Fixnum, Symbol] index_or_hash + # @param [Fixnum, Symbol, String] index_or_hash that identifies the column (strings are converted to symbols) # @return [Workbook::Cell, nil] def [](index_or_hash) if index_or_hash.is_a? Symbol rv = nil begin rv = to_hash[index_or_hash] rescue NoMethodError end return rv + elsif index_or_hash.is_a? String + symbolized = Workbook::Cell.new(index_or_hash).to_sym + self[symbolized] else if index_or_hash return to_a[index_or_hash] end end end # Overrides normal Array's []=-function with support for symbols that identify a column based on the header-values # - # @example Lookup using fixnum or header value encoded as symbol + # @example Lookup using fixnum or header value encoded as symbol (strings are converted to symbols) # row[1] #=> <Cell value="a"> # row[:a] #=> <Cell value="a"> # - # @param [Fixnum, Symbol] index_or_hash + # @param [Fixnum, Symbol, String] index_or_hash that identifies the column # @param [String, Fixnum, NilClass, Date, DateTime, Time, Float] value # @return [Workbook::Cell, nil] def []= (index_or_hash, value) index = index_or_hash if index_or_hash.is_a? Symbol index = table_header_keys.index(index_or_hash) + elsif index_or_hash.is_a? String + symbolized = Workbook::Cell.new(index_or_hash).to_sym + index = table_header_keys.index(symbolized) end value_celled = Workbook::Cell.new if value.is_a? Workbook::Cell value_celled = value @@ -142,10 +183,12 @@ end # Converts a row to an array of symbol representations of the row content, see also: Workbook::Cell#to_sym # @return [Array<Symbol>] returns row as an array of symbols def to_symbols - collect{|c| c.to_sym} + fetch_cache(:to_symbols){ + collect{|c| c.to_sym} + } end # Converts the row to an array of Workbook::Cell's # @return [Array<Workbook::Cell>] returns row as an array of symbols def to_a