Class: Mon::Monad::Reactor

Inherits:
React show all
Defined in:
lib/monads/reactron.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from React

valid?

Methods included from ChainableMonad

#_, #coerce, #method_missing, #respond_to?

Constructor Details

#initialize(fun) ⇒ Reactor

Returns a new instance of Reactor.



121
122
123
# File 'lib/monads/reactron.rb', line 121

def initialize(fun)
  @fun = fun
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mon::Monad::ChainableMonad

Class Method Details

.[](fun) ⇒ Object

You want React



134
135
136
# File 'lib/monads/reactron.rb', line 134

def self::[] fun
  Reactor.new(fun)
end

Instance Method Details

#==(o) ⇒ Object



160
161
162
# File 'lib/monads/reactron.rb', line 160

def ==(o)
  eql? o
end

#bind(&fun) ⇒ Object

Apply fun to the value wrapped by this Reactor (which in turn is a transform on some Reactron), returning another Reactron. Changes to the root Reactron will propagate through the whole tree.



129
130
131
# File 'lib/monads/reactron.rb', line 129

def bind &fun
  Reactor[lambda { fun.call(self.unwrap) }]
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
# File 'lib/monads/reactron.rb', line 148

def eql? o
  if o.is_a? React
    @fun.call == o.unwrap
  else
    @fun.call == o
  end
end

#equal?(o) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/monads/reactron.rb', line 156

def equal? o
  eql? o
end

#to_sObject



164
165
166
# File 'lib/monads/reactron.rb', line 164

def to_s
  "Reactor[#{ unwrap }]"
end

#unwrapObject

Unwrap the (current) value contained by this Reactor



139
140
141
142
# File 'lib/monads/reactron.rb', line 139

def unwrap
  r = @fun.call
  r.is_a?(Reactor) ? r.unwrap : r
end