Commits

Kaspar Schiess committed b8a29f4

Proper caching (via output command)

Comments (0)

Files changed (3)

lib/iscsi/array_enum.rb

 require 'delegate'
 
 module ISCSI
+  # An object that behaves like an Enumerator and also like an Array. Construct
+  # using an array of seed objects and a factory block that turns seed objects
+  # into array contents. 
+  # 
+  #   a = ArrayEnum.new(%q(a b c)) { |e| e.succ }
+  #   a.to_a # => %w(b c d)
+  #
   class ArrayEnum < Delegator
     def initialize array, &cons
       # Behaves like an enumerator over array

lib/iscsi/context.rb

       def access id, &producer
         @map[id] || @map[id]=producer.call
       end
-      def ids
-        if @map.size > 0
-          @map.keys
-        else
-          nil
-        end
-      end
     end
     
     attr_reader :runner
         find { |t| t.name == name }
     end
     def targets
-      target_ids = cat(:target).ids || ArrayEnum.new(stmf_targets)
-      target_ids.each { |node| 
+      ArrayEnum.new(stmf_targets) { |node| 
         cat(:target).access(node.value) { Target.new(self, node) } }
     end
     def stmf_targets
-      parser = HashParser.new
-      parser.parse runner.shell('stmfadm list-target -v')
+      @stmf_targets ||= begin
+        parser = HashParser.new
+        parser.parse runner.shell('stmfadm list-target -v')
+      end
     end
 
     def target_groups

spec/lib/iscsi/array_enum_spec.rb

+require 'spec_helper'
+
+require 'iscsi'
+
+describe ISCSI::ArrayEnum do
+  def ae ary, &block
+    ISCSI::ArrayEnum.new(ary, &block)
+  end
+  
+  describe '#to_a' do
+    it "turns the enumeration into an array" do
+      a = ae(%w(a b c)) { |e| e.succ }
+      a.to_a.should == %w(b c d)
+    end 
+  end
+end