measured 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/measured/base.rb +1 -0
- data/lib/measured/cache/json.rb +1 -1
- data/lib/measured/conversion_table_builder.rb +19 -0
- data/lib/measured/cycle_detected.rb +11 -0
- data/lib/measured/parser.rb +1 -1
- data/lib/measured/version.rb +1 -1
- data/test/conversion_table_builder_test.rb +10 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82ffeb317f95fc3c354f43453eabc91b138a5e4f1b50eec651775425a27316e5
|
4
|
+
data.tar.gz: c664577f19d2ab99b95f7c533fb46323b60390c1b34da1bc3602b27979cddff6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ae4538c2b2e9b00403b6363d639a6f0959704c4702b65cca3d1cd33d714f2eaf14c4fa4aa446b6247e1f9b7cd7977bc80581115965b21abbd4972b10b2e3eea
|
7
|
+
data.tar.gz: ee33ab98e63f586bcf1ce299909e4cd6ecc0c6b434e3f2e48340d5dc9f2641a8c34bcc2dd695f9c75f162f413b6533456f6c0ab22d29b3c93c21c186e647b671
|
data/CHANGELOG.md
CHANGED
data/lib/measured/base.rb
CHANGED
data/lib/measured/cache/json.rb
CHANGED
@@ -24,6 +24,8 @@ class Measured::ConversionTableBuilder
|
|
24
24
|
private
|
25
25
|
|
26
26
|
def generate_table
|
27
|
+
validate_no_cycles
|
28
|
+
|
27
29
|
units.map(&:name).each_with_object({}) do |to_unit, table|
|
28
30
|
to_table = {to_unit => Rational(1, 1)}
|
29
31
|
|
@@ -37,6 +39,23 @@ class Measured::ConversionTableBuilder
|
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
42
|
+
def validate_no_cycles
|
43
|
+
graph = units.select { |unit| unit.conversion_unit.present? }.group_by { |unit| unit.name }
|
44
|
+
validate_acyclic_graph(graph, from: graph.keys[0])
|
45
|
+
end
|
46
|
+
|
47
|
+
# This uses a depth-first search algorithm: https://en.wikipedia.org/wiki/Depth-first_search
|
48
|
+
def validate_acyclic_graph(graph, from:, visited: [])
|
49
|
+
graph[from]&.each do |edge|
|
50
|
+
adjacent_node = edge.conversion_unit
|
51
|
+
if visited.include?(adjacent_node)
|
52
|
+
raise Measured::CycleDetected.new(edge)
|
53
|
+
else
|
54
|
+
validate_acyclic_graph(graph, from: adjacent_node, visited: visited + [adjacent_node])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
40
59
|
def find_conversion(to:, from:)
|
41
60
|
conversion = find_direct_conversion_cached(to: to, from: from) || find_tree_traversal_conversion(to: to, from: from)
|
42
61
|
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Measured
|
3
|
+
class CycleDetected < UnitError
|
4
|
+
attr_reader :unit
|
5
|
+
|
6
|
+
def initialize(unit)
|
7
|
+
super("The following conversion introduces cycles in the unit system: #{unit}. Remove the conversion or fix the cycle.")
|
8
|
+
@unit = unit
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/measured/parser.rb
CHANGED
data/lib/measured/version.rb
CHANGED
@@ -117,6 +117,16 @@ class Measured::ConversionTableBuilderTest < ActiveSupport::TestCase
|
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
|
+
test "#to_h raises exception when there are cycles" do
|
121
|
+
unit1 = Measured::Unit.new(:pallets, value: "1 liters")
|
122
|
+
unit2 = Measured::Unit.new(:liters, value: "0.1 cases")
|
123
|
+
unit3 = Measured::Unit.new(:cases, value: "0.1 pallets")
|
124
|
+
|
125
|
+
assert_raises(Measured::CycleDetected) do
|
126
|
+
Measured::ConversionTableBuilder.new([unit1, unit2, unit3]).to_h
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
120
130
|
test "#cached? returns true if there's a cache" do
|
121
131
|
builder = Measured::ConversionTableBuilder.new([Measured::Unit.new(:test)], cache: { class: AlwaysTrueCache })
|
122
132
|
assert_predicate builder, :cached?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: measured
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-05-06 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/measured/cache/json_writer.rb
|
127
127
|
- lib/measured/cache/null.rb
|
128
128
|
- lib/measured/conversion_table_builder.rb
|
129
|
+
- lib/measured/cycle_detected.rb
|
129
130
|
- lib/measured/measurable.rb
|
130
131
|
- lib/measured/missing_conversion_path.rb
|
131
132
|
- lib/measured/parser.rb
|