Class: LinkedObject

Inherits:
Object
  • Object
show all
Defined in:
lib/linkedobject/version.rb,
lib/linkedobject/linkedobject.rb

Overview

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                  Version 2, December 2004

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Constant Summary collapse

VERSION =
'0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val = nil, listeners = []) ⇒ LinkedObject

Returns a new instance of LinkedObject.



14
15
16
17
# File 'lib/linkedobject/linkedobject.rb', line 14

def initialize(val = nil, listeners = [])
  @val = val
  @listeners = [self] + listeners
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/linkedobject/linkedobject.rb', line 57

def method_missing(method, *args, &block)
  if method =~ /with_\d_listeners?/
    n = method.to_s.scan(/with(_\d+_)?listeners?/).first.first[1..-2].to_i
    [self] + new_listeners(n, *args)
  else
    @listeners.each { |l| l.apply(method, *args) }
  end
end

Instance Attribute Details

#valObject

Returns the value of attribute val.



12
13
14
# File 'lib/linkedobject/linkedobject.rb', line 12

def val
  @val
end

Instance Method Details

#apply(method, *args) ⇒ Object



53
54
55
# File 'lib/linkedobject/linkedobject.rb', line 53

def apply(method, *args)
  @val = @val.send(method, *args)
end

#get_valObject



39
40
41
# File 'lib/linkedobject/linkedobject.rb', line 39

def get_val
  @val
end

#inspectObject



66
67
68
# File 'lib/linkedobject/linkedobject.rb', line 66

def inspect
  get_val
end

#is_listened_by(o) ⇒ Object Also known as: add_listener



35
36
37
# File 'lib/linkedobject/linkedobject.rb', line 35

def is_listened_by(o)
  @listeners << o
end

#new_listener(*args) ⇒ Object



19
20
21
# File 'lib/linkedobject/linkedobject.rb', line 19

def new_listener(*args)
  new_listeners(1, *args).first
end

#new_listeners(n, *args) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/linkedobject/linkedobject.rb', line 23

def new_listeners(n, *args)
  [].tap do |listeners|
    n.times do
      listeners << self.class.new(*args).tap { |l| self.is_listened_by(l) }
    end
  end
end

#set_val(val) ⇒ Object Also known as: set



43
44
45
# File 'lib/linkedobject/linkedobject.rb', line 43

def set_val(val)
  @val = val
end

#with_listeners(n, *args) ⇒ Object



31
32
33
# File 'lib/linkedobject/linkedobject.rb', line 31

def with_listeners(n, *args)
  [self] + new_listeners(n, *args)
end