ftpmvc 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ftpmvc/formats/csv.rb +52 -0
- data/lib/ftpmvc/formats.rb +1 -0
- data/lib/ftpmvc/version.rb +1 -1
- data/lib/ftpmvc.rb +1 -3
- data/spec/lib/ftpmvc/formats/csv_spec.rb +59 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98cdb7d4a71db92ae64bd575329169da5ceb288e
|
4
|
+
data.tar.gz: ba7456477a66da2a222182edbb89d3d9f2d875ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdf5bea735f2d78df7c5cc0a8e23c1f97eef221df855d4a34915e4bc31ac624b7fb5fecbaf59f17ad55c339627fea2b170eef8b5b4d3009ff40cb45e0370df16
|
7
|
+
data.tar.gz: 6752de0d38b2895fba9f8238fa2ca2c692d6a6b35222eb8ee124fc7fe67b72cc47fcfa620d7275a028d03270232962d97177d1a20d4de4d09ad7ee9aa48c6643
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module FTPMVC
|
5
|
+
module Formats
|
6
|
+
module CSV
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
@@date_format = nil
|
9
|
+
|
10
|
+
def header
|
11
|
+
end
|
12
|
+
|
13
|
+
def name
|
14
|
+
"#{super}.csv"
|
15
|
+
end
|
16
|
+
|
17
|
+
def data
|
18
|
+
::CSV.generate do |csv|
|
19
|
+
csv << header if header.present?
|
20
|
+
rows.each { |row| csv << row.to_a.map { |f| format(f) } }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def format(field)
|
27
|
+
self.class.format(field)
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
def date_format(format)
|
32
|
+
@date_format = format
|
33
|
+
end
|
34
|
+
|
35
|
+
def format(field)
|
36
|
+
case field.class.name
|
37
|
+
when 'Date'
|
38
|
+
format_date(field)
|
39
|
+
else
|
40
|
+
field
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
def format_date(date)
|
47
|
+
date.strftime(@date_format)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'ftpmvc/formats/csv'
|
data/lib/ftpmvc/version.rb
CHANGED
data/lib/ftpmvc.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
require './spec/spec_helper'
|
2
|
+
|
3
|
+
require 'ftpmvc/formats/csv'
|
4
|
+
require 'ftpmvc/file'
|
5
|
+
|
6
|
+
describe FTPMVC::Formats::CSV do
|
7
|
+
let(:csv_file_class) do
|
8
|
+
Class.new(FTPMVC::File) do
|
9
|
+
include FTPMVC::Formats::CSV
|
10
|
+
def rows
|
11
|
+
[['a', 'b', 'c'], ['d', 'e', 'f']]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
let(:csv_file) { csv_file_class.new('test') }
|
16
|
+
describe '#data' do
|
17
|
+
it 'is a CSV string based on #rows' do
|
18
|
+
expect(csv_file.data).to eq "a,b,c\nd,e,f\n"
|
19
|
+
end
|
20
|
+
context 'when #rows is not an array of arrays' do
|
21
|
+
before do
|
22
|
+
row = double('row')
|
23
|
+
allow(row).to receive(:to_a).and_return(['q', 'w', 'e'])
|
24
|
+
allow(csv_file).to receive(:rows).and_return([row])
|
25
|
+
end
|
26
|
+
it 'firstly converts that' do
|
27
|
+
expect(csv_file.data).to eq "q,w,e\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
context 'when #headers is defined' do
|
31
|
+
before do
|
32
|
+
allow(csv_file).to receive(:header).and_return(['X', 'Y', 'Z'])
|
33
|
+
end
|
34
|
+
it 'uses it on first line' do
|
35
|
+
expect(csv_file.data).to eq "X,Y,Z\na,b,c\nd,e,f\n"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
context 'when date_format is defined' do
|
39
|
+
let(:csv_file_class) do
|
40
|
+
Class.new(FTPMVC::File) do
|
41
|
+
include FTPMVC::Formats::CSV
|
42
|
+
date_format '%m/%d/%y'
|
43
|
+
def rows
|
44
|
+
[[Date.parse('2011-01-01')]]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
it 'formats dates' do
|
49
|
+
expect(csv_file.data).to eq "01/01/11\n"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#name' do
|
55
|
+
it 'appends .csv to original name' do
|
56
|
+
expect(csv_file.name).to eq 'test.csv'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ftpmvc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- André Aizim Kelmanson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -135,6 +135,8 @@ files:
|
|
135
135
|
- lib/ftpmvc/driver.rb
|
136
136
|
- lib/ftpmvc/file.rb
|
137
137
|
- lib/ftpmvc/filter.rb
|
138
|
+
- lib/ftpmvc/formats.rb
|
139
|
+
- lib/ftpmvc/formats/csv.rb
|
138
140
|
- lib/ftpmvc/server.rb
|
139
141
|
- lib/ftpmvc/test_helpers.rb
|
140
142
|
- lib/ftpmvc/version.rb
|
@@ -142,6 +144,7 @@ files:
|
|
142
144
|
- spec/lib/ftpmvc/access_filesystem_filter_spec.rb
|
143
145
|
- spec/lib/ftpmvc/application_spec.rb
|
144
146
|
- spec/lib/ftpmvc/directory_spec.rb
|
147
|
+
- spec/lib/ftpmvc/formats/csv_spec.rb
|
145
148
|
- spec/lib/ftpmvc/server_spec.rb
|
146
149
|
- spec/spec_helper.rb
|
147
150
|
homepage: https://github.com/investtools/ftpmvc
|
@@ -173,5 +176,6 @@ test_files:
|
|
173
176
|
- spec/lib/ftpmvc/access_filesystem_filter_spec.rb
|
174
177
|
- spec/lib/ftpmvc/application_spec.rb
|
175
178
|
- spec/lib/ftpmvc/directory_spec.rb
|
179
|
+
- spec/lib/ftpmvc/formats/csv_spec.rb
|
176
180
|
- spec/lib/ftpmvc/server_spec.rb
|
177
181
|
- spec/spec_helper.rb
|