Class: Optix::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, config, cmd, &b) ⇒ Command

Returns a new instance of Command.



199
200
201
202
203
204
205
# File 'lib/optix.rb', line 199

def initialize(tree, config, cmd, &b)
  @tree = tree
  @config = config
  @cmd = cmd || ''
  node
  cloak(&b).bind(self).call
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



221
222
223
# File 'lib/optix.rb', line 221

def method_missing(meth, *args, &block)
  push_call(meth, args, block)
end

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



198
199
200
# File 'lib/optix.rb', line 198

def tree
  @tree
end

Instance Method Details

#cloak(&b) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/optix.rb', line 266

def cloak &b
  (class << self; self; end).class_eval do
    define_method :cloaker_, &b
    meth = instance_method :cloaker_
    remove_method :cloaker_
    meth
  end
end

#desc(text) ⇒ Object



254
255
256
# File 'lib/optix.rb', line 254

def desc(text)
  node[:description] = text
end

#exec(&block) ⇒ Object



239
240
241
# File 'lib/optix.rb', line 239

def exec(&block)
  node[:exec] = block
end

#filter(&block) ⇒ Object



243
244
245
246
# File 'lib/optix.rb', line 243

def filter(&block)
  node[:filters] ||= []
  node[:filters] << block
end

#nodeObject



207
208
209
210
211
212
213
214
# File 'lib/optix.rb', line 207

def node
  path = @cmd.split
  o = @tree
  path.each do |e|
    o = o[e] ||= {}
  end
  o
end

#opt(cmd, desc = '', args = {}) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/optix.rb', line 225

def opt(cmd, desc='', args={})
  if args.fetch(:default, false)
    args.delete :required
  end
  if args.fetch(:required, false)
    desc += @config[:text_required]
  end
  push_call(:opt, [cmd, desc, args], nil)
end

#params(text) ⇒ Object



235
236
237
# File 'lib/optix.rb', line 235

def params(text)
  node[:params] = text
end

#push_call(meth, args, block) ⇒ Object



216
217
218
219
# File 'lib/optix.rb', line 216

def push_call(meth, args, block)
  node[:calls] ||= []
  node[:calls] << [meth, args, block]
end

#text(text) ⇒ Object



258
259
260
261
262
263
264
# File 'lib/optix.rb', line 258

def text(text)
  node[:text] ||= ''
  if 0 < node[:text].length
    node[:text] += "\n"
  end
  node[:text] += text
end

#trigger(opts, &block) ⇒ Object



248
249
250
251
# File 'lib/optix.rb', line 248

def trigger(opts, &block)
  node[:triggers] ||= {}
  node[:triggers][opts] = block
end