Class: Boson::MethodInspector

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

Overview

Gathers method attributes by redefining method_added and capturing method calls before a method.

Defined Under Namespace

Modules: API

Constant Summary collapse

METHODS =
[:config, :desc, :options]
SCRAPEABLE_METHODS =
[:options]
METHOD_CLASSES =
{:config=>Hash, :desc=>String, :options=>Hash}
ALL_METHODS =
METHODS + [:option]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#during_new_method_added, #has_inspector_method?, #set_arguments

Constructor Details

#initializeMethodInspector

Returns a new instance of MethodInspector.



32
33
34
# File 'lib/boson/method_inspector.rb', line 32

def initialize
  @mod_store = {}
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



19
20
21
# File 'lib/boson/method_inspector.rb', line 19

def instance
  @instance
end

Instance Attribute Details

#current_moduleObject

Returns the value of attribute current_module.



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

def current_module
  @current_module
end

#mod_storeObject

Returns the value of attribute mod_store.



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

def mod_store
  @mod_store
end

Class Method Details

.new_method_added(mod, meth) ⇒ Object



15
16
17
# File 'lib/boson/method_inspector.rb', line 15

def self.new_method_added(mod, meth)
  instance.new_method_added(mod, meth)
end

.safe_new_method_added(mod, meth) ⇒ Object



10
11
12
13
# File 'lib/boson/method_inspector.rb', line 10

def self.safe_new_method_added(mod, meth)
  return unless mod.to_s[/^Boson::Commands::/]
  new_method_added(mod, meth)
end

Instance Method Details

#new_method_added(mod, meth) ⇒ Object

The method_added used while scraping method attributes.



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

def new_method_added(mod, meth)
  self.current_module = mod

  store[:temp] ||= {}
  METHODS.each do |e|
    store[e][meth.to_s] = store[:temp][e] if store[:temp][e]
  end
  if store[:temp][:option]
    (store[:options][meth.to_s] ||= {}).merge! store[:temp][:option]
  end
  during_new_method_added mod, meth
  store[:temp] = {}

  if SCRAPEABLE_METHODS.any? {|m| has_inspector_method?(meth, m) }
    set_arguments(mod, meth)
  end
end

#option(mod, name, value) ⇒ Object

Scrapes option



63
64
65
66
67
# File 'lib/boson/method_inspector.rb', line 63

def option(mod, name, value)
  (@mod_store[mod] ||= {})[:options] ||= {}
  (store(mod)[:temp] ||= {})[:option] ||= {}
  (store(mod)[:temp] ||= {})[:option][name] = value
end

#rename_store_key(old, new) ⇒ Object

Renames store key from old to new name



76
77
78
# File 'lib/boson/method_inspector.rb', line 76

def rename_store_key(old, new)
  mod_store[new] = mod_store.delete old
end

#store(mod = @current_module) ⇒ Object

Hash of a module’s method attributes i.e. descriptions, options by method and then attribute



71
72
73
# File 'lib/boson/method_inspector.rb', line 71

def store(mod=@current_module)
  @mod_store[mod]
end