class Ki::DirectoryWithChildrenInListFile
Helper method for creating list files.
Public Class Methods
add_list_file(obj, clazz, name=nil)
click to toggle source
Helper method for creating list files. When called on a class it extends the class with:
-
class extending
KiJSONListFile
which stores list items: class VersionListFile -
method to load the file: versions()
-
method to load a specific item from list file: version(version_id, versions_list=versions)
# File lib/data_storage/ki_json.rb, line 120 def self.add_list_file(obj, clazz, name=nil) stripped_class_name = clazz.name.split("::").last class_name = clazz.name list_class_name = "#{stripped_class_name}ListFile" create_id_name = stripped_class_name.gsub(/.[A-Z]/) { |s| "#{s[0]}_#{s[1]}" }.downcase if name.nil? name = create_id_name end pluralized_name = "#{name}s".gsub(/ys$/, "ies") new_methods = <<METHODS class #{list_class_name} < KiJSONListFile def create_list_item(#{name}_id) i = #{class_name}.new(#{name}_id) i.parent(parent) i.#{create_id_name}_id(#{name}_id) i end end def #{pluralized_name} #{list_class_name}.new("ki-#{pluralized_name}.json").parent(self) end def #{name}(#{name}_id, #{pluralized_name}_list=#{pluralized_name}) #{pluralized_name}_list.each do |c| if c.#{name}_id == #{name}_id return c end end raise "#{class_name} '\#{#{name}_id}' not found" end METHODS obj.class_eval(new_methods, __FILE__, (__LINE__ - new_methods.split("\n").size - 1)) end