Module: Such::Part

Defined in:
lib/such/part.rb

Constant Summary collapse

PLUG_PATTERN =
/^(?<sym>[^\W_]+)_(?<cls>[^\W_]+)$/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(maybe, *args) ⇒ Object

Maybe a plug down the plugged things responds? This is a search through the connections which returns the first(non-nil) response.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/such/part.rb', line 29

def method_missing(maybe,*args)
  super unless args.length==0 and PLUG_PATTERN.match?(maybe)
  self.class.plugs.each do |plug|
    thing = public_send(plug)
    if thing.is_a? Such::Part
      if obj = thing.public_send(maybe)
        return obj
      end
    end
  end
  return nil
end

Instance Method Details

#initialize(*parameters, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/such/part.rb', line 5

def initialize(*parameters, &block)
  super(*parameters)
  self.class.plugs.each do |plg|
    if md = PLUG_PATTERN.match(plg)
      plg,sym,cls = "#{plg}=".to_sym,
                    "#{md[:sym]}!".to_sym,
                    Object.const_get("Such::#{md[:cls]}")
      # self.<plg> = Such::<cls>.new(self, :<sym>!, &block)
      public_send plg, cls.new(self, sym, &block)
    end
  end
end

#message(*parameters) ⇒ Object

Each partSuch::Part can invoke ‘message` on each of its connected part, and so on… Any part can override its `message` method to act as a terminal node or modify its relay behavior.



22
23
24
# File 'lib/such/part.rb', line 22

def message(*parameters)
  self.class.plugs.each{|plg| public_send(plg).message(*parameters) }
end