athena 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to athena version 0.1.0
5
+ This documentation refers to athena version 0.1.1
6
6
 
7
7
 
8
8
  == DESCRIPTION
data/bin/athena CHANGED
@@ -168,6 +168,12 @@ if Athena.deferred_output?(format)
168
168
  }.flatten.sort.uniq.each { |line|
169
169
  options[:output].puts line
170
170
  }
171
+ elsif Athena.raw_output?(format)
172
+ res = Athena.with_format(format, options[:output]) { |_format|
173
+ parser.parse(options[:input]) { |record|
174
+ record.to(_format)
175
+ }
176
+ }
171
177
  else
172
178
  res = Athena.with_format(format) { |_format|
173
179
  parser.parse(options[:input]) { |record|
data/lib/athena.rb CHANGED
@@ -72,8 +72,12 @@ module Athena
72
72
  Formats[:out, format].deferred?
73
73
  end
74
74
 
75
- def with_format(format, &block)
76
- Formats[:out, format].wrap(&block)
75
+ def raw_output?(format)
76
+ Formats[:out, format].raw?
77
+ end
78
+
79
+ def with_format(format, *args, &block)
80
+ Formats[:out, format].wrap(*args, &block)
77
81
  end
78
82
 
79
83
  end
@@ -113,6 +113,10 @@ module Athena::Formats
113
113
  false
114
114
  end
115
115
 
116
+ def raw?
117
+ false
118
+ end
119
+
116
120
  end
117
121
 
118
122
  class DuplicateFormatDefinitionError < StandardError
@@ -30,6 +30,7 @@ require 'forwardable'
30
30
 
31
31
  require 'rubygems'
32
32
 
33
+ require 'builder'
33
34
  require 'xmlstreamin'
34
35
  require 'nuggets/hash/insert'
35
36
 
@@ -39,6 +40,12 @@ module Athena::Formats
39
40
 
40
41
  include Athena::Util
41
42
 
43
+ # <http://www.w3.org/TR/2006/REC-xml-20060816/#NT-Name>
44
+ ELEMENT_START = %r{^[a-zA-Z_:]}
45
+ ELEMENT_CHARS = %q{\w:.-}
46
+
47
+ VALUE_SEPARATOR = '|'
48
+
42
49
  register_format :in do
43
50
 
44
51
  attr_reader :specs, :record_element
@@ -53,16 +60,81 @@ module Athena::Formats
53
60
  REXML::Document.parse_stream(source, listener(&block))
54
61
  end
55
62
 
56
- =begin
57
63
  register_format :out
58
64
 
59
65
  def convert(record)
60
- # ...
66
+ builder.row {
67
+ builder.id record.id
68
+
69
+ record.struct.each { |field, struct|
70
+ if block_given?
71
+ yield field, struct
72
+ else
73
+ builder.tag!(field) {
74
+ struct[:elements].each { |element|
75
+ (struct[:values][element] || []).each { |value|
76
+ value = (value || '').strip
77
+ builder.tag!(element, value) unless value.empty?
78
+ }
79
+ }
80
+ }
81
+ end
82
+ }
83
+ }
84
+ end
85
+
86
+ register_format! :out, 'xml/flat' do
87
+
88
+ def convert(record)
89
+ super { |field, struct|
90
+ strings = struct[:elements].inject([]) { |array, element|
91
+ values = (struct[:values][element] || []).map { |v|
92
+ (v || '').strip
93
+ }.reject { |v| v.empty? }
94
+
95
+ array << (values.empty? ? struct[:empty] : values.join(VALUE_SEPARATOR))
96
+ }
97
+
98
+ builder.tag!(field, struct[:string] % strings)
99
+ }
100
+ end
101
+
102
+ end
103
+
104
+ def wrap(out = nil)
105
+ res = nil
106
+
107
+ builder(:target => out).database {
108
+ res = super()
109
+ }
110
+
111
+ res
112
+ end
113
+
114
+ def raw?
115
+ true
61
116
  end
62
- =end
63
117
 
64
118
  private
65
119
 
120
+ def builder(options = {})
121
+ @builder ||= begin
122
+ builder = Builder::XmlMarkup.new({ :indent => 2 }.merge(options))
123
+ builder.instruct!
124
+
125
+ def builder.method_missing(sym, *args, &block)
126
+ elem = sym.to_s
127
+
128
+ elem.insert(0, '_') unless elem =~ ELEMENT_START
129
+ elem.gsub!(/[^#{ELEMENT_CHARS}]/, '_')
130
+
131
+ super(elem, *args, &block)
132
+ end
133
+
134
+ builder
135
+ end
136
+ end
137
+
66
138
  def setup_specs(config)
67
139
  case @record_element = config.delete(:__record_element)
68
140
  when String
@@ -30,7 +30,7 @@ module Athena::Version
30
30
 
31
31
  MAJOR = 0
32
32
  MINOR = 1
33
- TINY = 0
33
+ TINY = 1
34
34
 
35
35
  class << self
36
36
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: athena
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -68,15 +68,15 @@ licenses: []
68
68
 
69
69
  post_install_message:
70
70
  rdoc_options:
71
- - --line-numbers
72
71
  - --main
73
72
  - README
73
+ - --line-numbers
74
74
  - --inline-source
75
75
  - --title
76
76
  - athena Application documentation
77
+ - --all
77
78
  - --charset
78
79
  - UTF-8
79
- - --all
80
80
  require_paths:
81
81
  - lib
82
82
  required_ruby_version: !ruby/object:Gem::Requirement