Module: StateFu::ArrayWithSymbolAccessor

Included in:
EventArray, StateArray
Defined in:
lib/support/arrays.rb

Overview

Stuff shared between StateArray and EventArray

Instance Method Summary collapse

Instance Method Details

#[](idx) ⇒ Object

Pass a symbol to the array and get the object with that .name

<Foo @name=:bob>][:bob

> <Foo @name=:bob>


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/support/arrays.rb', line 10

def []( idx )
  begin
    super( idx )
  rescue TypeError => e
    if idx.respond_to?(:to_sym)
      self.detect { |i| i == idx || i.respond_to?(:name) && i.name == idx.to_sym }
    else
      raise e
    end
  end
end

#allObject


37
38
39
# File 'lib/support/arrays.rb', line 37

def all
  self
end

#except(*syms) ⇒ Object

SPECME


29
30
31
# File 'lib/support/arrays.rb', line 29

def except *syms
  reject {|el| syms.flatten.compact.map(&:to_sym).include?(el.to_sym) } #.extend ArrayWithSymbolAccessor
end

#namesObject

so we can go Machine.states.names mildly helpful with irb + readline


24
25
26
# File 'lib/support/arrays.rb', line 24

def names
  map(&:name)
end

#only(*syms) ⇒ Object


33
34
35
# File 'lib/support/arrays.rb', line 33

def only *syms
  select {|el| syms.flatten.compact.map(&:to_sym).include?(el.to_sym) } #.extend ArrayWithSymbolAccessor
end

#randObject


41
42
43
# File 'lib/support/arrays.rb', line 41

def rand
  self.rand
end