Class: QuickTravel::PriceChanges::PriceChangeTree

Inherits:
PriceChange
  • Object
show all
Defined in:
lib/quick_travel/price_changes/price_change_tree.rb

Direct Known Subclasses

BookingPriceChange

Instance Attribute Summary collapse

Attributes inherited from PriceChange

#changed_price, #original_price, #price_change, #reason, #reasons, #target

Instance Method Summary collapse

Methods inherited from PriceChange

#applied_on?

Constructor Details

#initialize(attrs = {}) ⇒ PriceChangeTree

Returns a new instance of PriceChangeTree.



6
7
8
9
10
11
12
# File 'lib/quick_travel/price_changes/price_change_tree.rb', line 6

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/quick_travel/price_changes/price_change_tree.rb', line 4

def children
  @children
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/quick_travel/price_changes/price_change_tree.rb', line 4

def root
  @root
end

Instance Method Details

#price_change_on(id, type = 'Reservation') ⇒ Object



14
15
16
17
# File 'lib/quick_travel/price_changes/price_change_tree.rb', line 14

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

#rootsObject



24
25
26
# File 'lib/quick_travel/price_changes/price_change_tree.rb', line 24

def roots
  [@root] + @children.flat_map(&:roots)
end

#total_price_change_on(id, type = 'Reservation') ⇒ Object



19
20
21
22
# File 'lib/quick_travel/price_changes/price_change_tree.rb', line 19

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