Module: Swappable

Defined in:
lib/swap.rb

Overview

Swappable mixin.

Examples
Class User
  extend Swappable
end

Instance Method Summary collapse

Instance Method Details

#swap!(name, code = nil, &block) ⇒ Object

Swaps a method for another

Arguments

name<Symbol>

Name of original method to swap

code<String|Proc|UnboundMethod>

New code for method

Block

Alternative way to pass method code

Returns

self

Examples

User.swap!(:name, "@name.reverse")
User.swap!(:name, lambda { @name.reverse })
User.swap!(:name) { @name.reverse }
User.swap!(:name, User.instance_method(:first_name))


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/swap.rb', line 33

def swap!(name, code=nil, &block)
  name = name.to_sym
  meth = self.instance_method(name.to_sym)
  @swapped_methods ||= {}
  @swapped_methods[name] = meth
  if block_given?
    self.class_eval { define_method(name, &block) }
  else
    case code
    when String
      self.class_eval(%|def #{name}() #{code}; end|)
    when Proc, UnboundMethod
      self.class_eval { define_method(name, code) }
    end
  end
  self
end

#unswap!(name = nil) ⇒ Object

Restores a swapped method

Arguments

name<String>

Name of method to restore. If nil, all swapped methods for this class will be restored.

Returns

self

Examples

class User
  attr_accessor :name
end
user = User.new
user.name = 'martin'
puts user.name #=> 'martin'

User.swap!(:name) { @name.reverse }
puts user.name #=> 'nitram'

User.unswap!(:name)
puts user.name #=> 'martin'


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/swap.rb', line 76

def unswap!(name=nil)
  if name
    name = name.to_sym
    self.class_eval { define_method(name, @swapped_methods[name]) }
  else
    @swapped_methods.each do |name, code|
      self.class_eval { define_method(name, code) }
    end
  end
  self
end