reading 0.7.0 → 0.9.0

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/bin/reading +80 -10
  3. data/lib/reading/config.rb +96 -52
  4. data/lib/reading/errors.rb +4 -1
  5. data/lib/reading/filter.rb +95 -0
  6. data/lib/reading/item/time_length.rb +69 -30
  7. data/lib/reading/item/view.rb +116 -0
  8. data/lib/reading/item.rb +384 -0
  9. data/lib/reading/parsing/attributes/attribute.rb +1 -8
  10. data/lib/reading/parsing/attributes/experiences/dates_and_head_transformer.rb +11 -12
  11. data/lib/reading/parsing/attributes/experiences/history_transformer.rb +31 -22
  12. data/lib/reading/parsing/attributes/experiences/spans_validator.rb +19 -20
  13. data/lib/reading/parsing/attributes/experiences.rb +6 -6
  14. data/lib/reading/parsing/attributes/notes.rb +1 -1
  15. data/lib/reading/parsing/attributes/shared.rb +15 -8
  16. data/lib/reading/parsing/attributes/variants.rb +10 -7
  17. data/lib/reading/parsing/csv.rb +58 -44
  18. data/lib/reading/parsing/parser.rb +24 -25
  19. data/lib/reading/parsing/rows/blank.rb +23 -0
  20. data/lib/reading/parsing/rows/comment.rb +6 -7
  21. data/lib/reading/parsing/rows/compact_planned.rb +9 -9
  22. data/lib/reading/parsing/rows/compact_planned_columns/head.rb +2 -2
  23. data/lib/reading/parsing/rows/custom_config.rb +42 -0
  24. data/lib/reading/parsing/rows/regular.rb +15 -14
  25. data/lib/reading/parsing/rows/regular_columns/length.rb +8 -8
  26. data/lib/reading/parsing/rows/regular_columns/sources.rb +15 -9
  27. data/lib/reading/parsing/transformer.rb +15 -19
  28. data/lib/reading/stats/filter.rb +738 -0
  29. data/lib/reading/stats/grouping.rb +243 -0
  30. data/lib/reading/stats/operation.rb +313 -0
  31. data/lib/reading/stats/query.rb +37 -0
  32. data/lib/reading/stats/terminal_result_formatters.rb +91 -0
  33. data/lib/reading/util/exclude.rb +12 -0
  34. data/lib/reading/util/hash_to_data.rb +30 -0
  35. data/lib/reading/version.rb +1 -1
  36. data/lib/reading.rb +51 -5
  37. metadata +28 -7
  38. data/bin/readingfile +0 -31
  39. data/lib/reading/util/hash_to_struct.rb +0 -30
  40. data/lib/reading/util/string_remove.rb +0 -28
  41. data/lib/reading/util/string_truncate.rb +0 -22
@@ -0,0 +1,30 @@
1
+ module Reading
2
+ module Util
3
+ # Converts a Hash to a Data. Converts inner hashes (and inner arrays of hashes) as well.
4
+ module HashToData
5
+ refine Hash do
6
+ # @return [Data]
7
+ def to_data
8
+ MEMOIZED_DATAS[keys] ||= Data.define(*keys)
9
+ data_class = MEMOIZED_DATAS[keys]
10
+
11
+ data_values = transform_values { |v|
12
+ if v.is_a?(Hash)
13
+ v.to_data
14
+ elsif v.is_a?(Array) && v.all? { |el| el.is_a?(Hash) }
15
+ v.map(&:to_data).freeze
16
+ else
17
+ v.freeze
18
+ end
19
+ }.values
20
+
21
+ data_class.new(*data_values)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ MEMOIZED_DATAS = {}
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module Reading
2
- VERSION = "0.7.0"
2
+ VERSION = '0.9.0'
3
3
  end
data/lib/reading.rb CHANGED
@@ -1,8 +1,37 @@
1
- require_relative "reading/parsing/csv"
2
- require_relative "reading/item/time_length.rb"
1
+ Dir[File.join(__dir__, 'reading', 'util', '*.rb')].each { |file| require file }
2
+ require_relative 'reading/errors'
3
+ require_relative 'reading/config'
4
+ require_relative 'reading/parsing/csv'
5
+ require_relative 'reading/filter'
6
+ require_relative 'reading/stats/query'
7
+ require_relative 'reading/item/time_length.rb'
3
8
 
4
9
  # The gem's public API. See https://github.com/fpsvogel/reading#usage
5
-
10
+ #
11
+ # Architectural overview:
12
+ #
13
+ # (filtered (stats input*
14
+ # (CSV input) (Items) Items) and Items) (results)
15
+ # | Λ | Λ | Λ
16
+ # V | V | V |
17
+ # ::parse | ::filter | ::stats |
18
+ # | | | | | |
19
+ # | | | | | |
20
+ # | | | | | |
21
+ # Parsing::CSV ---------> Item Filter Stats::Query
22
+ # / \ / \ / | \
23
+ # / \ Item::View Item::TimeLength / | \
24
+ # / \ / | \
25
+ # Parsing::Parser Parsing::Transformer Stats::Filter | Stats::Operation
26
+ # | | Stats::Grouping
27
+ # parsing/rows/* parsing/attributes/*
28
+ # * Stats input is either from the
29
+ # command line (via the `reading`
30
+ # command) or provided via Ruby
31
+ # code that uses this gem.
32
+ # Results likewise go either to
33
+ # stdout or to the gem user.
34
+ #
6
35
  module Reading
7
36
  # Parses a CSV file or string. See Parsing::CSV#initialize and #parse for details.
8
37
  def self.parse(...)
@@ -10,9 +39,26 @@ module Reading
10
39
  csv.parse
11
40
  end
12
41
 
42
+ # Filters an array of Items. See Filter::by for details.
43
+ def self.filter(...)
44
+ Filter.by(...)
45
+ end
46
+
47
+ # Returns statistics on Items. See Stats::Query#initialize and #result for details.
48
+ def self.stats(...)
49
+ query = Stats::Query.new(...)
50
+ query.result
51
+ end
52
+
53
+ # @return [Hash]
54
+ def self.config
55
+ Config.hash
56
+ end
57
+
58
+ # A shortcut for getting a time from a string.
13
59
  # @param string [String] a time duration in "h:mm" format.
14
- # @return [Reading::Item::TimeLength]
60
+ # @return [Item::TimeLength]
15
61
  def self.time(string)
16
- Reading::Item::TimeLength.parse(string)
62
+ Item::TimeLength.parse(string)
17
63
  end
18
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reading
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felipe Vogel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-05 00:00:00.000000000 Z
11
+ date: 2023-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda-context
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: pretty-diffs
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -113,16 +127,17 @@ email:
113
127
  - fps.vogel@gmail.com
114
128
  executables:
115
129
  - reading
116
- - readingfile
117
130
  extensions: []
118
131
  extra_rdoc_files: []
119
132
  files:
120
133
  - bin/reading
121
- - bin/readingfile
122
134
  - lib/reading.rb
123
135
  - lib/reading/config.rb
124
136
  - lib/reading/errors.rb
137
+ - lib/reading/filter.rb
138
+ - lib/reading/item.rb
125
139
  - lib/reading/item/time_length.rb
140
+ - lib/reading/item/view.rb
126
141
  - lib/reading/parsing/attributes/attribute.rb
127
142
  - lib/reading/parsing/attributes/author.rb
128
143
  - lib/reading/parsing/attributes/experiences.rb
@@ -137,10 +152,12 @@ files:
137
152
  - lib/reading/parsing/attributes/variants.rb
138
153
  - lib/reading/parsing/csv.rb
139
154
  - lib/reading/parsing/parser.rb
155
+ - lib/reading/parsing/rows/blank.rb
140
156
  - lib/reading/parsing/rows/column.rb
141
157
  - lib/reading/parsing/rows/comment.rb
142
158
  - lib/reading/parsing/rows/compact_planned.rb
143
159
  - lib/reading/parsing/rows/compact_planned_columns/head.rb
160
+ - lib/reading/parsing/rows/custom_config.rb
144
161
  - lib/reading/parsing/rows/regular.rb
145
162
  - lib/reading/parsing/rows/regular_columns/end_dates.rb
146
163
  - lib/reading/parsing/rows/regular_columns/genres.rb
@@ -152,14 +169,18 @@ files:
152
169
  - lib/reading/parsing/rows/regular_columns/sources.rb
153
170
  - lib/reading/parsing/rows/regular_columns/start_dates.rb
154
171
  - lib/reading/parsing/transformer.rb
172
+ - lib/reading/stats/filter.rb
173
+ - lib/reading/stats/grouping.rb
174
+ - lib/reading/stats/operation.rb
175
+ - lib/reading/stats/query.rb
176
+ - lib/reading/stats/terminal_result_formatters.rb
155
177
  - lib/reading/util/blank.rb
178
+ - lib/reading/util/exclude.rb
156
179
  - lib/reading/util/hash_array_deep_fetch.rb
157
180
  - lib/reading/util/hash_compact_by_template.rb
158
181
  - lib/reading/util/hash_deep_merge.rb
159
- - lib/reading/util/hash_to_struct.rb
182
+ - lib/reading/util/hash_to_data.rb
160
183
  - lib/reading/util/numeric_to_i_if_whole.rb
161
- - lib/reading/util/string_remove.rb
162
- - lib/reading/util/string_truncate.rb
163
184
  - lib/reading/version.rb
164
185
  homepage: https://github.com/fpsvogel/reading
165
186
  licenses:
data/bin/readingfile DELETED
@@ -1,31 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # A script that provides a quick way to see the output of a CSV file.
4
- #
5
- # Usage:
6
- # Run on the command line:
7
- # reading "<file path>" "<optional comma-separated names of enabled columns>"
8
- #
9
- # Examples:
10
- # reading '/home/alex/reading.csv'
11
- # reading '/home/alex/reading.csv' 'head, sources'
12
-
13
-
14
- require_relative "../lib/reading"
15
- require "amazing_print"
16
- require "debug"
17
-
18
- path = ARGV[0]
19
- unless path
20
- raise ArgumentError, "CSV path argument required, such as '/home/alex/reading.csv'"
21
- end
22
-
23
- config = {}
24
- if ARGV[1]
25
- enabled_columns = ARGV[1].split(",").map(&:strip).map(&:to_sym)
26
- config = { enabled_columns: }
27
- end
28
-
29
- items = Reading.parse(path, config:)
30
-
31
- ap items
@@ -1,30 +0,0 @@
1
- module Reading
2
- module Util
3
- # Converts a Hash to a Struct. Converts inner hashes (and inner arrays of hashes) as well.
4
- module HashToStruct
5
- refine Hash do
6
- # @return [Struct]
7
- def to_struct
8
- MEMOIZED_STRUCTS[keys] ||= Struct.new(*keys)
9
- struct_class = MEMOIZED_STRUCTS[keys]
10
-
11
- struct_values = transform_values { |v|
12
- if v.is_a?(Hash)
13
- v.to_struct
14
- elsif v.is_a?(Array) && v.all? { |el| el.is_a?(Hash) }
15
- v.map(&:to_struct)
16
- else
17
- v
18
- end
19
- }.values
20
-
21
- struct_class.new(*struct_values)
22
- end
23
- end
24
-
25
- private
26
-
27
- MEMOIZED_STRUCTS = {}
28
- end
29
- end
30
- end
@@ -1,28 +0,0 @@
1
- module Reading
2
- module Util
3
- # Shortcuts for String#sub and String#gsub when replacing with an empty string.
4
- module StringRemove
5
- refine String do
6
- def remove(pattern)
7
- sub(pattern, EMPTY_STRING)
8
- end
9
-
10
- def remove!(pattern)
11
- sub!(pattern, EMPTY_STRING)
12
- end
13
-
14
- def remove_all(pattern)
15
- gsub(pattern, EMPTY_STRING)
16
- end
17
-
18
- def remove_all!(pattern)
19
- gsub!(pattern, EMPTY_STRING)
20
- end
21
- end
22
-
23
- private
24
-
25
- EMPTY_STRING = "".freeze
26
- end
27
- end
28
- end
@@ -1,22 +0,0 @@
1
- module Reading
2
- module Util
3
- # Shortens the String to a given length.
4
- module StringTruncate
5
- refine String do
6
- # @param length [Integer]
7
- # @return [String]
8
- def truncate(length)
9
- if length < self.length - ELLIPSIS.length
10
- "#{self[0...length]}#{ELLIPSIS}"
11
- else
12
- self
13
- end
14
- end
15
- end
16
-
17
- private
18
-
19
- ELLIPSIS = "...".freeze
20
- end
21
- end
22
- end