Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Võ Anh Duy committed Oct 13, 2013
1 parent 77956cd commit 6162485
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 34 deletions.
23 changes: 12 additions & 11 deletions lib/exporter/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Exporter
class Configuration

def initialize
@exporters = Hash.new
end
Expand All @@ -12,22 +13,22 @@ def register(data_type, export_type, exporter)
end
end

def can_export?(data, export_type)
def index(data, default_value, method)
@exporters.keys.each do |key|
if data.kind_of? key
return true
end
return method.call(key) if data.kind_of?(key)
end
return false
default_value
end

def can_export?(data, export_type)
can_export_proc = Proc.new{|key| @exporters[key][export_type].present?}
index(data, false, can_export_proc)
end

def exporter(data, export_type)
@exporters.keys.each do |key|
if data.kind_of? key
return @exporters[key][export_type]
end
end
return nil
exporter_proc = Proc.new {|key| @exporters[key][export_type]}
index(data, nil, exporter_proc)
end

end
end
55 changes: 55 additions & 0 deletions spec/features/exporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require "spec_helper"

describe Exporter do

before(:all) {FactoryGirl.create(:user)}

let(:data){ User.all }

describe '.export' do
context 'ActiveRecord::Relation' do
it 'can export to csv' do
expect(Exporter.configuration.can_export?(data, :csv)).to eql(true)
end

it 'can export to excel' do
expect(Exporter.configuration.can_export?(data, :excel)).to eql(true)
end

it 'can\'t export not register type' do

This comment has been minimized.

Copy link
@hieuk09

hieuk09 Oct 13, 2013

You can use double quote here: "can't export not register type"

expect(Exporter.configuration.can_export?(data, :some_weird_type)).to eql(false)
end

context ':csv' do
it 'return an Exporter::Document subclass' do
document = Exporter.export(data, :csv)
expect(document).to be_kind_of(Exporter::Document)
end

it 'write to file correct' do
document = Exporter.export(data, :csv)
document.to_file('dummy.csv')
content = File.read('dummy.csv')
expect(content.length).to eql(document.data.length)
File.delete('dummy.csv')
end
end

context ':excel' do
it 'return an Exporter::Document subclass' do
document = Exporter.export(data, :excel)
expect(document).to be_kind_of(Exporter::Document)
end

it 'write to file correct' do
document = Exporter.export(data, :excel)
document.to_file('dummy.xls')
content = Spreadsheet.open('dummy.xls')
expect(content.worksheet(0).last_row_index).to eql(document.data.worksheet(0).last_row_index)
expect(content.worksheet(0).row(1)).to eql(document.data.worksheet(0).row(1))
File.delete('dummy.xls')
end
end
end
end
end
23 changes: 0 additions & 23 deletions spec/lib/exporter_spec.rb

This file was deleted.

0 comments on commit 6162485

Please sign in to comment.