-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improves handling of caching * improved documentation
- Loading branch information
Showing
8 changed files
with
238 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
module RubyUnits | ||
class Unit < Numeric | ||
@@cached_units = {} | ||
# Performance optimizations to avoid creating units unnecessarily | ||
class Cache | ||
attr_accessor :data | ||
|
||
class Cache | ||
def self.get(key = nil) | ||
key.nil? ? @@cached_units : @@cached_units[key] | ||
end | ||
def initialize | ||
clear | ||
end | ||
|
||
# @param key [String, #to_unit] | ||
# @return [RubyUnits::Unit, nil] | ||
def get(key) | ||
key = key&.to_unit&.units unless key.is_a?(String) | ||
data[key] | ||
end | ||
|
||
def self.set(key, value) | ||
@@cached_units[key] = value | ||
end | ||
# @param key [String, #to_unit] | ||
# @return [void] | ||
def set(key, value) | ||
key = key.to_unit.units unless key.is_a?(String) | ||
data[key] = value | ||
end | ||
|
||
# @return [Array<String>] | ||
def keys | ||
data.keys | ||
end | ||
|
||
def self.clear | ||
@@cached_units = {} | ||
@@base_unit_cache = {} | ||
Unit.new(1) | ||
end | ||
# Reset the cache | ||
def clear | ||
@data = {} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.