flex-cartesian 1.2.1 → 1.3.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.md +8 -0
- data/README.md +25 -2
- data/lib/flex-cartesian.rb +49 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20aebfbce656dcb5bbe2467cae65ad8bc510022f6a908b88f55f14101a92e1c3
|
4
|
+
data.tar.gz: 334d472b4cd56b2a159459c2a8f46d80c704780de7ff348fe711a63d7930c1dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f865d4ad80b7ff10d85ebbeaf1a0a2e0185fe9798cf45dd50c1ae4246ad1dd96cf3b01983c29606f27886312335c4aa7e540b45f38e25c37753df7ce5bfbe152
|
7
|
+
data.tar.gz: 9250f059eeb325d2d69732af18207561351cb8e86d3a6da878b1d66ab37e1d39f970af8385a463aa5ef89d0261f9cd7eda7eac39648c492c70e7bf841d181028
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.3.0 - 2025-08-19
|
4
|
+
### Added
|
5
|
+
- .func accepts flag order:
|
6
|
+
|
7
|
+
## 1.2.2 - 2025-07-28
|
8
|
+
### Added
|
9
|
+
- .dimensions functionality extended to more granular formatting
|
10
|
+
|
3
11
|
## 1.2.1 - 2025-07-28
|
4
12
|
### Fixed
|
5
13
|
- When adding function, check if its namesake exists
|
data/README.md
CHANGED
@@ -179,7 +179,16 @@ puts "\Add and then remove function 'test'"
|
|
179
179
|
s.func(:add, :test) { |v| v.dim3.to_i }
|
180
180
|
s.func(:del, :test)
|
181
181
|
|
182
|
+
puts "\nThis function will calculate first, always"
|
183
|
+
s.func(:add, :test_first, order: :first) { puts "HERE" }
|
182
184
|
|
185
|
+
puts "\nThis function will calculate last, always"
|
186
|
+
s.func(:add, :test_last, order: :last) { puts "HERE" }
|
187
|
+
|
188
|
+
puts "\nNew functions respect first and last ones"
|
189
|
+
s.func(:add, :test_more) { puts "HERE" }
|
190
|
+
|
191
|
+
puts "\nFirst and last functions are handy for pre- and post-processing for each combination"
|
183
192
|
|
184
193
|
# CONDITIONS ON CARTESIAN SPACE
|
185
194
|
|
@@ -211,6 +220,9 @@ puts "\nPrint Cartesian space as Markdown"
|
|
211
220
|
s.output(format: :markdown)
|
212
221
|
puts "\nPrint Cartesian space as CSV"
|
213
222
|
s.output(format: :csv)
|
223
|
+
puts "\nGranular output of Cartesian space dimensions: #{s.dimensions(values: false)}"
|
224
|
+
puts "\nGranular output of Cartesian vectors:"
|
225
|
+
s.cartesian { |v| puts s.dimensions(v, separator: ' ') }
|
214
226
|
|
215
227
|
|
216
228
|
|
@@ -374,7 +386,7 @@ s.cartesian { |v| puts "#{v.dim1} - #{v.dim2}" }
|
|
374
386
|
|
375
387
|
### Handling Functions
|
376
388
|
```ruby
|
377
|
-
func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", &block)
|
389
|
+
func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", order: nil, &block)
|
378
390
|
```
|
379
391
|
- `command`: symbol, one of the following
|
380
392
|
- `:add` to add function as a virtual dimension to Cartesian space
|
@@ -385,6 +397,7 @@ func(command = :print, name = nil, hide: false, progress: false, title: "calcula
|
|
385
397
|
- `hide`: flag that hides or shows the function in .output; it is useful to hide intermediate calculations
|
386
398
|
- `progress`: show progress bar during `:run`, useful for large Cartesian space
|
387
399
|
- `title`: title of the progress bar
|
400
|
+
- `order`: can be `:first` or `:last` to make the function calculate before or after all other functions
|
388
401
|
- `block`: a function that receives each vector and returns a computed value
|
389
402
|
|
390
403
|
Functions show up in `.output` like additional (virtual) dimensions.
|
@@ -435,7 +448,7 @@ Displays a progress bar using `ruby-progressbar`.
|
|
435
448
|
|
436
449
|
---
|
437
450
|
|
438
|
-
###
|
451
|
+
### Generate Output
|
439
452
|
```ruby
|
440
453
|
output(separator: " | ", colorize: false, align: true, format: :plain, limit: nil, file: nil)
|
441
454
|
```
|
@@ -455,6 +468,16 @@ Markdown example:
|
|
455
468
|
| 2 | "b" |
|
456
469
|
```
|
457
470
|
|
471
|
+
```ruby
|
472
|
+
dimensions(data = @dimensions, raw: false, separator: ', ', dimensions: true, values: true)
|
473
|
+
```
|
474
|
+
Generates formatted Cartesian dimensions or vectors of Cartesian space
|
475
|
+
- `data`: what to format, either Cartesian vector (usually `s.cartesian { |v| ... }) or entire Cartesian dimensions
|
476
|
+
- `raw`: if enabled, overrides any other formarring flags and returns the same as `.inspect`
|
477
|
+
- `separator`: how to separate individual components
|
478
|
+
- `dimensions`: whether or not show dimension names
|
479
|
+
- `values`: whether or not show value(s) associated with dimensions
|
480
|
+
|
458
481
|
---
|
459
482
|
|
460
483
|
### Import from JSON or YAML
|
data/lib/flex-cartesian.rb
CHANGED
@@ -25,20 +25,33 @@ module FlexOutput
|
|
25
25
|
end
|
26
26
|
|
27
27
|
class FlexCartesian
|
28
|
-
attr :dimensions
|
29
28
|
|
30
29
|
def initialize(dimensions = nil, path: nil, format: :json)
|
31
30
|
if dimensions && path
|
32
|
-
|
31
|
+
puts "Please specify either dimensions or path to dimensions"
|
32
|
+
exit
|
33
33
|
end
|
34
34
|
@dimensions = dimensions
|
35
35
|
@conditions = []
|
36
36
|
@derived = {}
|
37
|
+
@order = { first: nil, last: nil }
|
37
38
|
@function_results = {} # key: Struct instance.object_id => { fname => value }
|
38
39
|
@function_hidden = Set.new
|
39
40
|
import(path, format: format) if path
|
40
41
|
end
|
41
42
|
|
43
|
+
def dimensions(data = @dimensions, raw: false, separator: ', ', dimensions: true, values: true)
|
44
|
+
return data.inspect if raw # by default, with no data speciaifed, we assume dimensions of Cartesian
|
45
|
+
return nil if not dimensions and not values
|
46
|
+
|
47
|
+
if data.is_a?(Struct) or data.is_a?(Hash) # vector in Cartesian or entire Cartesian
|
48
|
+
data.each_pair.map { |k, v| (dimensions ? "#{k}" : "") + ((dimensions and values) ? "=" : "") + (values ? "#{v}" : "") }.join(separator)
|
49
|
+
else
|
50
|
+
puts "Incorrect type of dimensions: #{data.class}"
|
51
|
+
exit
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
42
55
|
def cond(command = :print, index: nil, &block)
|
43
56
|
case command
|
44
57
|
when :set
|
@@ -59,11 +72,11 @@ class FlexCartesian
|
|
59
72
|
end
|
60
73
|
end
|
61
74
|
|
62
|
-
def func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", &block)
|
75
|
+
def func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", order: nil, &block)
|
63
76
|
case command
|
64
77
|
when :add
|
65
78
|
raise ArgumentError, "Function name and block required for :add" unless name && block_given?
|
66
|
-
add_function(name, &block)
|
79
|
+
add_function(name, order: order, &block)
|
67
80
|
@function_hidden.delete(name.to_sym)
|
68
81
|
@function_hidden << name.to_sym if hide
|
69
82
|
|
@@ -77,10 +90,17 @@ def func(command = :print, name = nil, hide: false, progress: false, title: "cal
|
|
77
90
|
else
|
78
91
|
@derived.each do |fname, fblock|
|
79
92
|
source = fblock.source rescue '(source unavailable)'
|
80
|
-
|
81
93
|
body = source.sub(/^.*?\s(?=(\{|\bdo\b))/, '').strip
|
82
|
-
|
83
|
-
|
94
|
+
order = ""
|
95
|
+
if @order.value?(fname.to_sym)
|
96
|
+
case @order.key(fname.to_sym)
|
97
|
+
when :first
|
98
|
+
order = " [FIRST]"
|
99
|
+
when :last
|
100
|
+
order = " [LAST]"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
puts " #{fname.inspect.ljust(12)}| #{body}#{@function_hidden.include?(fname) ? ' [HIDDEN]' : ''}#{order}"
|
84
104
|
end
|
85
105
|
end
|
86
106
|
|
@@ -112,18 +132,38 @@ def func(command = :print, name = nil, hide: false, progress: false, title: "cal
|
|
112
132
|
end
|
113
133
|
end
|
114
134
|
|
115
|
-
def add_function(name, &block)
|
135
|
+
def add_function(name, order: nil , &block)
|
116
136
|
raise ArgumentError, "Block required" unless block_given?
|
117
137
|
if reserved_function_names.include?(name.to_sym)
|
118
138
|
raise ArgumentError, "Function name '#{name}' has been already added"
|
119
139
|
elsif reserved_struct_names.include?(name.to_sym)
|
120
140
|
raise ArgumentError, "Name '#{name}' has been reserved for internal method, you can't use it for a function"
|
121
141
|
end
|
122
|
-
|
142
|
+
if order == :last
|
143
|
+
@derived[name.to_sym] = block # add to the tail of the hash
|
144
|
+
@order[:last] = name.to_sym
|
145
|
+
elsif order == :first
|
146
|
+
@derived = { name.to_sym => block }.merge(@derived) # add to the head of the hash
|
147
|
+
@order[:first] = name.to_sym
|
148
|
+
elsif order == nil
|
149
|
+
if @order[:last] != nil
|
150
|
+
last_name = @order[:last]
|
151
|
+
last_body = @derived[last_name]
|
152
|
+
@derived.delete(@order[:last]) # remove the tail of the hash
|
153
|
+
@derived[name.to_sym] = block # add new function to the tail of the hash
|
154
|
+
@derived[last_name] = last_body # restore :last function in the tail of the hash
|
155
|
+
else
|
156
|
+
@derived[name.to_sym] = block
|
157
|
+
end
|
158
|
+
else
|
159
|
+
raise ArgumentError, "unknown function order '#{order}'"
|
160
|
+
end
|
123
161
|
end
|
124
162
|
|
125
163
|
def remove_function(name)
|
126
164
|
@derived.delete(name.to_sym)
|
165
|
+
@order[:last] = nil if @order[:last] == name.to_sym
|
166
|
+
@order[:first] = nil if @order[:first] == name.to_sym
|
127
167
|
end
|
128
168
|
|
129
169
|
def cartesian(dims = nil, lazy: false)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flex-cartesian
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Rassokhin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|