Method: Tap::Declarations#declare

Defined in:
lib/tap/declarations.rb

#declare(baseclass, const_name, configs = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tap/declarations.rb', line 74

def declare(baseclass, const_name, configs={}, &block)
  const_name = const_name.to_s.camelize
  subclass = Class.new(env.constant(baseclass))
  @namespace.const_set(const_name, subclass)
  
  # define configs
  configs.each_pair do |key, value|
    # specifying a desc prevents lazydoc registration of these lines
    opts = {:desc => ""}
    opts[:short] = key if key.to_s.length == 1
    config_block = Configurable::Validation.guess(value)
    subclass.send(:config, key, value, opts, &config_block)
  end
  
  # define process
  if block
    # determine arity, correcting for the self arg
    arity = block.arity
    arity -= arity > 0 ? 1 : -1
    signature = Array.new(arity < 0 ? arity.abs - 1 : arity, 'arg')
    signature << '*args' if arity < 0
    
    # prevents assessment of process args by lazydoc
    subclass.const_attrs[:process] = signature.join(' ')
    subclass.send(:define_method, :process) do |*args|
      block.call(self, *args)
    end
  end
  
  # register documentation
  constant = env.set(subclass, nil)
  
  if @desc
    subclass.desc = @desc
    constant.register_as(subclass.type, @desc)
    @desc = nil
  end
  
  subclass
end