Module: Optitron::ClassDsl::ClassMethods

Defined in:
lib/optitron/class_dsl.rb

Instance Method Summary collapse

Instance Method Details

#arg_types(*types) ⇒ Object



157
158
159
# File 'lib/optitron/class_dsl.rb', line 157

def arg_types(*types)
  @arg_types = types
end

#buildObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/optitron/class_dsl.rb', line 161

def build
  unless @built
    target = optitron_parser.target
    optitron_dsl.root.help unless send(:class_variable_defined?, :@@suppress_help)
    @cmds.each do |(cmd_name, cmd_desc, opts)|
      args = method_args[cmd_name.to_sym]
      arity = instance_method(cmd_name).arity
      arg_types = @arg_types
      optitron_dsl.root.cmd(cmd_name, cmd_desc) do
        opts.each { |o| opt *o }
        args.each do |(arg_name, arg_type, arg_default)|
          possible_arg_type = arg_name.to_s[/_(string|hash|array|numeric|int|float)$/, 1]
          if possible_arg_type && (arg_types.nil? || !arg_types.first)
            possible_arg_type = possible_arg_type.to_sym
            arg_name = arg_name.to_s[/^(.*)_(?:string|hash|array|numeric|int|float)$/, 1]
          end
          arg_opts = { :default => arg_default && target.instance_eval(arg_default), :type => arg_types && arg_types.shift || possible_arg_type }
          case arg_type
          when :required
            arg arg_name.to_s, arg_opts
          when :optional
            arg arg_name.to_s, arg_opts.merge(:required => false)
          when :greedy
            arg arg_name.to_s, arg_opts.merge(:type => :greedy)
          end
        end
      end
    end
    optitron_dsl.configure_options
    @built = true
  end
end

#build_method_args(file) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/optitron/class_dsl.rb', line 128

def build_method_args(file)
  unless send(:class_variable_defined?, :@@method_args)
    parser = RubyParser.new
    sexp = parser.process(File.read(file))
    method_args = MethodArgs.new(self)
    method_args.process(sexp)
    send(:class_variable_set, :@@method_args, method_args.method_map)
  end
  send(:class_variable_get, :@@method_args)
end

#class_opt(name, desc = nil, opts = nil) ⇒ Object



139
140
141
# File 'lib/optitron/class_dsl.rb', line 139

def class_opt(name, desc = nil, opts = nil)
  optitron_dsl.root.opt(name, desc, opts)
end

#desc(desc) ⇒ Object



147
148
149
150
# File 'lib/optitron/class_dsl.rb', line 147

def desc(desc)
  build_method_args(Callsite.parse(caller.first).filename)
  @last_desc = desc
end

#dispatch(args = ARGV, &blk) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/optitron/class_dsl.rb', line 194

def dispatch(args = ARGV, &blk)
  optitron_parser.target = blk ? blk.call : new
  build
  response = optitron_parser.parse(args)
  if response.valid?
    optitron_parser.target.params = response.params
    args = response.args
    while (args.size < optitron_parser.commands.assoc(response.command).last.args.size)
      args << optitron_parser.commandsassoc(response.command).last.args[args.size].default
    end
    optitron_parser.target.send(response.command.to_sym, *response.args)
  else
    puts response.error_messages.join("\n")
  end
end

#dont_use_helpObject



143
144
145
# File 'lib/optitron/class_dsl.rb', line 143

def dont_use_help
  send(:class_variable_set, :@@suppress_help, true)
end

#method_added(m) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/optitron/class_dsl.rb', line 104

def method_added(m)
  if @last_desc
    last_opts = @opts
    @cmds ||= []
    @cmds << [m.to_s, @last_desc, @opts ? @opts.dup : []]
    @opts.clear if @opts
    @args.clear if @args
    @last_desc = nil
  end
end

#method_argsObject



124
125
126
# File 'lib/optitron/class_dsl.rb', line 124

def method_args
  send(:class_variable_get, :@@method_args)
end

#opt(name, desc = nil, opts = nil) ⇒ Object



152
153
154
155
# File 'lib/optitron/class_dsl.rb', line 152

def opt(name, desc = nil, opts = nil)
  @opts ||= []
  @opts << [name, desc, opts]
end

#optitron_dslObject



119
120
121
122
# File 'lib/optitron/class_dsl.rb', line 119

def optitron_dsl
  send(:class_variable_set, :@@optitron_dsl, Optitron::Dsl.new(optitron_parser)) unless send(:class_variable_defined?, :@@optitron_dsl)
  send(:class_variable_get, :@@optitron_dsl)
end

#optitron_parserObject



114
115
116
117
# File 'lib/optitron/class_dsl.rb', line 114

def optitron_parser
  send(:class_variable_set, :@@optitron_parser, Optitron::Parser.new) unless send(:class_variable_defined?, :@@optitron_parser)
  send(:class_variable_get, :@@optitron_parser)
end