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.



196
197
198
199
200
201
202
# File 'lib/optix.rb', line 196

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



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

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

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



195
196
197
# File 'lib/optix.rb', line 195

def tree
  @tree
end

Instance Method Details

#cloak(&b) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/optix.rb', line 263

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



251
252
253
# File 'lib/optix.rb', line 251

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

#exec(&block) ⇒ Object



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

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

#filter(&block) ⇒ Object



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

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

#nodeObject



204
205
206
207
208
209
210
211
# File 'lib/optix.rb', line 204

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

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



222
223
224
225
226
227
228
229
230
# File 'lib/optix.rb', line 222

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



232
233
234
# File 'lib/optix.rb', line 232

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

#push_call(meth, args, block) ⇒ Object



213
214
215
216
# File 'lib/optix.rb', line 213

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

#text(text) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/optix.rb', line 255

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

#trigger(opts, &block) ⇒ Object



245
246
247
248
# File 'lib/optix.rb', line 245

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