save_class(klass)
public
Writes the ri data for klass (or module)
Show source
def save_class klass
full_name = klass.full_name
FileUtils.mkdir_p class_path(full_name) unless @dry_run
@cache[:modules] << full_name
path = class_file full_name
begin
disk_klass = load_class full_name
klass = disk_klass.merge klass
rescue MissingFileError
end
ancestors = klass.direct_ancestors.compact.map do |ancestor|
String === ancestor ? ancestor : ancestor.full_name
end
@cache[:ancestors][full_name] ||= []
@cache[:ancestors][full_name].concat ancestors
attribute_definitions = klass.attributes.map do |attribute|
"#{attribute.definition} #{attribute.name}"
end
unless attribute_definitions.empty? then
@cache[:attributes][full_name] ||= []
@cache[:attributes][full_name].concat attribute_definitions
end
to_delete = []
unless klass.method_list.empty? then
@cache[:class_methods][full_name] ||= []
@cache[:instance_methods][full_name] ||= []
class_methods, instance_methods =
klass.method_list.partition { |meth| meth.singleton }
class_methods = class_methods. map { |method| method.name }
instance_methods = instance_methods.map { |method| method.name }
attribute_names = klass.attributes.map { |attr| attr.name }
old = @cache[:class_methods][full_name] - class_methods
to_delete.concat old.map { |method|
method_file full_name, "#{full_name}::#{method}"
}
old = @cache[:instance_methods][full_name] -
instance_methods - attribute_names
to_delete.concat old.map { |method|
method_file full_name, "#{full_name}##{method}"
}
@cache[:class_methods][full_name] = class_methods
@cache[:instance_methods][full_name] = instance_methods
end
return if @dry_run
FileUtils.rm_f to_delete
marshal = Marshal.dump klass
open path, 'wb' do |io|
io.write marshal
end
end