Module: Rootage::CollectionInterface
- Included in:
- ActionCollection, ArgumentCollection, ArgumentDefinition, OptionCollection, OptionDefinition, Sequence
- Defined in:
- lib/rootage/core.rb
Overview
CollectionInterface provides the way to create item colloction modules.
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
Instance Method Summary collapse
-
#define(item) {|item| ... } ⇒ Item
Define a item in the collection.
- #find_item(name) ⇒ Object
Instance Attribute Details
#table ⇒ Object (readonly)
Returns the value of attribute table.
149 150 151 |
# File 'lib/rootage/core.rb', line 149 def table @table end |
Class Method Details
.included(sub) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rootage/core.rb', line 151 def self.included(sub) sub.instance_variable_set(:@__item_class__, @__item_class__) sub.instance_eval do @__item_class def set_item_class(klass) @__item_class__ = klass end def self.item_class @__item_class__ || (raise NotImplemented) end end def sub.extended(_sub) _sub.instance_variable_set(:@__item_class__, @__item_class__) _sub.instance_variable_set(:@table, Hash.new) end def sub.inherited(_sub) super _sub.instance_variable_set(:@__item_class__, @__item_class__) end end |
Instance Method Details
#define(item) {|item| ... } ⇒ Item
Define a item in the collection. If name
is a symbol, new item is
defined. If name
is item object, use it.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/rootage/core.rb', line 185 def define(item, &block) # setup the item case item when Symbol _item = item_class.new _item.name = item when Item _item = item.copy else raise ArgumentError.new(item) end if block_given? block.call(_item) end # push it to item table if table.has_key?(_item.name) raise ArgumentError.new(_item.name) else table[_item.name] = _item end # define a getter method if this is a module if self.kind_of?(Module) singleton_class.send(:define_method, _item.name) {_item} end return _item end |
#find_item(name) ⇒ Object
216 217 218 |
# File 'lib/rootage/core.rb', line 216 def find_item(name) table[name] end |