Class: Module

Inherits:
Object show all
Defined in:
lib/garcon/core_ext/module.rb

Overview

Author: Stefano Harding <[email protected]> License: Apache License, Version 2.0 Copyright: © 2014-2015 Stefano Harding

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instance Method Summary collapse

Instance Method Details

#set(option, value = self, &block) ⇒ Object

Sets an option to the given value. If the value is a proc, the proc will be called every time the option is accessed.

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/garcon/core_ext/module.rb', line 67

def set(option, value=self, &block)
  raise ArgumentError if block && value != self
  value = block if block
  if value.kind_of?(Proc)
    if value.arity == 1
      yield self
    else
      (class << self; self; end).module_eval do
        define_method(option, &value)
        define_method("#{option}?"){ !!__send__(option) }
        define_method("#{option}="){ |val| set(option, Proc.new{val}) }
      end
    end
  elsif value == self
    option.each{ |k,v| set(k, v) }
  elsif respond_to?("#{option}=")
    __send__("#{option}=", value)
  else
    set(option, Proc.new{value})
  end
  self
end

#to_objObject

Create an instance of Object and extend it with self.

mod = Module.new do
  def foo; "foo"; end
end

obj = mod.to_obj

obj.foo #=> "foo"


31
32
33
34
35
# File 'lib/garcon/core_ext/module.rb', line 31

def to_obj
  o = Object.new
  o.extend self
  o
end

#wrap_method(sym, &blk) ⇒ Object Also known as: wrap

Creates a new method wrapping the previous of the same name. Reference to the old method is passed into the new definition block as the first parameter.

class WrapExample
  def foo
    "foo"
  end

  wrap_method(:foo) do |old_meth, *args|
    old_meth.call + '!'
  end
end

example = WrapExample.new
example.foo #=> 'foo!'

Keep in mind that this cannot be used to wrap methods that take a block.



57
58
59
60
# File 'lib/garcon/core_ext/module.rb', line 57

def wrap_method( sym, &blk )
  old = instance_method(sym)
  define_method(sym) { |*args| blk.call(old.bind(self), *args) }
end