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/struct.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, Struct

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



81
82
83
84
85
# File 'lib/cow_proxy.rb', line 81

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



41
42
43
44
45
46
# File 'lib/cow_proxy.rb', line 41

def register_proxy(klass, proxy_klass)
  return if @wrapper_classes&.dig(klass)
  debug { "register proxy for #{klass} with #{proxy_klass}#{" < #{proxy_klass.superclass}" if proxy_klass}" }
  @wrapper_classes ||= {}
  @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



55
56
57
58
# File 'lib/cow_proxy.rb', line 55

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



29
30
31
# File 'lib/cow_proxy.rb', line 29

def WrapClass(klass) # rubocop:disable Naming/MethodName
  _wrap_class(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



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

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
    _wrap_class(obj.class, obj.class < ::Struct, true)
  end
end