Fork me on GitHub

proxeze by jacaetevha

A basic proxy/delegate framework for Ruby that will allow you to wrap any object with a proxy instance.

When Proxeze proxies an object it creates a delegate class under its namespace (using Ruby’s built-in Delegate class) that mirrors your class. It overrides your class’ #new method so that when you create instances of your class you get back the proxy object instead. In this way, you don’t have to change the way you instantiate your objects, but you get the benefit of the proxy pattern. When proxying a class, Proxeze will also proxy most class methods. See my GitHub page for more details.

Install

gem install proxeze

Dependencies

The only dependencies proxeze has are for development:

How will proxeze help me?

Examples tell it best... In this example we have a tree structure of Categories. At some point during the execution of our program we want to hide certain categories in that tree, without adding extra behavior to Category, and without changing the state of the Category instances themselves. Enter the Proxy.

  class Category
    attr_accessor :categories
    attr_reader   :name
    def initialize name
      @name = name
      @categories = []
    end
  end

  # now every time we instantiate Category, we
  # will get back a Proxeze::Category instance
  Proxeze.proxy Category

  # Controls the visibility of the categories
  module Visibility
    def visible?
      @visible = true if @visible.nil?
      @visible
    end

    def be_invisible!
      @visible = false
      categories.each {|e| e.be_invisible!}
      self
    end

    def be_visible!
      @visible = true
      categories.each {|e| e.be_visible!}
      self
    end

    Proxeze::Category.send :include, self
  end

  # instantiate Category objects as we normally would
  c1            = Category.new('1')
  c1.categories = [Category.new('2'), Category.new('3')]
  c2            = c1.categories.first
  c2.categories = [Category.new('4'), Category.new('5'), Category.new('6')]
  c6            = c2.categories.last
  c6.categories = [Category.new('7'), Category.new('8')]

  c6.be_invisible!

  # before we hide c6 and its children   |    after hiding c6
  # we have a tree like this:            |    our tree becomes:
  # ------------------------------------ + --------------------
  #                c1                    +            c1
  #             ___||___                 +         ___||___
  #             ||    ||                 +         ||    ||
  #             c2    c3                 +         c2    c3
  #         ____||____                   +     ____||____
  #         ||  ||  ||                   +     ||      ||
  #         c4  c5  c6                   +     c4      c5
  #              ___||__                 +
  #              ||    |                 +
  #              c7    c8                +
  #                                      |

In this example, we are adding a method interception to the #reverse method on our array that will be called before the #reverse is called. In it, we will append a value to the array.

  p = Proxeze.for [0, 1, 2, 3] do
    before :reverse do |obj, args|
      obj << obj.length
    end
  end

  p.reverse # => [4, 3, 2, 1, 0]

In this example, we are adding a method interception to the #reverse method on our array that will be called after the #reverse is called. In it, we will append a value to the array.

  p = Proxeze.for [0, 1, 2, 3] do
    after :reverse do |obj, result, args|
      obj << obj.length
    end
  end

  p.reverse # => [4, 3, 2, 1, 0]

In this example, we use a class for the callback routine, which is instantiated and called before and after all methods.

  class MyClass
    attr_accessor :foo
  end
  
  class DebugLogInterceptor
    def self.logger
      @logger ||= Logger.new(STDOUT)
      @logger
    end
    
    def initialize callback_type, object, result = nil, method = nil, args = nil
      @callback_type = callback_type
      @object = object
      @result = result
      @method = method
      @args = args
    end
    
    def call
      self.class.logger.debug log_message
    end
    
    def log_message
      "#{@callback_type}: #{@method} on #{@object.inspect} with args[#{@args.inspect}], result is [#{@result.inspect}]"
    end
  end

  p = Proxeze.proxy MyClass do
    after_all  DebugLogInterceptor
    before_all DebugLogInterceptor
  end

  o = MyClass.new
  o.foo = 2 # results in a log statement

License

Copyright (c) 2011 Jason Rogers

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Authors

Jason Rogers (jacaetevha at gmail dot com)

Contact

Jason Rogers (jacaetevha at gmail dot com)

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/jacaetevha/proxeze