Sha256: d264f9ca514fca4999f2ad198c915f20bbe776d15b445ca1cf4f598bd6a4de2c

Contents?: true

Size: 915 Bytes

Versions: 1

Compression:

Stored size: 915 Bytes

Contents

module At
  
  # InstanceVariableDelegator is a simply class which delegates it's methods it an object's instance variables.
  class InstanceVariableDelegator
    
    # @param [Object] parent The object to delegate accessors to it's instance variables.
    def initialize(parent)
      @parent = parent
    end
    
    # If the method ends with an equal sign (`=`), we will set the instance variable on the @parent object.
    # Otherwise, we will /get/ the instance variable on the @parent object.
    def method_missing(meth, value=nil)
      @parent.instance_eval do
        match = meth.to_s.match(/(.*)\=$/)
        raise ArgumentError, '`value` must be given' if match && value.nil?
        raise ArgumentError, '`value` must not be given' if !match && !value.nil?
        
        match ? instance_variable_set("@#{match[1]}", value) : instance_variable_get("@#{meth}")
      end
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
at-0.2.0 lib/at/instance_variable_delegator.rb