Skip to content

Commit 2fe9e34

Browse files
authored
Refactor Unit class for improved code quality and maintainability (#382)
1 parent b5ce11c commit 2fe9e34

File tree

6 files changed

+406
-277
lines changed

6 files changed

+406
-277
lines changed

.reek.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Reek configuration
2+
#
3+
# InstanceVariableAssumption is disabled for Unit class because it uses a mix of
4+
# eager initialization (in initialize_instance_variables) and lazy initialization
5+
# (using defined? checks) for its instance variables. This is an intentional design
6+
# pattern where some variables are initialized immediately and others are computed
7+
# on first access for performance reasons.
8+
#
9+
# See: https://github.com/troessner/reek/blob/master/docs/Instance-Variable-Assumption.md
10+
#
11+
# RepeatedConditional warnings are excluded for Unit class where the same condition
12+
# needs to be checked in different methods for different purposes:
13+
#
14+
# - base?: Checked in to_base (early return), update_base_scalar (base unit calculation),
15+
# and unit_signature_vector (recursion avoidance). Each serves a distinct purpose.
16+
#
17+
# - other: Type checking in operator overloading methods using case statements.
18+
# This is a standard Ruby pattern for implementing polymorphic operators.
19+
#
20+
# - prefix_value: Checked in to_base (numerator/denominator processing) and parse
21+
# (string parsing). These are completely different contexts.
22+
#
23+
# - temperature?: Checked in multiple methods (to_base, temperature_scale, arithmetic
24+
# operators, power/root) where each serves a different purpose (conversion, formatting,
25+
# validation, etc.).
26+
27+
detectors:
28+
InstanceVariableAssumption:
29+
exclude:
30+
- RubyUnits::Unit
31+
32+
RepeatedConditional:
33+
exclude:
34+
- RubyUnits::Unit

lib/ruby_units/cache.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RubyUnits
44
# Performance optimizations to avoid creating units unnecessarily
55
class Cache
6-
attr_accessor :data
6+
attr_reader :data
77

88
def initialize
99
clear

0 commit comments

Comments
 (0)