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.



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

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



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

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



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

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