Module: FunWith::Patterns::Loader::ClassMethods
- Defined in:
- lib/fun_with/patterns/loader.rb
Instance Method Summary collapse
-
#loader_pattern_extension(ext = nil) ⇒ Object
By default, looks for .rb files to evaluate ext => :all (or “*”) to load every file from directory.
-
#loader_pattern_load_from_dir(dir) ⇒ Object
Assumes that every file in the directory and subdirectories contain ruby code that will yield an object that the loader is looking for.
-
#loader_pattern_load_item(file) ⇒ Object
Default behavior: read the file, evaluate it, expect a ruby object of the class that the loader pattern is installed on.
- #loader_pattern_loaded_directories ⇒ Object
-
#loader_pattern_register_item(obj) ⇒ Object
Default, may want to override how the registry behaves.
- #loader_pattern_registry_lookup(key) ⇒ Object
- #loader_pattern_verbose(verbosity = nil) ⇒ Object
Instance Method Details
#loader_pattern_extension(ext = nil) ⇒ Object
By default, looks for .rb files to evaluate ext => :all (or “*”) to load every file from directory. ext => :rb (or “rb”) to load all .rb files (this is default) call without arguments to inquire about current extension.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fun_with/patterns/loader.rb', line 14 def loader_pattern_extension( ext = nil ) case ext when nil # do nothing when "*", :all @loader_pattern_extension = "*" else @loader_pattern_extension = "*.#{ext}" end @loader_pattern_extension end |
#loader_pattern_load_from_dir(dir) ⇒ Object
Assumes that every file in the directory and subdirectories contain ruby code that will yield an object that the loader is looking for. It also automatically adds the resulting object to a registry. You may want to override this if you’re looking for different behavior.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fun_with/patterns/loader.rb', line 75 def loader_pattern_load_from_dir( dir ) dir = dir.fwf_filepath @loader_pattern_directories ||= [] @loader_pattern_directories << dir for file in dir.glob( "**", self.loader_pattern_extension ) obj = self.loader_pattern_load_item( file ) self.loader_pattern_register_item( obj ) end end |
#loader_pattern_load_item(file) ⇒ Object
Default behavior: read the file, evaluate it, expect a ruby object of the class that the loader pattern is installed on. If anything goes wrong (file no exist, syntax error), returns a nil.
Override in your class if you need your files translated into objects differently.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fun_with/patterns/loader.rb', line 38 def loader_pattern_load_item( file ) file = file.fwf_filepath if file.file? obj = eval( file.read ) STDOUT.puts( "Loaded file #{file}" ) if self.loader_pattern_verbose return obj else STDERR.puts( "(#{self.class}) Load failed, no such file: #{file}" ) return nil end rescue Exception => e STDERR.puts( "Could not load file #{file}. Reason: #{e.class.name} #{e.}" ) if self.loader_pattern_verbose STDERR.puts( puts e.backtrace.map{|line| "\t\t#{line}"}.join("\n") ) STDERR.puts( "" ) end nil end |
#loader_pattern_loaded_directories ⇒ Object
86 87 88 |
# File 'lib/fun_with/patterns/loader.rb', line 86 def loader_pattern_loaded_directories @loader_pattern_directories ||= [] end |
#loader_pattern_register_item(obj) ⇒ Object
Default, may want to override how the registry behaves.
61 62 63 64 |
# File 'lib/fun_with/patterns/loader.rb', line 61 def loader_pattern_register_item( obj ) return nil if obj.nil? return (@loader_pattern_registry ||= {})[ obj.loader_pattern_registry_key ] = obj end |
#loader_pattern_registry_lookup(key) ⇒ Object
66 67 68 |
# File 'lib/fun_with/patterns/loader.rb', line 66 def loader_pattern_registry_lookup( key ) @loader_pattern_registry[key] end |
#loader_pattern_verbose(verbosity = nil) ⇒ Object
27 28 29 30 |
# File 'lib/fun_with/patterns/loader.rb', line 27 def loader_pattern_verbose( verbosity = nil ) @loader_pattern_verbose = verbosity unless verbosity.nil? @loader_pattern_verbose end |