Skip to content

Commit

Permalink
csv exporter class almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
chiramiso committed Apr 17, 2015
1 parent dd451b3 commit 9598841
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 68 deletions.
53 changes: 33 additions & 20 deletions app/objects/service/belboon_article_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
require 'exporter'

class BelboonArticleExporter
include CSVExporter

def initialize &block
super
end
@@csv_options = { col_sep: ';', encoding: 'utf-8' }

export_mapping do
field 'Merchant_ProductNumber' => 'slug',
EXPORT_MAPPING = {
'Merchant_ProductNumber' => 'slug',
'EAN_Code' => 'gtin',
'Product_Title' => 'title',
'Manufacturer' => nil,
Expand Down Expand Up @@ -38,20 +32,39 @@ def initialize &block
'Optional_3' => nil,
'Optional_4' => nil,
'Optional_5' => nil
end
}

export_header %w(
Merchant_ProductNumber EAN_Code Product_Title Manufacturer Brand
Price Price_old Currency Valid_From Valid_To DeepLink_URL
Into_Basket_URL Image_Small_URL Image_Small_HEIGHT Image_Small_WIDTH
Image_Large_URL Image_Large_HEIGHT Image_Large_WIDTH Merchant_Product_Category
Keywords Product_Description_Short Product_Description_Long Last_Update
Shipping Availability Optional_1 Optional_2 Optional_3 Optional_4 Optional_5
)
EXPORT_HEADER = %w(Merchant_ProductNumber EAN_Code Product_Title Manufacturer Brand Price Price_old Currency Valid_From Valid_To DeepLink_URL Into_Basket_URL Image_Small_URL Image_Small_HEIGHT Image_Small_WIDTH Image_Large_URL Image_Large_HEIGHT Image_Large_WIDTH Merchant_Product_Category Keywords Product_Description_Short Product_Description_Long Last_Update Shipping Availability Optional_1 Optional_2 Optional_3 Optional_4 Optional_5)

def file_name
Rails.env == 'development' ?
FILE_NAME = Rails.env == 'development' ?
"#{ Rails.root }/public/fairmondo_articles.csv" :
'/var/www/fairnopoly/shared/public/system/fairmondo_articles.csv'

class << self
def export(user)
CSV.open(FILE_NAME, 'wb', @@csv_options) do |line|
line << EXPORT_HEADER

exportable_articles_for(user).find_each do |article|
line << line_for(article)
end
end
end

def line_for article
attrs = []
EXPORT_HEADER.each do |attr|
begin
attrs << "'#{ article.instance_eval(EXPORT_MAPPING[attr]) }'"
rescue
attrs << "'#{ EXPORT_MAPPING[attr] }'"
end
end
attrs
end

def exportable_articles_for user
user.articles.belboon_trackable.includes(:images)
end
end
end
72 changes: 35 additions & 37 deletions app/objects/service/exporter.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module CSVExporter
attr_accessor :export_mapping, :export_header

class Exporter
def initialize &block
instance_eval &block if block
@export_mapping = {}
@file_name = ''
@csv_options ||= { col_sep: ';', encoding: 'utf-8' }

instance_eval(&block) if block_given?
end

def self.included base
base.extend ClassMethods
def build &block
initialize(&block)
end

def export &block
CSV.open(file_name, 'wb', csv_options) do |line|
binding.pry
line << export_header
CSV.open(@file_name, 'wb', @csv_options) do |line|
line << @export_mapping.keys

yield.find_each do |item|
line << generate_line_for(item)
Expand All @@ -22,45 +23,42 @@ def export &block

private

def csv_options
{ col_sep: ';', encoding: 'utf-8' }
end

def generate_line_for item
attrs = []
@export_header.each do |attr|
begin
attrs << "'#{ item.instance_eval(export_mapping[attr]) }'"
rescue
attrs << "'#{ export_mapping[attr] }'"
@export_mapping.each do |_key, value|
case
when empty_or_nil_in?(value)
attrs << value
when string_or_symbol_in?(value)
attrs << item.instance_eval(value.to_s)
when value.is_a?(Proc)
attrs << item.instance_exec(&(value)) if proc_in?(value)
end
end
attrs
end

module ClassMethods
def export_mapping
@export_mapping ||= {}
end
def empty_or_nil_in? value
value == nil || value == ''
end

def export_header
@export_header ||= []
end
def string_or_symbol_in? value
value.is_a?(String) || value.is_a?(Symbol)
end

def mapping &block
yield
end
def mapping &block
yield
end

def field hash = {}
export_mapping.merge! hash
end
def field hash
@export_mapping.merge! hash
end

def header
yield
end
def file_path string
@file_name = string
end

def name value
export_header << value
end
def options hash
@csv_options = hash
end
end
28 changes: 17 additions & 11 deletions app/objects/service/line_item_group_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
require 'exporter'

class LineItemGroupExporter
include CSVExporter
attr_reader :user, :exporter

def initialize &block
super
def initialize user
@user = user
end

mapping do
field 'id' => 'id'
field 'article_title' => 'self.articles.last.title'
end
@exporter = Exporter.new do
mapping do
field id: 'id'
field sold_at: :sold_at
field article_title: 'self.articles.last.title'
field created_at: ->{ created_at }
field hallo: nil
field foobar: ''
field 'hallo' => 'updated_at'
end

header do
name %w(id article_title)
file_path "#{ Rails.root }/public/fairmondo_articles.csv"
end

def file_name
"#{ Rails.root }/public/fairmondo_articles.csv"
def export &block
@exporter.export(block)
end
end

# export { @user.seller_line_item_groups.includes(:business_transactions, :articles, :buyer) }

# @@csv_options = { col_sep: ";", encoding: 'utf-8'}
#
Expand Down
Empty file removed public/fairmondo_articles.csv
Empty file.

0 comments on commit 9598841

Please sign in to comment.