Class: Ekylibre::Support::Lister

Inherits:
Object
  • Object
show all
Defined in:
lib/ekylibre/support/lister.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*types) ⇒ Lister

Returns a new instance of Lister.


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ekylibre/support/lister.rb', line 16

def initialize(*types)
  @list = []
  types.each do |type|
    raise 'Cannot use "list" as type name' if type.to_s == 'list'
    define_singleton_method type do |*args, &block|
      @list << Item.new(type.to_sym, args, block)
    end
    define_singleton_method type.to_s.pluralize do
      @list.select { |i| i.type == type }
    end
  end
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list


14
15
16
# File 'lib/ekylibre/support/lister.rb', line 14

def list
  @list
end

Instance Method Details

#detect_and_extract!(&block) ⇒ Object


29
30
31
32
33
# File 'lib/ekylibre/support/lister.rb', line 29

def detect_and_extract!(&block)
  index = @list.find_index(&block)
  return nil unless index
  @list.delete_at(index)
end