namo 0.30.1 → 0.31.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.
- checksums.yaml +4 -4
- data/CHANGELOG +13 -0
- data/README.md +96 -66
- data/lib/Namo/Collection.rb +4 -0
- data/lib/Namo/Formulae.rb +4 -0
- data/lib/Namo/Row.rb +8 -0
- data/lib/Namo/VERSION.rb +1 -1
- data/lib/namo.rb +24 -0
- data/test/Namo/Collection_test.rb +13 -0
- data/test/Namo/Formulae_test.rb +15 -0
- data/test/Namo/Row_test.rb +19 -0
- data/test/namo_test.rb +45 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1241332321c6f9d7643932d1b53949bfb12eb8596b1e388c206a9fad705efaf7
|
|
4
|
+
data.tar.gz: 00c558afd1fe979599820a39539f3216b496557a69f94229a59fc45c48cfd062
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 74b61473e59179fcd559d52eb98435ab9127499c17403174c1505f5076d1c6d2dd1f829ea8a69403da948709d047d42603bb1fd6bc32205bfb1bb3b0459d47c8
|
|
7
|
+
data.tar.gz: c9690afd20a5ab0de834d9dcca3b2a310e4da3e4811b0a29545e60290f1cb62f7571e8c177bacd405b380e8bfe6f78358a075c9c2eb5a4519654843c90ddaf5a
|
data/CHANGELOG
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 20260822
|
|
4
|
+
|
|
5
|
+
0.31.0: + Namo#inspect, Namo::Collection#inspect, Namo::Row#inspect, and Namo::Formulae#inspect.
|
|
6
|
+
|
|
7
|
+
1. + Namo#inspect: the class, the name where there is one, the stored rows, and the derived dimension names — the form the README has shown since 0.0.0 and the code has never produced. Rows beyond Namo::INSPECTED_ROWS are elided with a count of what is left, so the output no longer grows with the data.
|
|
8
|
+
2. + Namo::Row#inspect: the row and its formula names. It had been rendering @namo, so asking a 1,000-row Namo for one row emitted more output than inspecting the whole Namo.
|
|
9
|
+
3. + Namo::Collection#inspect: the member names and the row count, the members being the substance and the data view derived from them.
|
|
10
|
+
4. + Namo::Formulae#inspect: the formula names, never the callables.
|
|
11
|
+
5. + README.md: an Inspection output section, covering the rendered form, the naming of derived dimensions, and the row cap.
|
|
12
|
+
6. ~ README.md: the inspect-output blocks requoted from single to double quotes, as Hash#inspect emits, and the two projections that carry formulae given their derived: suffix. The blocks had been hand-written before there was an inspect to copy from.
|
|
13
|
+
7. ~ Namo::VERSION: /0.30.1/0.31.0/
|
|
14
|
+
|
|
15
|
+
|
|
3
16
|
## 20260819
|
|
4
17
|
|
|
5
18
|
0.30.1: ~ namo.gemspec: spec.files reordered, the globs first.
|
data/README.md
CHANGED
|
@@ -66,42 +66,42 @@ Select by named dimension using keyword arguments:
|
|
|
66
66
|
# Single value
|
|
67
67
|
sales[product: 'Widget']
|
|
68
68
|
# => #<Namo [
|
|
69
|
-
# {product:
|
|
70
|
-
# {product:
|
|
69
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
70
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150}
|
|
71
71
|
# ]>
|
|
72
72
|
|
|
73
73
|
# Multiple dimensions
|
|
74
74
|
sales[product: 'Widget', quarter: 'Q1']
|
|
75
75
|
# => #<Namo [
|
|
76
|
-
# {product:
|
|
76
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100}
|
|
77
77
|
# ]>
|
|
78
78
|
|
|
79
79
|
# Range
|
|
80
80
|
sales[price: 10.0..20.0]
|
|
81
81
|
# => #<Namo [
|
|
82
|
-
# {product:
|
|
83
|
-
# {product:
|
|
82
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
83
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150}
|
|
84
84
|
# ]>
|
|
85
85
|
|
|
86
86
|
# Array of values
|
|
87
87
|
sales[quarter: ['Q1']]
|
|
88
88
|
# => #<Namo [
|
|
89
|
-
# {product:
|
|
90
|
-
# {product:
|
|
89
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
90
|
+
# {product: "Gadget", quarter: "Q1", price: 25.0, quantity: 40}
|
|
91
91
|
# ]>
|
|
92
92
|
|
|
93
93
|
# Proc predicate
|
|
94
94
|
sales[price: ->(v){v < 20.0}]
|
|
95
95
|
# => #<Namo [
|
|
96
|
-
# {product:
|
|
97
|
-
# {product:
|
|
96
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
97
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150}
|
|
98
98
|
# ]>
|
|
99
99
|
|
|
100
100
|
# Regex predicate
|
|
101
101
|
sales[product: /^W/]
|
|
102
102
|
# => #<Namo [
|
|
103
|
-
# {product:
|
|
104
|
-
# {product:
|
|
103
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
104
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150}
|
|
105
105
|
# ]>
|
|
106
106
|
```
|
|
107
107
|
|
|
@@ -110,7 +110,7 @@ Procs receive the dimension value and select the row when they return truthy. Th
|
|
|
110
110
|
```ruby
|
|
111
111
|
sales[price: ->(v){v < 20.0}, quantity: ->(v){v > 100}]
|
|
112
112
|
# => #<Namo [
|
|
113
|
-
# {product:
|
|
113
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150}
|
|
114
114
|
# ]>
|
|
115
115
|
```
|
|
116
116
|
|
|
@@ -131,10 +131,10 @@ Project to specific dimensions:
|
|
|
131
131
|
```ruby
|
|
132
132
|
sales[:product, :price]
|
|
133
133
|
# => #<Namo [
|
|
134
|
-
# {product:
|
|
135
|
-
# {product:
|
|
136
|
-
# {product:
|
|
137
|
-
# {product:
|
|
134
|
+
# {product: "Widget", price: 10.0},
|
|
135
|
+
# {product: "Widget", price: 10.0},
|
|
136
|
+
# {product: "Gadget", price: 25.0},
|
|
137
|
+
# {product: "Gadget", price: 25.0}
|
|
138
138
|
# ]>
|
|
139
139
|
```
|
|
140
140
|
|
|
@@ -143,8 +143,8 @@ Selection and projection can be chained:
|
|
|
143
143
|
```ruby
|
|
144
144
|
sales[product: 'Widget'][:quarter, :price]
|
|
145
145
|
# => #<Namo [
|
|
146
|
-
# {quarter:
|
|
147
|
-
# {quarter:
|
|
146
|
+
# {quarter: "Q1", price: 10.0},
|
|
147
|
+
# {quarter: "Q2", price: 10.0}
|
|
148
148
|
# ]>
|
|
149
149
|
```
|
|
150
150
|
|
|
@@ -153,8 +153,8 @@ Or combined in a single call (names before selectors):
|
|
|
153
153
|
```ruby
|
|
154
154
|
sales[:quarter, :price, product: 'Widget']
|
|
155
155
|
# => #<Namo [
|
|
156
|
-
# {quarter:
|
|
157
|
-
# {quarter:
|
|
156
|
+
# {quarter: "Q1", price: 10.0},
|
|
157
|
+
# {quarter: "Q2", price: 10.0}
|
|
158
158
|
# ]>
|
|
159
159
|
```
|
|
160
160
|
|
|
@@ -165,10 +165,10 @@ Contraction is the complement of projection. Projection says "keep these dimensi
|
|
|
165
165
|
```ruby
|
|
166
166
|
sales[-:price, -:quantity]
|
|
167
167
|
# => #<Namo [
|
|
168
|
-
# {product:
|
|
169
|
-
# {product:
|
|
170
|
-
# {product:
|
|
171
|
-
# {product:
|
|
168
|
+
# {product: "Widget", quarter: "Q1"},
|
|
169
|
+
# {product: "Widget", quarter: "Q2"},
|
|
170
|
+
# {product: "Gadget", quarter: "Q1"},
|
|
171
|
+
# {product: "Gadget", quarter: "Q2"}
|
|
172
172
|
# ]>
|
|
173
173
|
```
|
|
174
174
|
|
|
@@ -183,8 +183,8 @@ Selection and contraction can be chained:
|
|
|
183
183
|
```ruby
|
|
184
184
|
sales[product: 'Widget'][-:price, -:quantity]
|
|
185
185
|
# => #<Namo [
|
|
186
|
-
# {product:
|
|
187
|
-
# {product:
|
|
186
|
+
# {product: "Widget", quarter: "Q1"},
|
|
187
|
+
# {product: "Widget", quarter: "Q2"}
|
|
188
188
|
# ]>
|
|
189
189
|
```
|
|
190
190
|
|
|
@@ -193,8 +193,8 @@ Or combined in a single call (names before selectors):
|
|
|
193
193
|
```ruby
|
|
194
194
|
sales[-:price, -:quantity, product: 'Widget']
|
|
195
195
|
# => #<Namo [
|
|
196
|
-
# {product:
|
|
197
|
-
# {product:
|
|
196
|
+
# {product: "Widget", quarter: "Q1"},
|
|
197
|
+
# {product: "Widget", quarter: "Q2"}
|
|
198
198
|
# ]>
|
|
199
199
|
```
|
|
200
200
|
|
|
@@ -219,10 +219,10 @@ q2_sales = Namo.new([
|
|
|
219
219
|
|
|
220
220
|
all_sales = q1_sales + q2_sales
|
|
221
221
|
# => #<Namo [
|
|
222
|
-
# {product:
|
|
223
|
-
# {product:
|
|
224
|
-
# {product:
|
|
225
|
-
# {product:
|
|
222
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
223
|
+
# {product: "Gadget", quarter: "Q1", price: 25.0, quantity: 40},
|
|
224
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150},
|
|
225
|
+
# {product: "Gadget", quarter: "Q2", price: 25.0, quantity: 60}
|
|
226
226
|
# ]>
|
|
227
227
|
```
|
|
228
228
|
|
|
@@ -247,8 +247,8 @@ discontinued = Namo.new([
|
|
|
247
247
|
|
|
248
248
|
sales - discontinued
|
|
249
249
|
# => #<Namo [
|
|
250
|
-
# {product:
|
|
251
|
-
# {product:
|
|
250
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
251
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150}
|
|
252
252
|
# ]>
|
|
253
253
|
```
|
|
254
254
|
|
|
@@ -273,8 +273,8 @@ confirmed = Namo.new([
|
|
|
273
273
|
|
|
274
274
|
sales & confirmed
|
|
275
275
|
# => #<Namo [
|
|
276
|
-
# {product:
|
|
277
|
-
# {product:
|
|
276
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
277
|
+
# {product: "Gadget", quarter: "Q2", price: 25.0, quantity: 60}
|
|
278
278
|
# ]>
|
|
279
279
|
```
|
|
280
280
|
|
|
@@ -297,9 +297,9 @@ all_sales = Namo.new([
|
|
|
297
297
|
|
|
298
298
|
q1_sales | all_sales
|
|
299
299
|
# => #<Namo [
|
|
300
|
-
# {product:
|
|
301
|
-
# {product:
|
|
302
|
-
# {product:
|
|
300
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
301
|
+
# {product: "Gadget", quarter: "Q1", price: 25.0, quantity: 40},
|
|
302
|
+
# {product: "Thingo", quarter: "Q3", price: 5.0, quantity: 10}
|
|
303
303
|
# ]>
|
|
304
304
|
```
|
|
305
305
|
|
|
@@ -322,8 +322,8 @@ set_b = Namo.new([
|
|
|
322
322
|
|
|
323
323
|
set_a ^ set_b
|
|
324
324
|
# => #<Namo [
|
|
325
|
-
# {product:
|
|
326
|
-
# {product:
|
|
325
|
+
# {product: "Gadget", quarter: "Q1", price: 25.0, quantity: 40},
|
|
326
|
+
# {product: "Thingo", quarter: "Q3", price: 5.0, quantity: 10}
|
|
327
327
|
# ]>
|
|
328
328
|
```
|
|
329
329
|
|
|
@@ -346,8 +346,8 @@ fundamentals = Namo.new([
|
|
|
346
346
|
|
|
347
347
|
ohlcv * fundamentals
|
|
348
348
|
# => #<Namo [
|
|
349
|
-
# {symbol:
|
|
350
|
-
# {symbol:
|
|
349
|
+
# {symbol: "BHP", date: "2025-01-01", close: 42.5, pe: 14.5},
|
|
350
|
+
# {symbol: "RIO", date: "2025-01-01", close: 118.3, pe: 9.2}
|
|
351
351
|
# ]>
|
|
352
352
|
```
|
|
353
353
|
|
|
@@ -376,8 +376,8 @@ prices.*(quarterly) do |row, candidates|
|
|
|
376
376
|
candidates[quarter_end: ->(qe){qe <= row[:date]}].sort_by{|f| f[:quarter_end]}.last(1)
|
|
377
377
|
end
|
|
378
378
|
# => #<Namo [
|
|
379
|
-
# {symbol:
|
|
380
|
-
# {symbol:
|
|
379
|
+
# {symbol: "BHP", date: "2025-02-15", close: 42.5, quarter_end: "2024-12-31", eps: 1.0},
|
|
380
|
+
# {symbol: "BHP", date: "2025-05-20", close: 44.0, quarter_end: "2025-03-31", eps: 1.2}
|
|
381
381
|
# ]>
|
|
382
382
|
```
|
|
383
383
|
|
|
@@ -395,10 +395,10 @@ quarters = Namo.new([{quarter: 'Q1'}, {quarter: 'Q2'}])
|
|
|
395
395
|
|
|
396
396
|
products ** quarters
|
|
397
397
|
# => #<Namo [
|
|
398
|
-
# {product:
|
|
399
|
-
# {product:
|
|
400
|
-
# {product:
|
|
401
|
-
# {product:
|
|
398
|
+
# {product: "Widget", quarter: "Q1"},
|
|
399
|
+
# {product: "Widget", quarter: "Q2"},
|
|
400
|
+
# {product: "Gadget", quarter: "Q1"},
|
|
401
|
+
# {product: "Gadget", quarter: "Q2"}
|
|
402
402
|
# ]>
|
|
403
403
|
```
|
|
404
404
|
|
|
@@ -429,9 +429,9 @@ orders.**(tiers) do |row, candidates|
|
|
|
429
429
|
candidates[max_weight: ->(w){w >= row[:weight]}]
|
|
430
430
|
end
|
|
431
431
|
# => #<Namo [
|
|
432
|
-
# {order:
|
|
433
|
-
# {order:
|
|
434
|
-
# {order:
|
|
432
|
+
# {order: "A", weight: 5, tier: "light", max_weight: 10},
|
|
433
|
+
# {order: "A", weight: 5, tier: "heavy", max_weight: 20},
|
|
434
|
+
# {order: "B", weight: 15, tier: "heavy", max_weight: 20}
|
|
435
435
|
# ]>
|
|
436
436
|
```
|
|
437
437
|
|
|
@@ -454,8 +454,8 @@ fundamentals = Namo.new([
|
|
|
454
454
|
|
|
455
455
|
combined / fundamentals
|
|
456
456
|
# => #<Namo [
|
|
457
|
-
# {date:
|
|
458
|
-
# {date:
|
|
457
|
+
# {date: "2025-01-01", close: 42.5},
|
|
458
|
+
# {date: "2025-01-01", close: 118.3}
|
|
459
459
|
# ]>
|
|
460
460
|
```
|
|
461
461
|
|
|
@@ -616,10 +616,10 @@ sales[:revenue] = proc{|row| row[:price] * row[:quantity]}
|
|
|
616
616
|
|
|
617
617
|
sales[:product, :quarter, :revenue]
|
|
618
618
|
# => #<Namo [
|
|
619
|
-
# {product:
|
|
620
|
-
# {product:
|
|
621
|
-
# {product:
|
|
622
|
-
# {product:
|
|
619
|
+
# {product: "Widget", quarter: "Q1", revenue: 1000.0},
|
|
620
|
+
# {product: "Widget", quarter: "Q2", revenue: 1500.0},
|
|
621
|
+
# {product: "Gadget", quarter: "Q1", revenue: 1000.0},
|
|
622
|
+
# {product: "Gadget", quarter: "Q2", revenue: 1500.0}
|
|
623
623
|
# ]>
|
|
624
624
|
```
|
|
625
625
|
|
|
@@ -633,11 +633,11 @@ sales[:profit] = proc{|row| row[:revenue] - row[:cost]}
|
|
|
633
633
|
|
|
634
634
|
sales[:product, :quarter, :profit]
|
|
635
635
|
# => #<Namo [
|
|
636
|
-
# {product:
|
|
637
|
-
# {product:
|
|
638
|
-
# {product:
|
|
639
|
-
# {product:
|
|
640
|
-
# ]>
|
|
636
|
+
# {product: "Widget", quarter: "Q1", profit: 600.0},
|
|
637
|
+
# {product: "Widget", quarter: "Q2", profit: 900.0},
|
|
638
|
+
# {product: "Gadget", quarter: "Q1", profit: 840.0},
|
|
639
|
+
# {product: "Gadget", quarter: "Q2", profit: 1260.0}
|
|
640
|
+
# ] derived: [:revenue, :cost]>
|
|
641
641
|
```
|
|
642
642
|
|
|
643
643
|
Formulae work with selection and projection:
|
|
@@ -645,9 +645,9 @@ Formulae work with selection and projection:
|
|
|
645
645
|
```ruby
|
|
646
646
|
sales[product: 'Widget'][:revenue, :quarter]
|
|
647
647
|
# => #<Namo [
|
|
648
|
-
# {revenue: 1000.0, quarter:
|
|
649
|
-
# {revenue: 1500.0, quarter:
|
|
650
|
-
# ]>
|
|
648
|
+
# {revenue: 1000.0, quarter: "Q1"},
|
|
649
|
+
# {revenue: 1500.0, quarter: "Q2"}
|
|
650
|
+
# ] derived: [:cost, :profit]>
|
|
651
651
|
```
|
|
652
652
|
|
|
653
653
|
Formulae carry through selection — a filtered Namo instance remembers its formulae.
|
|
@@ -970,6 +970,36 @@ sales[:product, :quarter, :revenue].to_h
|
|
|
970
970
|
# }
|
|
971
971
|
```
|
|
972
972
|
|
|
973
|
+
### Inspection output
|
|
974
|
+
|
|
975
|
+
`inspect` renders the class, the stored rows, and the names of any derived dimensions:
|
|
976
|
+
|
|
977
|
+
```ruby
|
|
978
|
+
sales.inspect
|
|
979
|
+
# => #<Namo [
|
|
980
|
+
# {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100},
|
|
981
|
+
# {product: "Widget", quarter: "Q2", price: 10.0, quantity: 150},
|
|
982
|
+
# {product: "Gadget", quarter: "Q1", price: 25.0, quantity: 40},
|
|
983
|
+
# {product: "Gadget", quarter: "Q2", price: 25.0, quantity: 60}
|
|
984
|
+
# ] derived: [:revenue]>
|
|
985
|
+
```
|
|
986
|
+
|
|
987
|
+
A name, where the Namo carries one, follows the class: `#<Namo :sales [`. A subclass reports its own name in place of `Namo`.
|
|
988
|
+
|
|
989
|
+
Derived dimensions are *named*, not evaluated. A console calls `inspect` on every result, so evaluating there would cost a pass over the data on each one, would raise whatever a formula raises, and could not render a parameterised formula at all. Ask through `values`, `coordinates`, or a projection when you want the values.
|
|
990
|
+
|
|
991
|
+
Rows beyond `Namo::INSPECTED_ROWS` — ten — are elided with a count of the remainder, so the output stays bounded however large the data: a thousand-row Namo renders its first ten rows and then `... 990 more rows`.
|
|
992
|
+
|
|
993
|
+
A `Row` renders itself and its formula names, never the Namo it came from. A `Collection` renders its member names and its row count, the members being its substance and the data view derived from them.
|
|
994
|
+
|
|
995
|
+
```ruby
|
|
996
|
+
sales.first
|
|
997
|
+
# => #<Namo::Row {product: "Widget", quarter: "Q1", price: 10.0, quantity: 100} derived: [:revenue]>
|
|
998
|
+
|
|
999
|
+
sales.group_by(:quarter)
|
|
1000
|
+
# => #<Namo::Collection members: ["Q1", "Q2"], 4 rows>
|
|
1001
|
+
```
|
|
1002
|
+
|
|
973
1003
|
### Named Namos
|
|
974
1004
|
|
|
975
1005
|
A Namo can carry a name, passed by the `name:` keyword and read or set through the `name` accessor:
|
data/lib/Namo/Collection.rb
CHANGED
|
@@ -53,6 +53,10 @@ class Namo
|
|
|
53
53
|
self
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
def inspect
|
|
57
|
+
"#<#{self.class}#{inspected_name} members: #{@members.map(&:name).inspect}, #{@data.length} rows#{inspected_derived}>"
|
|
58
|
+
end
|
|
59
|
+
|
|
56
60
|
private
|
|
57
61
|
|
|
58
62
|
def initialize(positional_data = nil, data: [], formulae: {}, name: nil)
|
data/lib/Namo/Formulae.rb
CHANGED
data/lib/Namo/Row.rb
CHANGED
|
@@ -43,8 +43,16 @@ class Namo
|
|
|
43
43
|
@row
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
+
def inspect
|
|
47
|
+
"#<#{self.class} #{@row.inspect}#{inspected_derived}>"
|
|
48
|
+
end
|
|
49
|
+
|
|
46
50
|
private
|
|
47
51
|
|
|
52
|
+
def inspected_derived
|
|
53
|
+
@formulae.keys.empty? ? '' : " derived: #{@formulae.keys.inspect}"
|
|
54
|
+
end
|
|
55
|
+
|
|
48
56
|
def initialize(row, formulae, namo = nil)
|
|
49
57
|
@row = row
|
|
50
58
|
@formulae = formulae
|
data/lib/Namo/VERSION.rb
CHANGED
data/lib/namo.rb
CHANGED
|
@@ -17,6 +17,8 @@ class Namo
|
|
|
17
17
|
attr_accessor :formulae
|
|
18
18
|
attr_accessor :name
|
|
19
19
|
|
|
20
|
+
INSPECTED_ROWS = 10
|
|
21
|
+
|
|
20
22
|
def dimensions
|
|
21
23
|
data_dimensions + derived_dimensions
|
|
22
24
|
end
|
|
@@ -247,6 +249,10 @@ class Namo
|
|
|
247
249
|
end
|
|
248
250
|
end
|
|
249
251
|
|
|
252
|
+
def inspect
|
|
253
|
+
"#<#{self.class}#{inspected_name}#{inspected_rows}#{inspected_derived}>"
|
|
254
|
+
end
|
|
255
|
+
|
|
250
256
|
protected
|
|
251
257
|
|
|
252
258
|
def row_multiset
|
|
@@ -265,6 +271,24 @@ class Namo
|
|
|
265
271
|
|
|
266
272
|
private
|
|
267
273
|
|
|
274
|
+
def inspected_name
|
|
275
|
+
@name.nil? ? '' : " #{@name.inspect}"
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# The rows as they are stored, never the derived values: inspect is called
|
|
279
|
+
# for every result in a console, and evaluating a formula there would cost a
|
|
280
|
+
# pass over the data per access and raise whatever the formula raises.
|
|
281
|
+
def inspected_rows
|
|
282
|
+
return ' []' if @data.empty?
|
|
283
|
+
shown = @data.first(INSPECTED_ROWS).map{|row| " #{row.inspect}"}.join(",\n")
|
|
284
|
+
shown += "\n ... #{@data.length - INSPECTED_ROWS} more rows" if @data.length > INSPECTED_ROWS
|
|
285
|
+
" [\n#{shown}\n]"
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def inspected_derived
|
|
289
|
+
derived_dimensions.empty? ? '' : " derived: #{derived_dimensions.inspect}"
|
|
290
|
+
end
|
|
291
|
+
|
|
268
292
|
def initialize(positional_data = nil, data: [], formulae: {}, name: nil)
|
|
269
293
|
@data = positional_data || data
|
|
270
294
|
@formulae = formulae.is_a?(Formulae) ? formulae : Formulae.new(formulae)
|
|
@@ -346,4 +346,17 @@ describe Namo::Collection do
|
|
|
346
346
|
_(car.detail.values(:assembly)).must_equal [:powertrain, :powertrain, :chassis, :body]
|
|
347
347
|
end
|
|
348
348
|
end
|
|
349
|
+
|
|
350
|
+
describe "#inspect" do
|
|
351
|
+
it "renders the member names and the row count" do
|
|
352
|
+
collection = Namo::Collection.new
|
|
353
|
+
collection << Namo.new([{a: 1}], name: :first) << Namo.new([{a: 2}], name: :second)
|
|
354
|
+
_(collection.inspect).must_equal "#<Namo::Collection members: [:first, :second], 2 rows>"
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
it "renders an empty Collection" do
|
|
358
|
+
_(Namo::Collection.new.inspect).must_equal "#<Namo::Collection members: [], 0 rows>"
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
|
|
349
362
|
end
|
data/test/Namo/Formulae_test.rb
CHANGED
|
@@ -440,4 +440,19 @@ describe Namo::Formulae do
|
|
|
440
440
|
end
|
|
441
441
|
end
|
|
442
442
|
end
|
|
443
|
+
|
|
444
|
+
describe "#inspect" do
|
|
445
|
+
it "renders the formula names" do
|
|
446
|
+
formulae = Namo::Formulae.new
|
|
447
|
+
formulae[:revenue] = proc{|row| row[:price]}
|
|
448
|
+
_(formulae.inspect).must_equal "#<Namo::Formulae [:revenue]>"
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
it "does not render the callables" do
|
|
452
|
+
formulae = Namo::Formulae.new
|
|
453
|
+
formulae[:revenue] = proc{|row| row[:price]}
|
|
454
|
+
_(formulae.inspect).wont_match(/Proc/)
|
|
455
|
+
end
|
|
456
|
+
end
|
|
457
|
+
|
|
443
458
|
end
|
data/test/Namo/Row_test.rb
CHANGED
|
@@ -459,4 +459,23 @@ describe Namo::Row do
|
|
|
459
459
|
_([a, b, duplicate_of_a].uniq.length).must_equal 2
|
|
460
460
|
end
|
|
461
461
|
end
|
|
462
|
+
|
|
463
|
+
describe "#inspect" do
|
|
464
|
+
it "renders the row data" do
|
|
465
|
+
_(Namo::Row.new({a: 1}, Namo::Formulae.new).inspect).must_equal "#<Namo::Row {a: 1}>"
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
it "names derived dimensions without evaluating them" do
|
|
469
|
+
formulae = Namo::Formulae.new
|
|
470
|
+
formulae[:b] = proc{|row| raise 'never called'}
|
|
471
|
+
_(Namo::Row.new({a: 1}, formulae).inspect).must_equal "#<Namo::Row {a: 1} derived: [:b]>"
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
it "does not render the Namo the row came from" do
|
|
475
|
+
namo = Namo.new((1..1000).map{|i| {a: i}})
|
|
476
|
+
_(namo.first.inspect).wont_match(/\{a: 2\}/)
|
|
477
|
+
_(namo.first.inspect.length).must_be :<, 100
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
|
|
462
481
|
end
|
data/test/namo_test.rb
CHANGED
|
@@ -3221,4 +3221,49 @@ describe Namo do
|
|
|
3221
3221
|
end
|
|
3222
3222
|
end
|
|
3223
3223
|
end
|
|
3224
|
+
|
|
3225
|
+
describe "#inspect" do
|
|
3226
|
+
it "renders the class and the rows" do
|
|
3227
|
+
_(Namo.new([{a: 1}]).inspect).must_equal "#<Namo [\n {a: 1}\n]>"
|
|
3228
|
+
end
|
|
3229
|
+
|
|
3230
|
+
it "renders an empty Namo without a row block" do
|
|
3231
|
+
_(Namo.new([]).inspect).must_equal "#<Namo []>"
|
|
3232
|
+
end
|
|
3233
|
+
|
|
3234
|
+
it "names derived dimensions without evaluating them" do
|
|
3235
|
+
namo = Namo.new([{a: 1}])
|
|
3236
|
+
namo[:b] = proc{|row| raise 'never called'}
|
|
3237
|
+
_(namo.inspect).must_equal "#<Namo [\n {a: 1}\n] derived: [:b]>"
|
|
3238
|
+
end
|
|
3239
|
+
|
|
3240
|
+
it "includes the name when there is one" do
|
|
3241
|
+
_(Namo.new([{a: 1}], name: :sales).inspect).must_match(/\A#<Namo :sales \[/)
|
|
3242
|
+
end
|
|
3243
|
+
|
|
3244
|
+
it "omits the name when there is none" do
|
|
3245
|
+
_(Namo.new([{a: 1}]).inspect).must_match(/\A#<Namo \[/)
|
|
3246
|
+
end
|
|
3247
|
+
|
|
3248
|
+
it "truncates beyond INSPECTED_ROWS and says how many are left" do
|
|
3249
|
+
namo = Namo.new((1..25).map{|i| {a: i}})
|
|
3250
|
+
_(namo.inspect.scan(/\{a: \d+\}/).length).must_equal Namo::INSPECTED_ROWS
|
|
3251
|
+
_(namo.inspect).must_match(/\.\.\. 15 more rows/)
|
|
3252
|
+
end
|
|
3253
|
+
|
|
3254
|
+
it "does not truncate at exactly INSPECTED_ROWS" do
|
|
3255
|
+
namo = Namo.new((1..Namo::INSPECTED_ROWS).map{|i| {a: i}})
|
|
3256
|
+
_(namo.inspect).wont_match(/more rows/)
|
|
3257
|
+
end
|
|
3258
|
+
|
|
3259
|
+
it "reports the subclass rather than Namo" do
|
|
3260
|
+
Object.const_set(:InspectedSubAssembly, Class.new(Namo)) unless defined?(InspectedSubAssembly)
|
|
3261
|
+
_(InspectedSubAssembly.new([{a: 1}]).inspect).must_match(/\A#<InspectedSubAssembly \[/)
|
|
3262
|
+
end
|
|
3263
|
+
|
|
3264
|
+
it "does not grow with the data beyond the truncation point" do
|
|
3265
|
+
_(Namo.new((1..10_000).map{|i| {a: i}}).inspect.length).must_be :<, 500
|
|
3266
|
+
end
|
|
3267
|
+
end
|
|
3268
|
+
|
|
3224
3269
|
end
|