Sha256: d23233c50963d1077cd94ceacff500190c7dc5493c2866a0aabe8c66b7d23ea0

Contents?: true

Size: 1.42 KB

Versions: 17

Compression:

Stored size: 1.42 KB

Contents

module Ms
  module Data
    module_function
    
    # Initializes a new simple data array.
    def new_simple(unresolved_data)
      Simple.new(unresolved_data)
    end
    
    # A Simple data array that lazily evaluates unresolved_data, and
    # each member of unresolved_data using to_a:
    # 
    #   class LazyObject
    #     attr_reader :to_a
    #     def initialize(array)
    #       @to_a = array
    #     end
    #   end
    #
    #   a = LazyObject.new([1,2,3])
    #   b = LazyObject.new([4,5,6])
    #   s = Ms::Data::Simple.new([a, b])
    #
    #   s.unresolved_data     # => [a, b]
    #   s.data                # => []
    #   s[0]                  # => [1,2,3]
    #   s[1]                  # => [4,5,6]
    #   s.data                # => [[1,2,3], [4,5,6]]
    #
    class Simple
      # The underlying resolved data store.
      attr_reader :data
      
      # The underlying unresolved data store.
      attr_reader :unresolved_data
      
      def initialize(unresolved_data)
        @data = []
        @unresolved_data = unresolved_data
      end
      
      def [](index)
        @data[index] ||= @unresolved_data.to_a[index].to_a 
      end
      
      def resolve
        0.upto(@unresolved_data.length - 1) do |index| 
          self[index]
        end unless resolved?
        
        self
      end
      
      def resolved?
        @data.compact.length == @unresolved_data.length
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
ms-core-0.0.19 lib/ms/data/simple.rb
ms-core-0.0.18 lib/ms/data/simple.rb
ms-core-0.0.17 lib/ms/data/simple.rb
ms-core-0.0.16 lib/ms/data/simple.rb
ms-core-0.0.14 lib/ms/data/simple.rb
ms-core-0.0.13 lib/ms/data/simple.rb
ms-core-0.0.12 lib/ms/data/simple.rb
ms-core-0.0.11 lib/ms/data/simple.rb
ms-core-0.0.10 lib/ms/data/simple.rb
ms-core-0.0.9 lib/ms/data/simple.rb
ms-core-0.0.7 lib/ms/data/simple.rb
ms-core-0.0.6 lib/ms/data/simple.rb
ms-core-0.0.5 lib/ms/data/simple.rb
ms-core-0.0.4 lib/ms/data/simple.rb
ms-core-0.0.2 lib/ms/data/simple.rb
ms-core-0.0.1 lib/ms/data/simple.rb
mspire-0.5.0 lib/ms/data/simple.rb