fat_table 0.4.0 → 0.4.2

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: dea0adf5d428fa875a7dda51538a79e514b9095b167d392ef06eae849e563a9f
4
- data.tar.gz: d5ac2784c1f569fc1677f596278af3c6a00b1b5736ea1a080667f8a0f0acdd16
3
+ metadata.gz: a0f33bfa7f3d9d9c21cbc214a64c5d4f89d917dc9e704e6f7dae710630ff0907
4
+ data.tar.gz: 9bd6f3ed31da1fd4a120be80e47b1a262c846efefee24e4ed6aee47ccc5d8107
5
5
  SHA512:
6
- metadata.gz: 7ea35509645ae4c11c8f7fd61157fe5057cf5d11237d888b9e5cfcbab437ff5fd90a3d3cffa9bd6d53038406138b12a890c65a72a59ac0b9542c1a90a979158c
7
- data.tar.gz: 04eaf2e05b452efc8f25c14ab51b069d09dd9c548983481731562883d8866beac555a4a48a85b3352d408736c1b22d90881c0a23fab273fd78b459f31f704988
6
+ metadata.gz: 4bb8871cce72b3a70dbdf4ed114395f86d48003f0a6e590e8e3c691913625c085a4e64f067a28b278723804a5fd19deb6f5d8cc0c8ef658958163835168f2a89
7
+ data.tar.gz: efce3072c71e2231fc3768cfae9ad81d0af45a8403e0e17677c3a99b1f5dfdb57e9e69d8bbb27024db17c343e364f34e89bc982abf8d318e29c5b97acad0f5be
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
- --format documentation
2
1
  --color
2
+ --require spec_helper
3
+ --format documentation
@@ -500,7 +500,7 @@ module FatTable
500
500
  format_h = format_h.merge(typ_fmt)
501
501
  end
502
502
  if fmts.key?(:nil)
503
- typ_fmt = parse_nil_fmt(fmts[:nil]).first
503
+ typ_fmt = parse_nilclass_fmt(fmts[:nil]).first
504
504
  format_h = format_h.merge(typ_fmt)
505
505
  end
506
506
  end
@@ -587,7 +587,7 @@ module FatTable
587
587
  fmt = fmt.sub($&, '')
588
588
  end
589
589
  # Nil formatting can apply to strings as well
590
- nil_hash, fmt = parse_nil_fmt(fmt)
590
+ nil_hash, fmt = parse_nilclass_fmt(fmt)
591
591
  fmt_hash = fmt_hash.merge(nil_hash)
592
592
  if fmt =~ /u/
593
593
  fmt_hash[:case] = :lower
@@ -636,7 +636,7 @@ module FatTable
636
636
  # instructions and the unconsumed part of the instruction string. This is
637
637
  # called to cull nil-based instructions from a formatting string intended
638
638
  # for other types, such as numeric, etc.
639
- def parse_nil_fmt(fmt, _strict: true)
639
+ def parse_nilclass_fmt(fmt, _strict: true)
640
640
  # We parse the more complex formatting constructs first, and after each
641
641
  # parse, we remove the matched construct from fmt. At the end, any
642
642
  # remaining characters in fmt should be invalid.
@@ -855,11 +855,16 @@ module FatTable
855
855
  result = val.secs_to_hms
856
856
  istruct.commas = false
857
857
  elsif istruct.currency
858
- prec = istruct.post_digits.zero? ? 2 : istruct.post_digits
859
858
  delim = istruct.commas ? ',' : ''
860
- result = val.to_s(:currency, precision: prec, delimiter: delim,
859
+ result =
860
+ if istruct.post_digits < 0
861
+ val.to_s(:currency, delimiter: delim,
861
862
  unit: FatTable.currency_symbol)
862
- istruct.commas = false
863
+ else
864
+ val.to_s(:currency, precision: istruct.post_digits, delimiter: delim,
865
+ unit: FatTable.currency_symbol)
866
+ end
867
+ # istruct.commas = false
863
868
  elsif istruct.pre_digits.positive?
864
869
  if val.whole?
865
870
  # No fractional part, ignore post_digits
@@ -67,6 +67,23 @@ module FatTable
67
67
  @boundaries = []
68
68
  end
69
69
 
70
+ # :category: Constructors
71
+
72
+ # Return an empty duplicate of self. This allows the library to create an
73
+ # empty table that preserves all the instance variables from self. Even
74
+ # though FatTable::Table objects have no instance variables, a class that
75
+ # inherits from it might.
76
+ def empty_dup
77
+ self.dup.__empty!
78
+ end
79
+
80
+ def __empty!
81
+ @columns = []
82
+ @boundaries = []
83
+ self
84
+ end
85
+
86
+
70
87
  # :category: Constructors
71
88
 
72
89
  # Construct a Table from the contents of a CSV file named +fname+. Headers
@@ -597,8 +614,12 @@ module FatTable
597
614
  key1 <=> key2
598
615
  end
599
616
  # Add the new rows to the table, but mark a group boundary at the points
600
- # where the sort key changes value.
601
- new_tab = Table.new
617
+ # where the sort key changes value. NB: I use self.class.new here
618
+ # rather than Table.new because if this class is inherited, I want the
619
+ # new_tab to be an instance of the subclass. With Table.new, this
620
+ # method's result will be an instance of FatTable::Table rather than of
621
+ # the subclass.
622
+ new_tab = empty_dup
602
623
  last_key = nil
603
624
  new_rows.each_with_index do |nrow, k|
604
625
  new_tab << nrow
@@ -713,7 +734,7 @@ module FatTable
713
734
  before: before_hook,
714
735
  after: after_hook)
715
736
  # Compute the new Table from this Table
716
- result = Table.new
737
+ result = empty_dup
717
738
  normalize_boundaries
718
739
  rows.each_with_index do |old_row, old_k|
719
740
  # Set the group number in the before hook and run the hook with the
@@ -770,7 +791,7 @@ module FatTable
770
791
  # tab.where('@row.even? && shares > 500') => even rows with lots of shares
771
792
  def where(expr)
772
793
  expr = expr.to_s
773
- result = Table.new
794
+ result = empty_dup
774
795
  headers.each do |h|
775
796
  col = Column.new(header: h)
776
797
  result.add_column(col)
@@ -792,7 +813,7 @@ module FatTable
792
813
  # Return a new table with all duplicate rows eliminated. Resets groups. Same
793
814
  # as #uniq.
794
815
  def distinct
795
- result = Table.new
816
+ result = empty_dup
796
817
  uniq_rows = rows.uniq
797
818
  uniq_rows.each do |row|
798
819
  result << row
@@ -904,7 +925,7 @@ module FatTable
904
925
  raise UserError, msg
905
926
  end
906
927
  other_rows = other.rows.map { |r| r.replace_keys(headers) }
907
- result = Table.new
928
+ result = empty_dup
908
929
  new_rows = rows.send(oper, other_rows)
909
930
  new_rows.each_with_index do |row, k|
910
931
  result << row
@@ -1011,7 +1032,7 @@ module FatTable
1011
1032
  join_exp, other_common_heads =
1012
1033
  build_join_expression(exps, other, join_type)
1013
1034
  ev = Evaluator.new
1014
- result = Table.new
1035
+ result = empty_dup
1015
1036
  other_rows = other.rows
1016
1037
  other_row_matches = Array.new(other_rows.size, false)
1017
1038
  rows.each do |self_row|
@@ -1259,7 +1280,7 @@ module FatTable
1259
1280
  groups = sorted_tab.rows.group_by do |r|
1260
1281
  group_cols.map { |k| r[k] }
1261
1282
  end
1262
- result = Table.new
1283
+ result = empty_dup
1263
1284
  groups.each_pair do |_vals, grp_rows|
1264
1285
  result << row_from_group(grp_rows, group_cols, agg_cols)
1265
1286
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module FatTable
4
4
  # The current version of FatTable
5
- VERSION = '0.4.0'
5
+ VERSION = '0.4.2'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel E. Doherty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-02 00:00:00.000000000 Z
11
+ date: 2022-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler