class ProtobufDescriptor::NamedCollection

Array wrapper that also supports lookup by “name”

By default all members must respond to name, but this behavior can be overriden by passing a block in the initializer.

Attributes

collection[RW]
matcher[RW]

Public Class Methods

new(collection, &matcher) click to toggle source
# File lib/protobuf_descriptor/named_collection.rb, line 16
def initialize(collection, &matcher)
  @collection = []
  collection.each { |c| @collection << c }

  if block_given?
    @matcher = matcher
  else
    @matcher = lambda { |name, member| return member.name == name }
  end
end

Public Instance Methods

[](index) click to toggle source
# File lib/protobuf_descriptor/named_collection.rb, line 27
def [](index)
  if Fixnum === index
    return collection[index]
  else
    return find_by_name(index)
  end
end
find_by_name(name) click to toggle source
# File lib/protobuf_descriptor/named_collection.rb, line 35
def find_by_name(name)
  return collection.find { |member|
    matcher.call(name.to_s, member)
  }
end
to_a() click to toggle source
# File lib/protobuf_descriptor/named_collection.rb, line 41
def to_a
  return self.collection.to_a
end