Sha256: 90262b8bf42770cee57375c4579c164368b0c21253fa55f7721f8ae05258561c

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module Stockman
  module Logic
    class Kit
      class ContentsCollection
        include Enumerable

        attr_reader :kit_id

        def initialize(kit_id)
          @kit_id = kit_id.to_i
        end

        def [](index)
          contents[index]
        end

        def each
          contents.each{ |content| yield content }
        end

        def minimum_physical_amount(warehouse_id)
          minimum_amount(:physical, warehouse_id)
        end

        def minimum_available_amount(warehouse_id)
          minimum_amount(:available, warehouse_id)
        end

        def contents
          @contents ||= load_contents
        end

        private

        def load_contents
          Storage.obtain_hash("kits:#{kit_id}:variants").map do |variant_id, amount_in_kit|
            Content.new(kit_id, variant_id, amount_in_kit)
          end
        end

        def minimum_amount(type, warehouse_id)
          contents.map{|content| content.send(:"#{type}_amount", warehouse_id)}.min
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stockman-logic-0.1.1 lib/stockman/logic/kit/contents_collection.rb
stockman-logic-0.1.0 lib/stockman/logic/kit/contents_collection.rb