Class: Detroit::BasicTool

Inherits:
Object show all
Includes:
BasicUtils
Defined in:
lib/detroit/basic_tool.rb

Overview

This base class can be used for tools that do not need all of the utility methods provided by the regular Tool class.

Direct Known Subclasses

Tool

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BasicUtils

#apply_naming_policy, #config, #naming_policy, #root_pattern

Constructor Details

#initialize(options = {}) ⇒ void

Create a new tool object.

This sets up utility extensions and assigns options to setter attributes if they exist and values are not nil. That last point is important. You must use ‘false’ to purposely negate an option, as nil will instead allow any default setting to be used.



121
122
123
# File 'lib/detroit/basic_tool.rb', line 121

def initialize(options={})
  initialize_options(@options = options)
end

Instance Attribute Details

#optionsHash (readonly)

Access to all options passed into ‘#initialize`.

Returns:



142
143
144
# File 'lib/detroit/basic_tool.rb', line 142

def options
  @options
end

Class Method Details

.assembly(a = nil) ⇒ Assembly

Call this method to register a tool with an assembly. A tool can only belong to one assembly, but migration adapters can be defined to allow tools from one assembly to work with another (COMING SOON).

Examples:

class SomeTool < Tool
  assembly Standard

Parameters:

  • a (Assembly) (defaults to: nil)

    (optional) Assembly for which this tool is designed.

Returns:



24
25
26
27
28
# File 'lib/detroit/basic_tool.rb', line 24

def self.assembly(a=nil)
  #include(@assembly = a) if a
  include(a) if a
  @assembly
end

.assembly=(a) ⇒ Object



31
32
33
# File 'lib/detroit/basic_tool.rb', line 31

def self.assembly=(a)
  @assembly = a
end

.available?Boolean

Override this method if the tool’s availability is conditional.

Returns:

  • (Boolean)


93
94
95
# File 'lib/detroit/basic_tool.rb', line 93

def self.available?
  true
end

.new(options = {}) ⇒ BasicTool

Override the usual new method in order to apply prerequisites.

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/detroit/basic_tool.rb', line 58

def self.new(options={})
  tool = allocate
  # TODO: I don't exactly like this, but how else to get project
  #        into the tool befire running `#prerequiste`?
  tool.project = options['project']
  ancestors.reverse_each do |anc|
    next if (anc == BasicObject || anc == Object || anc == Kernel)
    if anc.instance_methods.include?(:prerequisite)
      pre = anc.instance_method(:prerequisite)
      pre.bind(tool).call
    end
  end
  tool.send(:initialize, options)
  tool
end

.options(tool_class = self) ⇒ Array<String>

Returns list of writer method names. This is used for reference.

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/detroit/basic_tool.rb', line 77

def self.options(tool_class=self)
  i = tool_class.ancestors.index(Tool)
  m = []
  tool_class.ancestors[0..i].each do |sc|
    sc.public_instance_methods(false).each do |pm|
      next if pm !~ /\w+=$/
      next if %w{taguri=}.include?(m.to_s)
      m << pm.to_s.chomp('=')
    end
  end
  m
end

Instance Method Details

#assemble(station, options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/detroit/basic_tool.rb', line 165

def assemble(station, options={})
  meth = method(station)

  case meth.arity
  when 0
    meth.call()
  else
    meth.call(options)
  end
end

#assemble?(station, options = {}) ⇒ Boolean

Does this tool attach to the specified station?

By default this checks for the definition of a public method in the tool class with the same name as the station. Note, it does not use ‘respond_to?` to do this, which would find any such method in the class hierarchy. Instead it specifically checks for a definition in the tool class itself. This helps prevent potential accidental name clashes between support methods and station names.

Returns:

  • (Boolean)


160
161
162
# File 'lib/detroit/basic_tool.rb', line 160

def assemble?(station, options={})
  self.class.public_methods(false).include?(station.to_sym)
end

#initialize_options(options) ⇒ void

This method returns an undefined value.

Called by ‘#initialize` to call writers from given options.



128
129
130
131
132
133
134
135
136
137
# File 'lib/detroit/basic_tool.rb', line 128

def initialize_options(options)
  options.each do |k, v|
    #send("#{k}=", v) unless v.nil? #if respond_to?("#{k}=") && !v.nil?
    if respond_to?("#{k}=")
      send("#{k}=", v) unless v.nil? #if respond_to?("#{k}=") && !v.nil?
    else
      warn "#{self.class.name} does not respond to `#{k}=`."
    end
  end
end

#metadataObject

Shortcut to project metadata.



187
188
189
# File 'lib/detroit/basic_tool.rb', line 187

def 
  project.
end

#prerequisitevoid

This method returns an undefined value.

This pre-initialization procedure is run before #initialize and for all ancestors, so ‘#super` should never be called within it. The method is intended to be used to require dependencies for a tool, so that tool’s dependencies are only required when needed. But it can also be used to set pre-option attribute defaults.

Examples:

def prerequisite
  require 'ostruct'
  @gravy = true
end


110
111
# File 'lib/detroit/basic_tool.rb', line 110

def prerequisite
end

#projectObject

Project instance.



182
183
184
# File 'lib/detroit/basic_tool.rb', line 182

def project
  @project #||= Project.factory(root)
end

#project=(project) ⇒ Object

Project instance.



177
178
179
# File 'lib/detroit/basic_tool.rb', line 177

def project=(project)
  @project = project
end

#rootPathname

Project root directory.

Returns:

  • (Pathname)


194
195
196
# File 'lib/detroit/basic_tool.rb', line 194

def root
  @project.root
end

#titleObject

TODO:

Is this needed?



147
148
149
# File 'lib/detroit/basic_tool.rb', line 147

def title
  self.class.name
end