Class: Caty::OptionConstructor

Inherits:
Object
  • Object
show all
Defined in:
lib/caty/option_constructor.rb

Overview

Constructs options using a DSL, e.g:

constructor.construct do
     desc('description')
     string  :option, 'default'

     desc('desc2')
     integer :nother_one
end

Used by Caty when Caty::global_options() is called.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_klass) ⇒ OptionConstructor

Creates a new OptionConstructor that constructs the given options of the given class.



46
47
48
49
# File 'lib/caty/option_constructor.rb', line 46

def initialize( option_klass )
    @klass     = option_klass
    @options   = Caty::OptionArray.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

Emulates the type methods, e.g:

string :name, 'default'


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/caty/option_constructor.rb', line 67

def method_missing( meth, *args, &block )
    if self.class.types.include?(meth) and args.length.between?(1,2)
        name    = args[0]
        default = args.length == 2 ?
            args[1] : meth
        option = @klass.new(name, default)
        option.description, @description =
            @description, nil
        @options << option
    else
        super
    end
end

Class Method Details

.register(type) ⇒ Object

Registers an option type (symbol) with the constructor.



35
36
37
38
# File 'lib/caty/option_constructor.rb', line 35

def register( type )
    @types ||= Array.new
    @types <<  type
end

.typesObject

Returns an array of all the option types registered.



27
28
29
# File 'lib/caty/option_constructor.rb', line 27

def types
    @types ||= Array.new
end

Instance Method Details

#construct(&block) ⇒ Object

Constructs the actual options according to the given block.



55
56
57
58
# File 'lib/caty/option_constructor.rb', line 55

def construct( &block )
    self.instance_eval(&block)
    @options
end