Class: Geeklets
- Inherits:
-
Object
- Object
- Geeklets
- Defined in:
- lib/geeklets.rb
Instance Method Summary collapse
-
#initialize(scripts = {}) ⇒ Geeklets
constructor
A new instance of Geeklets.
- #run(params) ⇒ Object
- #show_known_scripts ⇒ Object
- #show_usage ⇒ Object
Constructor Details
#initialize(scripts = {}) ⇒ Geeklets
Returns a new instance of Geeklets.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/geeklets.rb', line 11 def initialize(scripts = {}) @geeklet_scripts = scripts if @geeklet_scripts.empty? cwd = File.dirname(__FILE__) children = Pathname.new(cwd).children children.reject! { |child| !child.directory? } children.each do |child_dir| geeklet_name = child_dir.basename.to_s geeklet_file = geeklet_name.downcase begin Kernel.require "#{geeklet_name}/#{geeklet_file}" @geeklet_scripts[geeklet_name] = Kernel.eval("#{geeklet_name}.new") rescue => e Kernel.puts "Problem loading #{geeklet_name} geeklet." # Kernel.puts e.inspect # Kernel.puts e.backtrace next end end end end |
Instance Method Details
#run(params) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/geeklets.rb', line 46 def run(params) if params.nil? || params.empty? show_usage show_known_scripts else geeklet = params.shift if @geeklet_scripts.include?(geeklet) @geeklet_scripts[geeklet].run(params) else Kernel.puts "I do not know how to run the #{geeklet} geeklet." show_known_scripts end end end |
#show_known_scripts ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/geeklets.rb', line 38 def show_known_scripts Kernel.puts "These are the currently known geeklet scripts:" Kernel.puts Kernel.puts "There are no defined geeklet scripts." if @geeklet_scripts.empty? @geeklet_scripts.keys.sort.each { |key| Kernel.puts "\t#{key}" } Kernel.puts end |
#show_usage ⇒ Object
33 34 35 36 |
# File 'lib/geeklets.rb', line 33 def show_usage puts "Usage: geeklets <geeklet-script> [relevant-parameters-for-script]" puts end |