Class: Ki::DirectoryWithChildrenInListFile
- Defined in:
- lib/data_storage/ki_json.rb
Overview
Helper method for creating list files.
Class Method Summary collapse
-
.add_list_file(obj, clazz, name = nil) ⇒ Object
Helper method for creating list files.
Class Method Details
.add_list_file(obj, clazz, name = nil) ⇒ Object
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)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# 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 |