file_generator 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.rbc
6
+ .config
7
+ .yardoc
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in file_generator.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # file_generator
2
+
3
+ ## Description
4
+
5
+ Every time we need to generate text files for our applications, we are
6
+ writing code for every format we need to generate.
7
+
8
+ FileGenerator tries to minimize the ammount of code needed to perform
9
+ this task.
10
+
11
+ # How does it work?
12
+
13
+ FileGenerator takes the header, body, and footer file formats as input
14
+ and additionally a hash with the data we want to export.
15
+
16
+ The format is expressed as string as follows:
17
+
18
+ 0 => nombre
19
+ 1 => longitud
20
+ 2 => valor
21
+ 3 => relleno
22
+ 4 => alineado
23
+
24
+
25
+ as for instance:
26
+
27
+ body format "id:3:0:0:D,name:30:: :I,region_id:3:0:0:D"
28
+
29
+
30
+ and the data we want to export is:
31
+
32
+ [{"id"=>1, "region_id"=>7, "name"=>"les Escaldes"},{"id"=>2, "region_id"=>7, "name"=>"Lima"}]
33
+
34
+ FileGenerator will try to match the attribute names with the format
35
+ names, if they dont match, it will assign the value to the line of the
36
+ file to export, otherwise it will take the default value defined in the
37
+ format.
38
+
39
+ It also has names for the format which stablishes a special field as for
40
+ instance:
41
+
42
+ time: puts the current date with the format "·$"·$"·
43
+
44
+ nreg : put the ammount of records in the file in the body
45
+
46
+ ## Usage
47
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "file_generator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "file_generator"
7
+ s.version = FileGenerator::VERSION
8
+ s.authors = ["Martin Aceto"]
9
+ s.email = ["martin.aceto@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Generator of files using a format description}
12
+ s.description = %q{Generator of files using a format description}
13
+
14
+ s.rubyforge_project = "file_generator"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,91 @@
1
+ module FileGenerator
2
+ class Base
3
+
4
+ def initialize()
5
+ end
6
+
7
+ def generate_file(records, headerformat, bodyformat, footerformat)
8
+ file = []
9
+ fh = process_hf(headerformat, records)
10
+ file << fh
11
+ records.each do |record|
12
+ fb = process_body(bodyformat, record)
13
+ file << fb
14
+ end
15
+ ff = process_hf(footerformat, records)
16
+ file << ff
17
+ file = file.join("\n")
18
+ file
19
+ end
20
+
21
+ private
22
+
23
+ def process_hf(format, records)
24
+ row = ''
25
+ attributes = split_format(format)
26
+ attributes.each do |attrib|
27
+ behaviors = split_behaviors(attrib) # 0=>nombre, 1=>longitud, 2=> valor, 3=> relleno, 4 => alineado
28
+ value = ''
29
+ case behaviors[0]
30
+ when 'nreg' then
31
+ value = records.size.to_s
32
+ when 'time' then
33
+ value = Time.now.strftime("%Y%m%d")
34
+ else
35
+ value = behaviors[2]
36
+ end
37
+ if (behaviors[3] != '')
38
+ if (behaviors[4] == 'I')
39
+ row << value.ljust(behaviors[1].to_i, behaviors[3])
40
+ else
41
+ row << value.rjust(behaviors[1].to_i, behaviors[3])
42
+ end
43
+ else
44
+ row << value
45
+ end
46
+ end
47
+ row
48
+ end
49
+
50
+ def process_body(format, record)
51
+ row = ''
52
+ attributes = split_format(format)
53
+ attributes.each do |attrib|
54
+ behaviors = split_behaviors(attrib) # 0=>nombre, 1=>longitud, 2=> valor, 3=> relleno, 4 => alineado
55
+ value = ''
56
+ if record.has_key?(behaviors[0])
57
+ value = record["#{behaviors[0]}"].to_s
58
+ else
59
+ case behaviors[0]
60
+ when 'time' then
61
+ value = Time.now.strftime("%Y%m%d")
62
+ else
63
+ value = behaviors[2]
64
+ end
65
+ end
66
+ if (behaviors[3] != '')
67
+ if (behaviors[4] == 'I')
68
+ row << value.ljust(behaviors[1].to_i, behaviors[3])
69
+ else
70
+ row << value.rjust(behaviors[1].to_i, behaviors[3])
71
+ end
72
+ else
73
+ row << value
74
+ end
75
+ end
76
+ row
77
+ end
78
+
79
+ # split the format in each behaviors
80
+ def split_format(format)
81
+ format.split(',')
82
+ end
83
+
84
+ # split the line pased
85
+ # 0=>nombre, 1=>longitud, 2=> valor, 3=> relleno, 4 => alineado
86
+ def split_behaviors(behavior)
87
+ behavior.split(':')
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module FileGenerator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "file_generator/version"
2
+ require "file_generator/base"
3
+
4
+ module FileGenerator
5
+
6
+ end
data/spec/formats.yml ADDED
@@ -0,0 +1,5 @@
1
+ # 0=>nombre, 1=>longitud, 2=> valor, 3=> relleno, 4 => alineado
2
+ formats:
3
+ headerformat: "treg:2:CC::I,csuc:3:193::I,time:8:0::I"
4
+ bodyformat: "id:3:0:0:D,name:30:: :I,region_id:3:0:0:D"
5
+ footerformat: "pie:2:CC::I,csuc:3:193::I,nreg:10:0:0:D"
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe FileGenerator do
4
+ context "generating file" do
5
+
6
+ let(:file_generator) do
7
+ records = [{"id"=>1, "region_id"=>7, "name"=>"les Escaldes"},{"id"=>2, "region_id"=>7, "name"=>"Lima"}]
8
+ format = load_formats.formats
9
+ f = FileGenerator::Base.new
10
+ f.generate_file(records,format['headerformat'],format['bodyformat'],format['footerformat'])
11
+ #raise f.generate_file(records,format['headerformat'],format['bodyformat'],format['footerformat']).inspect
12
+ end
13
+
14
+ it "should not be empty string" do
15
+ file_generator.should_not be_empty
16
+ end
17
+
18
+ it "header should be match the format" do
19
+ header = file_generator.split("\n")[0]
20
+ header.should match "CC193" + Time.now.strftime("%Y%m%d").to_s
21
+ end
22
+
23
+ it "footer should be match the format" do
24
+ footer = file_generator.split("\n")[3]
25
+ footer.should match "CC1930000000002"
26
+ end
27
+
28
+ it "first line body should be match format" do
29
+ body = file_generator.split("\n")[1]
30
+ body.should match "001les Escaldes 007"
31
+ end
32
+
33
+ it "second line body should be match format" do
34
+ body = file_generator.split("\n")[2]
35
+ body.should match "002Lima 007"
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,5 @@
1
+ require 'file_generator'
2
+ require 'rspec'
3
+
4
+ Dir[File.join(Dir.pwd, "spec/support/**/*.rb")].each {|f| require f}
5
+
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+ require "yaml"
3
+
4
+ module Helpers
5
+
6
+ def load_formats
7
+ formats_file = File.join(Dir.pwd, "/spec/formats.yml")
8
+ if File.exists?(formats_file)
9
+ conf = YAML.load(File.read("#{Dir.pwd}/spec/formats.yml"))
10
+ OpenStruct.new(conf)
11
+ else
12
+ raise "missing formats file. "
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ RSpec.configuration.include Helpers
19
+
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Aceto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Generator of files using a format description
15
+ email:
16
+ - martin.aceto@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - file_generator.gemspec
26
+ - lib/file_generator.rb
27
+ - lib/file_generator/base.rb
28
+ - lib/file_generator/version.rb
29
+ - spec/formats.yml
30
+ - spec/lib/file_generator_spec.rb
31
+ - spec/spec_helper.rb
32
+ - spec/support/helpers.rb
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project: file_generator
53
+ rubygems_version: 1.8.10
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Generator of files using a format description
57
+ test_files:
58
+ - spec/formats.yml
59
+ - spec/lib/file_generator_spec.rb
60
+ - spec/spec_helper.rb
61
+ - spec/support/helpers.rb