Module: Reac::Reactivize

Defined in:
lib/reac/reactivize.rb

Constant Summary collapse

OLD_NAME =

reactivize - make methods to take reactive values NOTE: currently supports one-arg method only

exmaple: class String

alias __old_plus__ +
def +(other)
  if other.kind_of?(Reac)
    Reac(self) + other
  else
    self.__old_plus__(other)
  end
end

end

{
  :+ => "plus",
  :- => "minus",
  :* => "mul",
  :/ => "div"
}

Instance Method Summary collapse

Instance Method Details

#reactivize(*methods) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reac/reactivize.rb', line 26

def reactivize(*methods)
  methods.each do |m|
    old_name = OLD_NAME[m] || m
    module_eval <<-EOD
      alias __old_#{old_name}__ #{m}
      def #{m}(arg)
        if arg.kind_of?(Reac)
          Reac(self).#{m}(arg)
        else
          __old_#{old_name}__(arg)
        end
      end
    EOD
  end
end

#reactivize_singleton(*methods) ⇒ Object

reactivize singleton methods.

example:

module Math
 reactivize_singleton :sin, :cos
end


48
49
50
51
52
53
54
55
# File 'lib/reac/reactivize.rb', line 48

def reactivize_singleton(*methods)
  method_list = methods.map{|m|m.inspect}.join(',')
  instance_eval <<-EOD
    class << self
      reactivize #{method_list}
    end
  EOD
end