Class: Boson::Inspector

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/boson/inspector.rb

Overview

Uses method decorators to scrape, process and hand off method attributes as data to Library objects.

Method Decorators

Method decorators refer to methods placed before a command's method in a library:

class SomeRunner < Boson::Runner
   options :verbose=>:boolean
   option :count, :numeric
   # Something descriptive perhaps
   def some_method(opts)
     # ...
   end
end

Method decorators serve as configuration for a method's command. All decorators should only be called once per method except for option. Available method decorators:

  • config: Hash to define any command attributes (see Command.new).

  • desc: String to define a command's description for a command. Defaults to first commented line above a method.

  • options: Hash to define an OptionParser object for a command's options.

  • option: Option name and value to be merged in with options. See OptionParser for what an option value can be.

Defined Under Namespace

Modules: API

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#add_data

Constructor Details

#initialize(library) ⇒ Inspector

Returns a new instance of Inspector



67
68
69
70
71
72
# File 'lib/boson/inspector.rb', line 67

def initialize(library)
  @commands_hash = library.commands_hash
  @library_file = library.library_file
  MethodInspector.instance.current_module = library.module
  @store = MethodInspector.instance.store
end

Class Attribute Details

.enabledObject (readonly)

Returns the value of attribute enabled



27
28
29
# File 'lib/boson/inspector.rb', line 27

def enabled
  @enabled
end

Class Method Details

.add_method_data_to_library(library) ⇒ Object

Adds method attributes to the library's commands



63
64
65
# File 'lib/boson/inspector.rb', line 63

def self.add_method_data_to_library(library)
  new(library).add_data
end

.disableObject

Disable scraping method data.



54
55
56
57
58
59
60
# File 'lib/boson/inspector.rb', line 54

def self.disable
  ::Module.module_eval %[
    Boson::MethodInspector::ALL_METHODS.each {|e| remove_method e }
    alias_method :method_added, :_old_method_added
  ]
  @enabled = false
end

.enable(options = {}) ⇒ Object

Enable scraping by overridding method_added to snoop on a library while it's loading its methods.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/boson/inspector.rb', line 31

def self.enable(options = {})
  method_inspector_meth = options[:all_classes] ?
    :new_method_added : :safe_new_method_added
  klass = options[:module] || ::Module
  @enabled = true unless options[:module]

  body = MethodInspector::ALL_METHODS.map {|e|
    %[def #{e}(*args)
        Boson::MethodInspector.#{e}(self, *args)
      end]
  }.join("\n") +
  %[
    def new_method_added(method)
      Boson::MethodInspector.#{method_inspector_meth}(self, method)
    end

    alias_method :_old_method_added, :method_added
    alias_method :method_added, :new_method_added
  ]
  klass.module_eval body
end