Sha256: 023149dfb18b9619ac2212a22006f05ad3822b0e4ba62f54ec77b424f5245c1b

Contents?: true

Size: 942 Bytes

Versions: 4

Compression:

Stored size: 942 Bytes

Contents

module QuickTravel
  module Discounts
    class DiscountTree < Discount
      attr_reader :root, :children

      def initialize(attrs = {})
        super(attrs)
        @root     = Discount.new(attrs['root'])
        @children = attrs.fetch('children', []).map do |child_attrs|
          DiscountTree.new(child_attrs)
        end
      end

      def discount_on(id, type = 'Reservation')
        return @root if applied_on?(id, type)
        find_and_return_on_children { |child| child.discount_on(id, type) }
      end

      def total_discount_on(id, type = 'Reservation')
        return self if applied_on?(id, type)
        find_and_return_on_children { |child| child.total_discount_on(id, type) }
      end

      private

      def find_and_return_on_children(&block)
        @children.each do |child|
          result = block.call(child)
          return result unless result.nil?
        end
        nil
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
quicktravel_client-1.1.2 lib/quick_travel/discounts/discount_tree.rb
quicktravel_client-1.1.1 lib/quick_travel/discounts/discount_tree.rb
quicktravel_client-1.1.0 lib/quick_travel/discounts/discount_tree.rb
quicktravel_client-1.0.2 lib/quick_travel/discounts/discount_tree.rb