Module: Boson::Inspector

Extended by:
Inspector
Included in:
Inspector
Defined in:
lib/boson/inspector.rb

Overview

Scrapes and processes method attributes with the inspectors (MethodInspector, CommentInspector and ArgumentInspector) and hands off the data to FileLibrary objects.

Method Attributes

Method attributes refer to (commented) Module methods placed before a command’s method in a FileLibrary module:

module SomeMod
   # @render_options :fields=>%w{one two}
   # @config :alias=>'so'
   options :verbose=>:boolean
   option :count, :numeric
   # Something descriptive perhaps
   def some_method(opts)
     # ...
   end
end

Method attributes serve as configuration for a method’s command. All attributes should only be called once per

method except for option. Available method attributes:
  • 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.

  • render_options: Hash to define an OptionParser object for a command’s local/global render options (see View).

When deciding whether to use commented or normal Module methods, remember that commented Module methods allow independence from Boson (useful for testing). See CommentInspector for more about commented method attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



31
32
33
# File 'lib/boson/inspector.rb', line 31

def enabled
  @enabled
end

Instance Method Details

#add_comment_scraped_dataObject



109
110
111
112
113
114
115
116
117
# File 'lib/boson/inspector.rb', line 109

def add_comment_scraped_data
  (@store[:method_locations] || []).select {|k,(f,l)| f == @library_file }.each do |cmd, (file, lineno)|
    scraped = CommentInspector.scrape(FileLibrary.read_library_file(file), lineno, MethodInspector.current_module)
    @commands_hash[cmd] ||= {}
    MethodInspector::METHODS.each do |e|
      add_valid_data_to_config(e, scraped[e], cmd)
    end
  end
end

#add_method_data_to_library(library) ⇒ Object

Adds method attributes scraped for the library’s module to the library’s commands.



63
64
65
66
67
68
69
70
# File 'lib/boson/inspector.rb', line 63

def add_method_data_to_library(library)
  @commands_hash = library.commands_hash
  @library_file = library.library_file
  MethodInspector.current_module = library.module
  @store = MethodInspector.store
  add_method_scraped_data
  add_comment_scraped_data
end

#add_method_scraped_dataObject

:stopdoc:



73
74
75
76
77
78
79
80
# File 'lib/boson/inspector.rb', line 73

def add_method_scraped_data
  (MethodInspector::METHODS + [:args]).each do |key|
    (@store[key] || []).each do |cmd, val|
      @commands_hash[cmd] ||= {}
      add_valid_data_to_config(key, val, cmd)
    end
  end
end

#add_scraped_data_to_config(key, value, cmd) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/boson/inspector.rb', line 92

def add_scraped_data_to_config(key, value, cmd)
  if value.is_a?(Hash)
    if key == :config
      @commands_hash[cmd] = Util.recursive_hash_merge value, @commands_hash[cmd]
    else
      @commands_hash[cmd][key] = Util.recursive_hash_merge value, @commands_hash[cmd][key] || {}
    end
  else
    @commands_hash[cmd][key] ||= value
  end
end

#add_valid_data_to_config(key, value, cmd) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/boson/inspector.rb', line 82

def add_valid_data_to_config(key, value, cmd)
  if valid_attr_value?(key, value)
    add_scraped_data_to_config(key, value, cmd)
  else
    if Runner.debug
      warn "DEBUG: Command '#{cmd}' has #{key.inspect} attribute with invalid value '#{value.inspect}'"
    end
  end
end

#disableObject

Disable scraping method data.



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

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

#enableObject

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



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

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

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

#valid_attr_value?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/boson/inspector.rb', line 104

def valid_attr_value?(key, value)
  return true if (klass = MethodInspector::METHOD_CLASSES[key]).nil?
  value.is_a?(klass) || value.nil?
end