module GObjectIntrospection::CollectionReader

Public Instance Methods

collection_reader(name) click to toggle source
# File lib/gobject-introspection/collection-reader.rb, line 19
def collection_reader(name)
  n_getter = "n_#{name}"
  if name.end_with?("ies")
    singular = name.sub(/ies\z/, "y")
  else
    singular = name.sub(/s\z/, "")
  end
  getter = "get_#{singular}"
  cache_name = "@#{name}"
  define_method(name) do
    if instance_variable_defined?(cache_name)
      instance_variable_get(cache_name)
    else
      collection = send(n_getter).times.collect do |i|
        send(getter, i)
      end
      instance_variable_set(cache_name, collection)
      collection
    end
  end
end