Class: TF::Plugins
Instance Method Summary collapse
- #add(plugin) ⇒ Object
- #delete(plugin) ⇒ Object
- #detect ⇒ Object
- #file_to_class(item) ⇒ Object
-
#initialize ⇒ Plugins
constructor
A new instance of Plugins.
- #input_plugins ⇒ Object
- #list(pattern = nil) ⇒ Object
- #load(wanted) ⇒ Object
- #match_arg_klass(arg, klass, type) ⇒ Object
- #output_plugins(*args) ⇒ Object
- #parse_args(args) ⇒ Object
- #test_plugins ⇒ Object
Constructor Details
#initialize ⇒ Plugins
Returns a new instance of Plugins.
4 5 6 7 8 9 10 |
# File 'lib/tf/plugins.rb', line 4 def initialize detect @additional_plugins = [] @input_plugins = [] @output_plugins = [] @test_plugins = [] end |
Instance Method Details
#add(plugin) ⇒ Object
16 17 18 |
# File 'lib/tf/plugins.rb', line 16 def add plugin @additional_plugins << plugin end |
#delete(plugin) ⇒ Object
20 21 22 |
# File 'lib/tf/plugins.rb', line 20 def delete plugin @additional_plugins.delete plugin end |
#detect ⇒ Object
12 13 14 |
# File 'lib/tf/plugins.rb', line 12 def detect @plugins = Gem.find_files('plugins/tf/*.rb') end |
#file_to_class(item) ⇒ Object
24 25 26 |
# File 'lib/tf/plugins.rb', line 24 def file_to_class item File.basename(item,'.rb').capitalize.gsub(/_(.)/){ $1.upcase } end |
#input_plugins ⇒ Object
87 88 89 |
# File 'lib/tf/plugins.rb', line 87 def input_plugins @input_plugins end |
#list(pattern = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tf/plugins.rb', line 28 def list pattern=nil # collect to lists _list = @plugins + @additional_plugins # filter by pattern if given _list = _list.select{|item| item.match("_#{pattern}.rb$") } unless pattern.nil? # get path and class name _list.map!{|item| [ item, file_to_class(item), pattern ] } # TODO: limit plugin versions (highest || use bundler) _list.each{|item, klass, type| require item } _list end |
#load(wanted) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tf/plugins.rb', line 40 def load wanted [ :input, :test, :output ].each do |type| _list = list(type) if ! wanted.include?("all") && ! wanted.include?("all_#{type}") _list = _list.select{|item, klass, _type| wanted.include?(klass) } end _list.each{|item, klass, _type| klass = TF.const_get(klass) instance_variable_get("@#{type}_plugins".to_sym) << klass.new } end end |
#match_arg_klass(arg, klass, type) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tf/plugins.rb', line 53 def match_arg_klass arg, klass, type klass = TF.const_get(klass) return nil unless klass.respond_to? :argument_matches? matches = klass.argument_matches? arg return nil if matches.nil? matches.each do |match| case match when :load instance_variable_get("@#{type}_plugins".to_sym) << klass.new when :input @input_files << [klass.new, arg] else return nil end end return matches rescue NameError return nil end |
#output_plugins(*args) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/tf/plugins.rb', line 91 def output_plugins *args if args.empty? @output_plugins else @output_plugins.each{|plugin| plugin.send(*args) } end end |
#parse_args(args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/tf/plugins.rb', line 73 def parse_args args @input_files, not_processed = [], [] available_plugins = [ :input, :test, :output ].map{ |type| list(type) }.flatten(1) args.each do |arg| matched = available_plugins.map do |item, klass, type| match_arg_klass arg, klass, type end.flatten.reject(&:nil?) if matched.empty? not_processed << arg end end [ @input_files, not_processed ] end |
#test_plugins ⇒ Object
99 100 101 |
# File 'lib/tf/plugins.rb', line 99 def test_plugins @test_plugins end |