class Adamantium::Freezer
Abstract base class for freezers
TODO: Use dkubb/abstract_class?
Better pattern for singleton inheritance/shared code
Constants
- Noop
Public Class Methods
get(name)
click to toggle source
Return freezer for name
@param [Symbol] name
a freezer name
@return [#call]
@api private
# File lib/adamantium/freezer.rb, line 111 def self.get(name) @freezers.fetch(name) do fail UnknownFreezerError, "Freezer with name #{name.inspect} is unknown" end end
parse(options)
click to toggle source
Parse freezer options
@param [Hash] options
an options hash
@return [#call]
if freezer option was present
@return [nil]
otherwise
@api private
# File lib/adamantium/freezer.rb, line 130 def self.parse(options) keys = options.keys - [:freezer] unless keys.empty? fail OptionError, "Unknown option key(s) for memoizer #{keys.inspect}" end get(options.fetch(:freezer)) if options.key?(:freezer) end
Private Class Methods
call(object)
click to toggle source
Attempt to freeze an object
@example using a value object
Adamantium.freeze_object(12345) # => noop
@example using a normal object
Adamantium.freeze_object({}) # => duplicate & freeze object
@param [Object] object
the object to freeze
@return [Object]
if supported, the frozen object, otherwise the object directly
@api public
# File lib/adamantium/freezer.rb, line 29 def self.call(object) case object when Numeric, TrueClass, FalseClass, NilClass, Symbol, Class, Module, UnboundMethod, Method object else freeze_value(object) end end
freeze_value(value)
click to toggle source
Returns a frozen value
@param [Object] value
a value to freeze
@return [Object]
if frozen, the value directly, otherwise a frozen copy of the value
@api private
# File lib/adamantium/freezer.rb, line 49 def self.freeze_value(value) value.frozen? ? value : freeze(value.dup) end