lib/card/set/loader.rb in card-1.18.6 vs lib/card/set/loader.rb in card-1.19.0
- old
+ new
@@ -32,53 +32,53 @@
#
# When a Card application loads, it uses set modules to autogenerate
# tmp files that add module names (Card::Set::PATTERN::ANCHOR) and
# extend the module with Card::Set.
- #
#
- #
def write_tmp_file from_file, to_file, rel_path
- name_parts = rel_path.gsub(/\.rb/, '').split(File::SEPARATOR)
- submodules = name_parts.map { |a| "module #{a.camelize};" } * ' '
- file_content = <<EOF
-# -*- encoding : utf-8 -*-
-class Card; module Set; #{submodules} extend Card::Set
-# ~~~~~~~~~~~ above autogenerated; below pulled from #{from_file} ~~~~~~~~~~~
-#{File.read from_file}
-
-# ~~~~~~~~~~~ below autogenerated; above pulled from #{from_file} ~~~~~~~~~~~
-end;end;#{'end;' * name_parts.size}
-EOF
-
+ pattern, submodules = pattern_and_modules_from_path rel_path
FileUtils.mkdir_p File.dirname(to_file)
- File.write to_file, file_content
+ File.write to_file, tmp_file_template(pattern, submodules, from_file)
to_file
end
+ def pattern_and_modules_from_path path
+ # remove file extension and number prefixes
+ parts = path.gsub(/\.rb/, "").gsub(%r{(?<=\A|/)\d+_}, "")
+ .split(File::SEPARATOR)
+ parts.map! &:camelize
+ [parts.shift, parts]
+ end
+
+ def tmp_file_template pattern, modules, content_path
+ submodules = modules.map { |m| "module #{m};" }
+<<-RUBY
+# -*- encoding : utf-8 -*-
+class Card; module Set; class #{pattern}; #{submodules * ' '} extend Card::Set
+# ~~ above autogenerated; below pulled from #{content_path} ~~
+#{File.read content_path}
+
+# ~~ below autogenerated; above pulled from #{content_path} ~~
+end;end;end;#{'end;' * submodules.size}
+RUBY
+ end
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Organization Phase
# 'base modules' are modules that are permanently included on the Card or
# Format class
# 'nonbase modules' are included dynamically on singleton_classes
def process_base_modules
- process_base_module_list modules[:base], Card
+ return unless modules[:base]
+ Card.add_set_modules modules[:base]
modules[:base_format].each do |format_class, modules_list|
- process_base_module_list modules_list, format_class
+ format_class.add_set_modules modules_list
end
modules.delete :base
modules.delete :base_format
- end
-
- def process_base_module_list list, klass
- list.each do |mod|
- klass.send :include, mod if mod.instance_methods.any?
- if (class_methods = mod.const_get_if_defined(:ClassMethods))
- klass.send :extend, class_methods
- end
- end
end
def clean_empty_modules
clean_empty_module_from_hash modules[:nonbase]
modules[:nonbase_format].values.each do |hash|