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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41671cac00088b48c072b6e276538989ce92f3583faef903748265558ad7d1d3
4
- data.tar.gz: 32003d306f1d5ea608efea4c2dd875ac935fd205ff4e016e8d8af4afbc7ee1c5
3
+ metadata.gz: 600739b827185e0d02252824b7b5616662a378b1c907a632ba695536e76d631e
4
+ data.tar.gz: f327e31860ef19e0ab8177ebd0e3d0c927c003cb25b8e48af202145cdf1ceece
5
5
  SHA512:
6
- metadata.gz: 5c0fa25d89eefaaed383a8d675e046894ed8cdbc705ffc8a283a523d53d7c89b0879118d6a8163eedb5e60add7fe4dd2594238722946652c93d298c2cfd7f2e2
7
- data.tar.gz: 7b0181f9d426d856f66c51ca010009c834cd4c09f3fa9563013ab06a67181576b13f469a1e4209edc82eff9d0100f3f26fdaafb4e8e21efc667cfaa4b5878371
6
+ metadata.gz: 2cd09c0006211c6db5d220ee2371669bd3e29bf32c71eae9fb9ba2c4698392ed172412bbd71c9eded5df34172ab469aa6d20a4117ef263955ed73e8a128cf856
7
+ data.tar.gz: 8777307569c78001741a597b3ecd9974aa1ee94780495d86f97af38313dcd185c9516d43b9c1635cae5e57606c7422d3e6b3f4889be0b2adcdfeb9f2c30dee24
@@ -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
- Writer::Csv.new(options).call(self, string_or_io)
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.
@@ -1,3 +1,4 @@
1
1
  require_relative 'support/tuple_algebra'
2
2
  require_relative 'support/tuple_transformer'
3
3
  require_relative 'support/keys'
4
+ require_relative 'support/output_preferences'
@@ -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
@@ -2,7 +2,7 @@ module Bmg
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 17
5
- TINY = 7
5
+ TINY = 8
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -6,26 +6,37 @@ module Bmg
6
6
  DEFAULT_OPTIONS = {
7
7
  }
8
8
 
9
- def initialize(options)
10
- @options = DEFAULT_OPTIONS.merge(options)
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 :options
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.to_attrlist if relation.type.knows_attrlist?
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.keys if headers.nil?
22
- csv = CSV.new(string_or_io, options.merge(headers: headers))
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.7
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