Sha256: 90adeedceba8247b3015791c9077a3bd815b3ee4953a931f6473b6d576904c8c
Contents?: true
Size: 1.29 KB
Versions: 1
Compression:
Stored size: 1.29 KB
Contents
require 'haversack/itemcollection' class Sack < Haversack::ItemCollection attr_accessor :capacity attr_accessor :max_weight def initialize(data, capacity:, max_weight:, &block) @capacity = capacity @max_weight = max_weight block_given? ? super(data, &block) : super(data) raise Haversack::KnapsackCapacityExceededError if contents_exceed_capacity? raise Haversack::KnapsackWeightExceededError if contents_exceed_weight? end def available_weight empty? ? @max_weight : (@max_weight - weight) end def available_capacity empty? ? @capacity : (@capacity - size) end def fits_weight?(item) item.weight <= available_weight end def exceeds_weight?(item) !fits_weight?(item) end def fits_capacity?(item) item.size <= available_capacity end def exceeds_capacity?(item) !fits_capacity?(item) end def fits_item?(item) item.is_a?(Haversack::Item) && fits_capacity?(item) && fits_weight?(item) end alias :fits? :fits_item? def push(item) ## TODO: Describe which constraint failed fits_item?(item) ? super : raise(Haversack::KnapsackContentError) end private def contents_exceed_weight? weight > @max_weight end def contents_exceed_capacity? size > @capacity end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
haversack-1.0.0 | lib/haversack/sack.rb |