Class: Detroit::BasicTool
- 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
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Access to all options passed into ‘#initialize`.
Class Method Summary collapse
-
.assembly(a = nil) ⇒ Assembly
Call this method to register a tool with an assembly.
- .assembly=(a) ⇒ Object
-
.available? ⇒ Boolean
Override this method if the tool’s availability is conditional.
-
.new(options = {}) ⇒ BasicTool
Override the usual new method in order to apply prerequisites.
-
.options(tool_class = self) ⇒ Array<String>
Returns list of writer method names.
Instance Method Summary collapse
- #assemble(station, options = {}) ⇒ Object
-
#assemble?(station, options = {}) ⇒ Boolean
Does this tool attach to the specified station?.
-
#initialize(options = {}) ⇒ void
constructor
Create a new tool object.
-
#initialize_options(options) ⇒ void
Called by ‘#initialize` to call writers from given options.
-
#metadata ⇒ Object
Shortcut to project metadata.
-
#prerequisite ⇒ void
This pre-initialization procedure is run before #initialize and for all ancestors, so ‘#super` should never be called within it.
-
#project ⇒ Object
Project instance.
-
#project=(project) ⇒ Object
Project instance.
-
#root ⇒ Pathname
Project root directory.
- #title ⇒ Object
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 = ) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Access to all options passed into ‘#initialize`.
142 143 144 |
# File 'lib/detroit/basic_tool.rb', line 142 def @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).
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.
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.
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(={}) tool = allocate # TODO: I don't exactly like this, but how else to get project # into the tool befire running `#prerequiste`? tool.project = ['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, ) tool end |
.options(tool_class = self) ⇒ Array<String>
Returns list of writer method names. This is used for reference.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/detroit/basic_tool.rb', line 77 def self.(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, ={}) meth = method(station) case meth.arity when 0 meth.call() else meth.call() 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.
160 161 162 |
# File 'lib/detroit/basic_tool.rb', line 160 def assemble?(station, ={}) 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 () .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 |
#metadata ⇒ Object
Shortcut to project metadata.
187 188 189 |
# File 'lib/detroit/basic_tool.rb', line 187 def project. end |
#prerequisite ⇒ void
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.
110 111 |
# File 'lib/detroit/basic_tool.rb', line 110 def prerequisite end |
#project ⇒ Object
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 |
#root ⇒ Pathname
Project root directory.
194 195 196 |
# File 'lib/detroit/basic_tool.rb', line 194 def root @project.root end |
#title ⇒ Object
Is this needed?
147 148 149 |
# File 'lib/detroit/basic_tool.rb', line 147 def title self.class.name end |