Class: Adamantium::Freezer

Inherits:
Object
  • Object
show all
Defined in:
lib/adamantium/freezer.rb

Overview

Abstract base class for freezers

TODO: Use dkubb/abstract_class?

Better pattern for singleton inheritance/shared code

Direct Known Subclasses

Deep, Flat

Defined Under Namespace

Classes: Deep, Flat, OptionError, UnknownFreezerError

Constant Summary collapse

Noop =
lambda { |object| object }.freeze

Class Method Summary collapse

Class Method Details

.call(object) ⇒ Object

Attempt to freeze an object

Examples:

using a value object

Adamantium.freeze_object(12345)  # => noop

using a normal object

Adamantium.freeze_object({})  # => duplicate & freeze object

Parameters:

  • object (Object)

    the object to freeze

Returns:

  • (Object)

    if supported, the frozen object, otherwise the object directly



29
30
31
32
33
34
35
36
# 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

.get(name) ⇒ #call

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return freezer for name

Parameters:

  • name (Symbol)

    a freezer name

Returns:



105
106
107
108
109
110
111
112
113
# File 'lib/adamantium/freezer.rb', line 105

def self.get(name)
  case name
  when :noop then Noop
  when :deep then Deep
  when :flat then Flat
  else
    raise UnknownFreezerError, "Freezer with name #{name.inspect} is unknown"
  end
end

.parse(options) ⇒ #call?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse freezer options

Parameters:

  • options (Hash)

    an options hash

Returns:

  • (#call)

    if freezer option was present

  • (nil)

    otherwise



128
129
130
131
132
133
134
# File 'lib/adamantium/freezer.rb', line 128

def self.parse(options)
  keys = options.keys - [:freezer]
  unless keys.empty?
    raise OptionError, "Unknown option key(s) for memoizer #{keys.inspect}"
  end
  get(options.fetch(:freezer)) if options.key?(:freezer)
end