Skip to content

Commit

Permalink
move Scorpio.stringify_symbol_keys, Scorpio::FingerprintHash, Scorpio…
Browse files Browse the repository at this point in the history
…::Memoize to scorpio/util.rb
  • Loading branch information
notEthan committed May 3, 2018
1 parent d5f979c commit fa728ae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
38 changes: 4 additions & 34 deletions lib/scorpio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module Scorpio
def self.root
@root ||= Pathname.new(__FILE__).dirname.parent.expand_path
end
end

require "scorpio/util"

module Scorpio
# generally put in code paths that are not expected to be valid control flow paths.
# rather a NotImplementedCorrectlyError. but that's too long.
class Bug < NotImplementedError
Expand Down Expand Up @@ -74,38 +78,4 @@ class NetworkAuthenticationRequired511Error < ServerError; status(511); end
autoload :Typelike, 'scorpio/typelike_modules'
autoload :Hashlike, 'scorpio/typelike_modules'
autoload :Arraylike, 'scorpio/typelike_modules'

class << self
def stringify_symbol_keys(hash)
unless hash.respond_to?(:to_hash)
raise ArgumentError, "expected argument to be a Hash; got #{hash.class}: #{hash.pretty_inspect}"
end
Scorpio::Typelike.modified_copy(hash) do |hash_|
hash_.map { |k, v| {k.is_a?(Symbol) ? k.to_s : k => v} }.inject({}, &:update)
end
end
end

module FingerprintHash
def ==(other)
other.respond_to?(:fingerprint) && other.fingerprint == self.fingerprint
end

alias eql? ==

def hash
fingerprint.hash
end
end

module Memoize
def memoize(key, *args_)
@memos ||= {}
@memos[key] ||= Hash.new do |h, args|
h[args] = yield(*args)
end
@memos[key][args_]
end
end
extend Memoize
end
36 changes: 36 additions & 0 deletions lib/scorpio/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Scorpio
module Util
def stringify_symbol_keys(hash)
unless hash.respond_to?(:to_hash)
raise(ArgumentError, "expected argument to be a hash; got #{hash.class}: #{hash.pretty_inspect.chomp}")
end
Scorpio::Typelike.modified_copy(hash) do |hash_|
hash_.map { |k, v| {k.is_a?(Symbol) ? k.to_s : k => v} }.inject({}, &:update)
end
end
end
extend Util

module FingerprintHash
def ==(other)
other.respond_to?(:fingerprint) && other.fingerprint == self.fingerprint
end

alias_method :eql?, :==

def hash
fingerprint.hash
end
end

module Memoize
def memoize(key, *args_)
@memos ||= {}
@memos[key] ||= Hash.new do |h, args|
h[args] = yield(*args)
end
@memos[key][args_]
end
end
extend Memoize
end

0 comments on commit fa728ae

Please sign in to comment.