Module: Hamster::Immutable::ClassMethods

Defined in:
lib/hamster/immutable.rb

Instance Method Summary collapse

Instance Method Details

#memoize(*names) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hamster/immutable.rb', line 17

def memoize(*names)
  include MemoizeMethods unless include?(MemoizeMethods)
  names.each do |name|
    original_method = "__hamster_immutable_#{name}__"
    alias_method original_method, name
    class_eval <<-METHOD, __FILE__, __LINE__
      def #{name}
        if @__hamster_immutable_memory__.instance_variable_defined?(:@#{name})
          @__hamster_immutable_memory__.instance_variable_get(:@#{name})
        else
          @__hamster_immutable_memory__.instance_variable_set(:@#{name}, #{original_method})
        end
      end
    METHOD
  end
end

#new(*args) ⇒ Object


13
14
15
# File 'lib/hamster/immutable.rb', line 13

def new(*args)
  super.__send__(:immutable!)
end