Module: GObjectIntrospection::CollectionReader

Included in:
BoxedInfo, CallableInfo, InterfaceInfo, ObjectInfo, StructInfo, UnionInfo
Defined in:
lib/gobject-introspection/collection-reader.rb

Instance Method Summary collapse

Instance Method Details

#collection_reader(name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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