Module: Adamantium

Defined in:
lib/adamantium.rb,
lib/adamantium/freezer.rb,
lib/adamantium/mutable.rb,
lib/adamantium/version.rb,
lib/adamantium/class_methods.rb,
lib/adamantium/module_methods.rb

Overview

Allows objects to be made immutable

Defined Under Namespace

Modules: ClassMethods, Flat, ModuleMethods, Mutable Classes: Freezer

Constant Summary collapse

Memory =

Storage for memoized methods

Class.new(::Hash)
VERSION =

Gem version

'0.0.11'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(descendant) ⇒ self

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.

Hook called when module is included

Parameters:

  • descendant (Module)

    the module or class including Adamantium

Returns:

  • (self)


48
49
50
51
52
# File 'lib/adamantium.rb', line 48

def self.included(descendant)
  descendant.extend ModuleMethods
  descendant.extend ClassMethods  if descendant.kind_of?(Class)
  self
end

Instance Method Details

#dupself

A noop #dup for immutable objects

Examples:

object.dup  # => self

Returns:

  • (self)


110
111
112
# File 'lib/adamantium.rb', line 110

def dup
  self
end

#freezeObject

Freeze the object

Examples:

object.freeze  # object is now frozen

Returns:

  • (Object)


62
63
64
65
# File 'lib/adamantium.rb', line 62

def freeze
  memory  # initialize memory
  super
end

#memoize(name, value) ⇒ self

Sets a memoized value for a method

Examples:

object.memoize(:hash, 12345)

Parameters:

  • name (Symbol)

    the method name

  • value (Object)

    the value to memoize

Returns:

  • (self)


95
96
97
98
99
100
# File 'lib/adamantium.rb', line 95

def memoize(name, value)
  unless memory.key?(name)
    store_memory(name, freeze_object(value))
  end
  self
end

#memoized(name) ⇒ Object

Get the memoized value for a method

Examples:

hash = object.memoized(:hash)

Parameters:

  • name (Symbol)

    the method name

Returns:

  • (Object)


78
79
80
# File 'lib/adamantium.rb', line 78

def memoized(name)
  memory[name]
end