Permalink
Browse files

merged simple_style into master

  • Loading branch information...
2 parents 778f6ec + 0bebf4c commit 81f330f4a8d01239a6649c42d50ea6f6fb8d8d06 @kikito kikito committed Jan 11, 2012
Showing with 46 additions and 15 deletions.
  1. +1 −0 .gitignore
  2. +0 −2 Gemfile
  3. +8 −1 README.rdoc
  4. +13 −1 lib/to_xls/enumerable_writer.rb
  5. +11 −3 spec/enumerable_writer_spec.rb
  6. +13 −8 spec/spec_helper.rb
View
1 .gitignore
@@ -1,4 +1,5 @@
*.gem
.bundle
+# bundler generated
Gemfile.lock
pkg/*
View
2 Gemfile
@@ -1,4 +1,2 @@
source "http://rubygems.org"
-
-# Specify your gem's dependencies in to_xls.gemspec
gemspec
View
9 README.rdoc
@@ -13,6 +13,14 @@ This gem transform an Array or Hash into a excel file using the spreadsheet gem.
@users.to_xls(:columns => [:name, {:company => [:name, :address]}]) # able to pick associations/called methods
@users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => [:name, :company, :address]) # provide better names for the associated columns
+== Simple Format
+This gem includes a simplified way to change the format of the cells. Also this gem provides a simplified way for changing the format of the headers.
+
+To change the format of the cells and headers the code should be like this:
+
+ User.all.to_xls :cell_format => {:color => :blue}, :header_format => {:weight => :bold, :color => :red}
+
+To have a more specific description of the format admited for the cells and headers you should have a look {on the source code of spreadsheet gem}[http://scm.ywesee.com/?p=spreadsheet/.git;a=blob;f=lib/spreadsheet/format.rb;h=ceef6bb6618238d1ff5cc0462826e2c127c272f1;hb=HEAD]. This gem runs under to_xls, so the format supported by this gem are the ones of spreadsheet.
== Example of use in Rails
@@ -85,4 +93,3 @@ The options of ArrayWriter.new(array, options) are the same as in to_xls.
Copyright (c) 2011 Enrique García Cota. See LICENSE.txt for
further details.
-
View
14 lib/to_xls/enumerable_writer.rb
@@ -8,6 +8,8 @@ class EnumerableWriter
def initialize(array, options = {})
@array = array
@options = options
+ @cell_format = create_format :cell_format
+ @header_format = create_format :header_format
end
def write_string(string = '')
@@ -34,12 +36,14 @@ def write_sheet(sheet)
row_index = 0
if headers_should_be_included?
+ apply_format_to_row(sheet.row(0), @header_format)
fill_row(sheet.row(0), headers)
row_index = 1
end
@array.each do |model|
row = sheet.row(row_index)
+ apply_format_to_row(row, @cell_format)
fill_row(row, columns, model)
row_index += 1
end
@@ -54,7 +58,7 @@ def columns
end
def can_get_columns_from_first_element?
- @array.first &&
+ @array.first &&
@array.first.respond_to?(:attributes) &&
@array.first.attributes.respond_to?(:keys) &&
@array.first.attributes.keys.is_a?(Array)
@@ -77,6 +81,14 @@ def headers_should_be_included?
private
+ def apply_format_to_row(row, format)
+ row.default_format = format if format
+ end
+
+ def create_format(name)
+ Spreadsheet::Format.new @options[name] if @options.has_key? name
+ end
+
def fill_row(row, column, model=nil)
case column
when String, Symbol
View
14 spec/enumerable_writer_spec.rb
@@ -158,7 +158,15 @@
end
end
-
-
-
+ describe "formats" do
+ it "accepts the format options" do
+ cell_format = {:color => :blue}
+ header_format = {:weight => :bold, :color => :red}
+ xls = make_book( mock_users,
+ :cell_format => cell_format,
+ :header_format => header_format
+ )
+ check_format( xls.worksheets.first, header_format, cell_format)
+ end
+ end
end
View
21 spec/spec_helper.rb
@@ -3,14 +3,6 @@
require 'rspec'
require 'to_xls'
-# Requires supporting files with custom matchers and macros, etc,
-# in ./support/ and its subdirectories.
-Dir["#{File.dirname(__FILE__)}/support/**.rb", ].each {|f| require f}
-
-RSpec.configure do |config|
-
-end
-
def mock_model(name, attributes)
attributes[:attributes] = attributes.clone
mock(name, attributes)
@@ -47,3 +39,16 @@ def make_book(array, options={})
ToXls::EnumerableWriter.new(array, options).write_book(book)
book
end
+
+def check_format(sheet, header_format, cell_format)
+ sheet.rows.each_with_index do |row, i|
+ hash = i == 0 ? header_format : cell_format
+ compare_hash_format(hash, row.default_format)
+ end
+end
+
+def compare_hash_format(hash, format)
+ hash.each do |key, value|
+ format.font.send(key).should == value
+ end
+end

0 comments on commit 81f330f

Please sign in to comment.