Module: Card::Set::Loader
- Included in:
- Card::Set
- Defined in:
- lib/card/set/loader.rb
Overview
- Definition: interpret each set file, creating/defining set and
set_format modules
- Organization: have base classes include modules associated with the 'all' set, and clean up the other modules
Instance Method Summary collapse
- #clean_empty_module_from_hash(hash) ⇒ Object
- #clean_empty_modules ⇒ Object
-
#extended(mod) ⇒ Object
each set file calls
extend Card::Set
when loaded. - #pattern_and_modules_from_path(path) ⇒ Object
-
#process_base_modules ⇒ Object
'base modules' are modules that are permanently included on the Card or Format class 'nonbase modules' are included dynamically on singleton_classes.
-
#register_set(set_module) ⇒ Object
make the set available for use.
- #tmp_file_frame(pattern, submodules, content) ⇒ Object
- #tmp_file_template(pattern, modules, content_path) ⇒ Object
- #tmp_file_wrapped_content(content_path, content) ⇒ Object
- #write_tmp_file(from_file, to_file, rel_path) ⇒ Object
Instance Method Details
#clean_empty_module_from_hash(hash) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/card/set/loader.rb', line 111 def clean_empty_module_from_hash hash hash.each do |mod_name, modlist| modlist.delete_if { |x| x.instance_methods.empty? } hash.delete mod_name if modlist.empty? end end |
#clean_empty_modules ⇒ Object
104 105 106 107 108 109 |
# File 'lib/card/set/loader.rb', line 104 def clean_empty_modules clean_empty_module_from_hash modules[:nonbase] modules[:nonbase_format].values.each do |hash| clean_empty_module_from_hash hash end end |
#extended(mod) ⇒ Object
each set file calls extend Card::Set
when loaded
16 17 18 |
# File 'lib/card/set/loader.rb', line 16 def extended mod register_set mod end |
#pattern_and_modules_from_path(path) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/card/set/loader.rb', line 46 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 |
#process_base_modules ⇒ Object
'base modules' are modules that are permanently included on the Card or Format class 'nonbase modules' are included dynamically on singleton_classes
94 95 96 97 98 99 100 101 102 |
# File 'lib/card/set/loader.rb', line 94 def process_base_modules return unless modules[:base] Card.add_set_modules modules[:base] modules[:base_format].each do |format_class, modules_list| format_class.add_set_modules modules_list end modules.delete :base modules.delete :base_format end |
#register_set(set_module) ⇒ Object
make the set available for use
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/card/set/loader.rb', line 21 def register_set set_module if set_module.all_set? # automatically included in Card class modules[:base] << set_module else set_type = set_module.abstract_set? ? :abstract : :nonbase # made ready for dynamic loading via #include_set_modules modules[set_type][set_module.shortname] ||= [] modules[set_type][set_module.shortname] << set_module end end |
#tmp_file_frame(pattern, submodules, content) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/card/set/loader.rb', line 70 def tmp_file_frame pattern, submodules, content <<-RUBY # -*- encoding : utf-8 -*- class Card; module Set; class #{pattern}; #{submodules.join ' '} #{content} end;end;end;#{'end;' * submodules.size} RUBY end |
#tmp_file_template(pattern, modules, content_path) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/card/set/loader.rb', line 54 def tmp_file_template pattern, modules, content_path content = File.read(content_path) wrapped_content = tmp_file_wrapped_content content_path, content # TODO: load directly without tmp file return wrapped_content if content =~ /\A#!\s?simple load/ submodules = modules.map { |m| "module #{m};" } if content =~ /\A#!\s?not? set module/ submodules.pop else submodules[-1] += " extend Card::Set" end tmp_file_frame pattern, submodules, wrapped_content end |
#tmp_file_wrapped_content(content_path, content) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/card/set/loader.rb', line 79 def tmp_file_wrapped_content content_path, content <<-RUBY # ~~ above autogenerated; below pulled from #{content_path} ~~ #{content} # ~~ below autogenerated; above pulled from #{content_path} ~~ RUBY end |
#write_tmp_file(from_file, to_file, rel_path) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/card/set/loader.rb', line 39 def write_tmp_file from_file, to_file, rel_path pattern, submodules = pattern_and_modules_from_path rel_path FileUtils.mkdir_p File.dirname(to_file) File.write to_file, tmp_file_template(pattern, submodules, from_file) to_file end |