bmg 0.17.7 → 0.17.8
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.
- checksums.yaml +4 -4
- data/lib/bmg/relation.rb +3 -2
- data/lib/bmg/support.rb +1 -0
- data/lib/bmg/support/output_preferences.rb +44 -0
- data/lib/bmg/version.rb +1 -1
- data/lib/bmg/writer/csv.rb +18 -7
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 600739b827185e0d02252824b7b5616662a378b1c907a632ba695536e76d631e
|
4
|
+
data.tar.gz: f327e31860ef19e0ab8177ebd0e3d0c927c003cb25b8e48af202145cdf1ceece
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cd09c0006211c6db5d220ee2371669bd3e29bf32c71eae9fb9ba2c4698392ed172412bbd71c9eded5df34172ab469aa6d20a4117ef263955ed73e8a128cf856
|
7
|
+
data.tar.gz: 8777307569c78001741a597b3ecd9974aa1ee94780495d86f97af38313dcd185c9516d43b9c1635cae5e57606c7422d3e6b3f4889be0b2adcdfeb9f2c30dee24
|
data/lib/bmg/relation.rb
CHANGED
@@ -113,9 +113,10 @@ module Bmg
|
|
113
113
|
# When no string_or_io is used, the method uses a string.
|
114
114
|
#
|
115
115
|
# The method always returns the string_or_io.
|
116
|
-
def to_csv(options = {}, string_or_io = nil)
|
116
|
+
def to_csv(options = {}, string_or_io = nil, preferences = nil)
|
117
117
|
options, string_or_io = {}, options unless options.is_a?(Hash)
|
118
|
-
|
118
|
+
string_or_io, preferences = nil, string_or_io if string_or_io.is_a?(Hash)
|
119
|
+
Writer::Csv.new(options, preferences).call(self, string_or_io)
|
119
120
|
end
|
120
121
|
|
121
122
|
# Converts to an sexpr expression.
|
data/lib/bmg/support.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Bmg
|
2
|
+
class OutputPreferences
|
3
|
+
|
4
|
+
DEFAULT_PREFS = {
|
5
|
+
attributes_ordering: nil,
|
6
|
+
extra_attributes: :after
|
7
|
+
}
|
8
|
+
|
9
|
+
def initialize(options)
|
10
|
+
@options = DEFAULT_PREFS.merge(options)
|
11
|
+
end
|
12
|
+
attr_reader :options
|
13
|
+
|
14
|
+
def self.dress(arg)
|
15
|
+
return arg if arg.is_a?(OutputPreferences)
|
16
|
+
arg = {} if arg.nil?
|
17
|
+
new(arg)
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes_ordering
|
21
|
+
options[:attributes_ordering]
|
22
|
+
end
|
23
|
+
|
24
|
+
def extra_attributes
|
25
|
+
options[:extra_attributes]
|
26
|
+
end
|
27
|
+
|
28
|
+
def order_attrlist(attrlist)
|
29
|
+
return attrlist if attributes_ordering.nil?
|
30
|
+
index = Hash[attributes_ordering.each_with_index.to_a]
|
31
|
+
attrlist.sort{|a,b|
|
32
|
+
ai, bi = index[a], index[b]
|
33
|
+
if ai && bi
|
34
|
+
ai <=> bi
|
35
|
+
elsif ai
|
36
|
+
extra_attributes == :after ? -1 : 1
|
37
|
+
else
|
38
|
+
extra_attributes == :after ? 1 : -1
|
39
|
+
end
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
end # class OutputPreferences
|
44
|
+
end # module Bmg
|
data/lib/bmg/version.rb
CHANGED
data/lib/bmg/writer/csv.rb
CHANGED
@@ -6,26 +6,37 @@ module Bmg
|
|
6
6
|
DEFAULT_OPTIONS = {
|
7
7
|
}
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
@
|
9
|
+
def initialize(csv_options, output_preferences = nil)
|
10
|
+
@csv_options = DEFAULT_OPTIONS.merge(csv_options)
|
11
|
+
@output_preferences = OutputPreferences.dress(output_preferences)
|
11
12
|
end
|
12
|
-
attr_reader :
|
13
|
+
attr_reader :csv_options, :output_preferences
|
13
14
|
|
14
15
|
def call(relation, string_or_io = nil)
|
15
16
|
require 'csv'
|
16
17
|
string_or_io, to_s = string_or_io.nil? ? [StringIO.new, true] : [string_or_io, false]
|
17
|
-
headers = relation.type
|
18
|
-
csv = nil
|
18
|
+
headers, csv = infer_headers(relation.type), nil
|
19
19
|
relation.each do |tuple|
|
20
20
|
if csv.nil?
|
21
|
-
headers = tuple
|
22
|
-
csv = CSV.new(string_or_io,
|
21
|
+
headers = infer_headers(tuple) if headers.nil?
|
22
|
+
csv = CSV.new(string_or_io, csv_options.merge(headers: headers))
|
23
23
|
end
|
24
24
|
csv << headers.map{|h| tuple[h] }
|
25
25
|
end
|
26
26
|
to_s ? string_or_io.string : string_or_io
|
27
27
|
end
|
28
28
|
|
29
|
+
private
|
30
|
+
|
31
|
+
def infer_headers(from)
|
32
|
+
attrlist = if from.is_a?(Type) && from.knows_attrlist?
|
33
|
+
from.to_attrlist
|
34
|
+
elsif from.is_a?(Hash)
|
35
|
+
from.keys
|
36
|
+
end
|
37
|
+
attrlist ? output_preferences.order_attrlist(attrlist) : nil
|
38
|
+
end
|
39
|
+
|
29
40
|
end # class Csv
|
30
41
|
end # module Writer
|
31
42
|
end # module Bmg
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bmg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.17.
|
4
|
+
version: 0.17.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
@@ -260,6 +260,7 @@ files:
|
|
260
260
|
- lib/bmg/summarizer/variance.rb
|
261
261
|
- lib/bmg/support.rb
|
262
262
|
- lib/bmg/support/keys.rb
|
263
|
+
- lib/bmg/support/output_preferences.rb
|
263
264
|
- lib/bmg/support/tuple_algebra.rb
|
264
265
|
- lib/bmg/support/tuple_transformer.rb
|
265
266
|
- lib/bmg/type.rb
|