Class: Rubeus::Util::NameAccessArray

Inherits:
Array
  • Object
show all
Defined in:
lib/rubeus/util/name_access_array.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ NameAccessArray

Returns a new instance of NameAccessArray.



5
6
7
8
9
10
11
12
13
14
# File 'lib/rubeus/util/name_access_array.rb', line 5

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options = {
    :name_attr => :name,
    :detect_with => :to_s
  }.update(options)
  @name_attr = options[:name_attr]
  @detect_with = options[:detect_with]
  args.each{|arg| push(arg)}
end

Instance Method Details

#[](*args) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rubeus/util/name_access_array.rb', line 27

def [](*args)
  if args.length == 1
    if (args.first.is_a?(Symbol) || args.first.is_a?(String))
      return by_name(args.first)
    end
  end
  return super
end

#by_name(name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/rubeus/util/name_access_array.rb', line 16

def by_name(name)
  return nil unless name
  detect_name = name.send(@detect_with)
  detect do |item|
    next unless item
    ite_name = item.send(@name_attr)
    next unless ite_name
    ite_name.send(@detect_with) == detect_name
  end
end