Module: Vigilem::Core::Multiplexer

Extended by:
ActiveSupport::Concern, Support::ObjSpace, ClassMethods
Defined in:
lib/vigilem/core/multiplexer.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

extended

Instance Attribute Details

#inputsArray

Returns:

  • (Array)


85
86
87
# File 'lib/vigilem/core/multiplexer.rb', line 85

def inputs
  @inputs ||= []
end

#outObject Also known as: output

Returns the value of attribute out.



19
20
21
# File 'lib/vigilem/core/multiplexer.rb', line 19

def out
  @out
end

Instance Method Details

#__sweep__(len) ⇒ Array

Returns:

  • (Array)

Raises:

  • (ArgumentError)


54
55
56
57
# File 'lib/vigilem/core/multiplexer.rb', line 54

def __sweep__(len)
  recursive_check(RuntimeError)
  sweep(len)
end

#add_inputs(*added_inputs) ⇒ Array<IO || Array>

adds a inputs and runs a uniq

Parameters:

  • added_inputs (Array<IO || Array>)

Returns:

  • (Array<IO || Array>)


92
93
94
95
96
97
98
# File 'lib/vigilem/core/multiplexer.rb', line 92

def add_inputs(*added_inputs)
  if not added_inputs.empty?
    (self.inputs += added_inputs).uniq! {|obj| obj.object_id }
    recursive_check
  end
  self.inputs
end

#initialize_multiplexer(in_ios_or_arrays, out = nil) ⇒ Object

Parameters:

  • in_ios_or_arrays (Array<IO || Array>)
  • out (Array<#<< || #concat>) (defaults to: nil)

Returns:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vigilem/core/multiplexer.rb', line 28

def initialize_multiplexer(in_ios_or_arrays, out=nil)
  @inputs = in_ios_or_arrays
  
  @out = out
  self.class.obj_register(self)
  multiplexers = ::Vigilem::Core::Multiplexer.all
  if (ties = multiplexers.flat_map {|muxer| self.intersect muxer }).any?
    raise NotImplemented, "Multiplexers cannot share the same inputs `#{ties.inspect}'"
  end
  multiplexers << self
end

#inspectString

Returns:

  • (String)

See Also:

  • Object#inspect


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/vigilem/core/multiplexer.rb', line 119

def inspect
  if out.is_a? Array  # @todo switch from regex
    updated_out = super.gsub(/@out=.+?(\s+|>)/, "@out=#<#{out.class}:#{Support::Utils.inspect_id(out)} #{out}>\\1")
    
    if inputs.any? {|inp| inp.is_a? Array }
      str_ary_bdy = inputs.map do |inp| 
        if inp.is_a?(Array) 
          "#<#{inp.class}:#{Support::Utils.inspect_id(inp)} #{inp}>"
        else
          inp
        end
      end.join(', ')
      updated_out.gsub(/@inputs=.+?(\s+|>)/, "@inputs=[#{str_ary_bdy}]\\1")
    end || updated_out
    
  else
    super
  end
end

#intersect(*other_mux_or_ios_or_arrays) ⇒ Array

compares the inputs to the list or multiplexer given

Parameters:

  • other_mux_or_ios_or_arrays (Multiplexer || Array<IO> || Array<Array<>>)

Returns:

  • (Array)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/vigilem/core/multiplexer.rb', line 142

def intersect(*other_mux_or_ios_or_arrays)
  ios, non_fileno = self.inputs.partition {|inpt| inpt.respond_to?(:fileno) }
  ios_fn = ios.map(&:fileno)
  
  non_fileno.map!(&:object_id)
  
  ary = other_mux_or_ios_or_arrays.respond_to?(:inputs) ? other_mux_or_ios_or_arrays.inputs : other_mux_or_ios_or_arrays
  ary.select do |inp|
    if inp.respond_to? :fileno
      ios_fn.include? inp.fileno
    else
      non_fileno.include? inp.object_id  #default compares by #hash
    end
  end
end

#mux(len) ⇒ Array

pushes the converted array from #sweep to #out

Parameters:

  • len (Integer)

Returns:

  • (Array)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vigilem/core/multiplexer.rb', line 70

def mux(len)
  ary = __sweep__(len)
  if out.respond_to? :concat
    out.concat(ary)
  elsif out.respond_to? :<<
    ary.each {|item| out << item }
  else
    raise TypeError, "`#{self}'#out is `#{self.out.inspect}' and does not respond to #concat or #<<"
  end
  out.flush if out.respond_to? :flush
  ary
end

#ready?Array<IO> || NilClass

TODO:

change name to select?

Returns objects ready to be read, or nil if nothing is to be read.

Returns:

  • (Array<IO> || NilClass)

    objects ready to be read, or nil if nothing is to be read



43
44
45
46
47
48
49
# File 'lib/vigilem/core/multiplexer.rb', line 43

def ready?
  if not (ios = inputs.select {|io| io.respond_to? :to_io }).empty?
    @ready, others = IO.select(ios, nil, nil, 0)
  end
  @ready ||= []
  @ready += inputs.select {|ary| ary.respond_to?(:empty?) and ary.empty? }
end

#sweep(num) ⇒ Object

Parameters:

  • num (Integer)

Returns:

Raises:

  • (NotImplemented)


62
63
64
# File 'lib/vigilem/core/multiplexer.rb', line 62

def sweep(num)
  raise NotImplemented, "#sweep method not defined on `#{self}' needs to be overriden"
end