Class: Sprout::Executable::ParameterFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/sprout/executable/parameter_factory.rb

Overview

A factory to create concrete Sprout::Executable::Param entities from a set of known types.

If an unrecognized Class reference is provided we will instantiate it and ensure that it responds to the public members of the Executable::Param interface.

This Factory gives you the ability to create new, custom parameter types by simply ensuring they are available to Ruby before your executable is interpreted.

Following is an example of a custom Parameter:

class CustomParam < Sprout::Executable::Param

  def to_shell
    "--foo-bar=#{value}"
  end
end

Following is an example Executable that can consume the above parameter:

require 'custom_param'

class Foo
  include Sprout::Executable

  add_param :name, CustomParam

end

That's it, there is no need to register your custom types with the Factory, just get it into your load path and require it.

Class Method Summary (collapse)

Class Method Details

+ (Object) create(type)

This factory allows us to use classes by reference in the Executable interface. Since there are already Ruby primitives for String and File and we don't want to clobber them, we use this factory to convert those to the appropriate types.



54
55
56
57
58
59
60
61
62
# File 'lib/sprout/executable/parameter_factory.rb', line 54

def create type
  # Didn't want to clobber the stdlib references
  # to these two important data types...
  # But wanted to keep the add_param interface
  # clean and simple.
  return StringParam.new if type == String
  return FileParam.new if type == File
  type.new
end