module Pione::Command::CollectionInterface

Attributes

items[R]

Public Instance Methods

define(name, &b) click to toggle source

Define a new item.

# File lib/pione/command/common.rb, line 135
def define(name, &b)
  item = item_class.new.tap do |item|
    item.name = name
    b.call(item)
  end

  @items ||= []
  @items << item

  # define a getter method if this is a module
  if self.kind_of?(Module)
    singleton_class.send(:define_method, name) {item}
  end
end
item_class() click to toggle source
# File lib/pione/command/common.rb, line 130
def item_class
  raise NotImplemented
end
use(item, &b) click to toggle source

Use the item as command option. If block is given, apply the block with cloned item and use it.

@param item [OptionItem]

option item

@yield [item]

the cloned item
# File lib/pione/command/common.rb, line 157
def use(item, &b)
  unless item.kind_of?(item_class)
    raise ArgumentError.new(item)
  end

  if block_given?
    _item = item.copy
    b.call(_item)
    @items << _item
  else
    @items << item
  end

  # define a getter method if this is a module
  if self.kind_of?(Module)
    singleton_class.send(:define_method, name) {item}
  end
end