Module: Pione::Command::CollectionInterface

Defined in:
lib/pione/command/common.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



128
129
130
# File 'lib/pione/command/common.rb', line 128

def items
  @items
end

Instance Method Details

#define(name, &b) ⇒ Object

Define a new item.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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_classObject

Raises:

  • (NotImplemented)


130
131
132
# File 'lib/pione/command/common.rb', line 130

def item_class
  raise NotImplemented
end

#use(item) {|item| ... } ⇒ Object

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

Parameters:

  • item (OptionItem)

    option item

Yields:

  • (item)

    the cloned item



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 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