Module: CowProxy

Defined in:
lib/cow_proxy.rb,
lib/cow_proxy/set.rb,
lib/cow_proxy/base.rb,
lib/cow_proxy/hash.rb,
lib/cow_proxy/array.rb,
lib/cow_proxy/string.rb,
lib/cow_proxy/version.rb,
lib/cow_proxy/indexable.rb,
lib/cow_proxy/enumerable.rb

Overview

This module include public api for CowProxy usage

Examples:

Create a CowProxy class and register to be used by wrap

module CowProxy
  class CustomClass < WrapClass(::CustomClass)
  end
end

Call CowProxy.wrap with object to be proxied

obj = CustomClass.new
obj.freeze
proxy = CowProxy.wrap(obj)

Defined Under Namespace

Modules: Enumerable, Indexable, Version Classes: Array, Base, Hash, Set, String

Class Method Summary collapse

Class Method Details

.debug(line = nil) ⇒ Object

Print debug line if debug is enabled (ENV true) Accepts a block instead of line, so interpolation is skipped when debug is disabled

Parameters:

  • line (String) (defaults to: nil)

    debug line to print

Returns:

  • nil


83
84
85
86
87
# File 'lib/cow_proxy.rb', line 83

def debug(line = nil)
  return unless ENV['DEBUG']
  line ||= yield if block_given?
  Kernel.puts line
end

.register_proxy(klass, proxy_klass) ⇒ Object

Register proxy to be used when wrapping an object of klass.

It's called automatically when inheriting from class returned by WrapClass Can be called with nil proxy_klass to disable wrapping objects of klass, for example Integer is registered with nil because there is no point in wrapping immutable classes.

Returns:

  • proxy_klass


45
46
47
48
# File 'lib/cow_proxy.rb', line 45

def register_proxy(klass, proxy_klass)
  debug { "register proxy for #{klass} with #{proxy_klass}#{" < #{proxy_klass.superclass}" if proxy_klass}" } unless @@wrapper_classes[klass]
  @@wrapper_classes[klass] ||= proxy_klass
end

.wrap(obj) ⇒ Object

Returns a proxy wrapping obj, using registered class for obj's class. If no class is registered for obj's class, it uses default proxy, without copy on write.

If class is registered with nil Proxy, return obj.

Returns:

  • wrapped obj with CowProxy class


57
58
59
60
# File 'lib/cow_proxy.rb', line 57

def wrap(obj)
  klass = wrapper_class(obj)
  klass ? klass.new(obj) : obj
end

.WrapClass(klass) ⇒ Object

Create new proxy class for klass, with copy on write enabled.

In other case CowProxy will wrap objects of klass without copy on write

module CowProxy
  module MyModule
    class MyClass < WrapClass(::MyModule::MyClass)
    end
  end
end

Returns:

  • new proxy class, so it can be used to create a class which inherits from it


33
34
35
# File 'lib/cow_proxy.rb', line 33

def WrapClass(klass)
  _WrapClass(klass)
end

.wrapper_class(obj) ⇒ Object

Returns proxy wrapper class for obj. It will return registered proxy or default proxy without copy on write if none is registered.

Returns:

  • registered proxy or default proxy without copy on write if none is registered


68
69
70
71
72
73
74
75
# File 'lib/cow_proxy.rb', line 68

def wrapper_class(obj)
  # only classes with defined wrapper and Structs has COW enabled by default
  if @@wrapper_classes.has_key?(obj.class)
    @@wrapper_classes[obj.class]
  else
    _WrapClass(obj.class, obj.class < Struct, true)
  end
end