Module: FunWith::Patterns::Loader::ClassMethods

Defined in:
lib/fun_with/patterns/loader.rb

Instance Method Summary collapse

Instance Method Details

#loader_pattern_configure(*args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/fun_with/patterns/loader.rb', line 130

def loader_pattern_configure( *args )
  for arg in args
    case arg
    when :bracketwise_lookup
      self.class_eval do
        def self.[]( key )
          loader_pattern_registry_lookup( key )
        end
        
        def self.[]=( key, val )
          loader_pattern_register_item( val, key )
        end
      end
    when :warn_on_key_changes
      @loader_pattern_warn_on_key_changes = true
    when :dont_warn_on_key_changes
      @loader_pattern_warn_on_key_changes = false
    when Hash
      for key, val in arg
        case key
        when :key
          self.class_eval do
            eval( "alias :loader_pattern_registry_key #{val.inspect}" )
          end
        when :verbose
          self.loader_pattern_verbose( val )
        end
      end
    end
  end
end

#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_is_item_registerable?(item) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
# File 'lib/fun_with/patterns/loader.rb', line 98

def loader_pattern_is_item_registerable?( item )
  return true if loader_pattern_only_register_classes.fwf_blank?
  
  for klass in @loader_pattern_only_register_classes
    return true if item.is_a?( klass )
  end
  
  return false
end

#loader_pattern_load_from_dir(*dirs) ⇒ 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.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fun_with/patterns/loader.rb', line 113

def loader_pattern_load_from_dir( *dirs )
  for dir in dirs
    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 ) if self.loader_pattern_is_item_registerable?( obj )
    end
  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.message}" )
  
  if self.loader_pattern_verbose
    STDERR.puts( puts e.backtrace.map{|line| "\t\t#{line}"}.join("\n") )
    STDERR.puts( "\n" )
  end
  
  nil
end

#loader_pattern_loaded_directoriesObject



126
127
128
# File 'lib/fun_with/patterns/loader.rb', line 126

def loader_pattern_loaded_directories
  @loader_pattern_directories ||= []
end

#loader_pattern_only_register_classes(*args) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/fun_with/patterns/loader.rb', line 90

def loader_pattern_only_register_classes( *args )
  if args.length > 0
    @loader_pattern_only_register_classes = args
  end
    
  @loader_pattern_only_register_classes || []
end

#loader_pattern_register_item(obj, key = nil) ⇒ Object

Default, may want to override how the registry behaves. If you don’t provide a key argument, then the object needs to respond to .loader_pattern_registry_key()



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fun_with/patterns/loader.rb', line 63

def loader_pattern_register_item( obj, key = nil )
  return nil if obj.nil?
  @loader_pattern_registry ||= {}

  key = obj.loader_pattern_registry_key if key.nil?
  
  if loader_pattern_is_item_registerable?( obj )
    if @loader_pattern_warn_on_key_changes && loader_pattern_registry_lookup( key )
      warn( "class #{self} is replacing lookup key #{key.inspect}" )
    end
      
    return @loader_pattern_registry[ key ] = obj
  else
    warn( "#{obj} is not an instance of a registerable class.  Registerable classes: #{self.loader_pattern_only_register_classes.inspect}" )
    return nil
  end
end

#loader_pattern_registryObject



86
87
88
# File 'lib/fun_with/patterns/loader.rb', line 86

def loader_pattern_registry
  @loader_pattern_registry
end

#loader_pattern_registry_lookup(key) ⇒ Object



81
82
83
84
# File 'lib/fun_with/patterns/loader.rb', line 81

def loader_pattern_registry_lookup( key )
  @loader_pattern_registry ||= {}
  @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