Class: Tardvig::Toolkit

Inherits:
Object
  • Object
show all
Defined in:
lib/tardvig/toolkit.rb

Overview

Container which initializes and gives you access to its tools (objects). Useful when you need to do a DSL

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Toolkit

Creates a new toolbox instance and initializes its tools.

Parameters:

  • params (Hash) (defaults to: {})

    you can set some options (see below) via this hash. It also will be available as the second argument passed to the ‘call` methods of your tools.

Options Hash (params):

  • :initialize_tools (boolean) — default: true

    initialize tools or not? If not, you will not able to use them, but you can initialize them later (see #create_tools).



31
32
33
34
35
# File 'lib/tardvig/toolkit.rb', line 31

def initialize(params = {})
  default_params = { initialize_tools: true }
  @params = default_params.merge params
  create_tools if @params[:initialize_tools]
end

Class Method Details

.tool(name, tool_itself = nil) ⇒ Object

Adds a new tool (object).

If the given object has either ‘call` or `new` methods then result of the method execution will be used as a tool instead. When the `call` method of your tool is executed, the toolkit will be given as the first argument and the `params` (see #initialize) as the second.

Parameters:

  • name (Symbol)

    you can access your tool via this method name

  • tool_itself (#call, #new, Object) (defaults to: nil)

    your tool.



19
20
21
# File 'lib/tardvig/toolkit.rb', line 19

def tool(name, tool_itself = nil)
  tools[name] = tool_itself || Proc.new
end

.toolsHash

Returns your tools.

Returns:

  • (Hash)

    your tools



7
8
9
# File 'lib/tardvig/toolkit.rb', line 7

def tools
  @tools ||= {}
end

Instance Method Details

#create_toolsObject

You can create tools to use them through this method if you have not done this on toolkit initialize.



39
40
41
# File 'lib/tardvig/toolkit.rb', line 39

def create_tools
  create_tools_readers
end