Created
January 24, 2019 18:57
-
-
Save jbodah/9ab329d62d9734a786b20a429879f119 to your computer and use it in GitHub Desktop.
trees
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Processes nodes in depth-first order | |
class DepthFirstTraversal | |
def initialize(root, hierarchy = nil) | |
@root = root | |
@hierarchy = hierarchy | |
end | |
def traverse | |
stack = [@root] | |
order = [] | |
until stack.empty? | |
el = stack.shift | |
order << el | |
stack = @hierarchy.children_for(el) + stack | |
end | |
order.each { |el| yield el } | |
end | |
end | |
# Processes each leaf node first working its way up the tree | |
# class ChildrenFirstTraversal | |
# def initialize(root, hierarchy = nil) | |
# @root = root | |
# @hierarchy = hierarchy | |
# end | |
# def traverse | |
# queue = [@root] | |
# order = [] | |
# until queue.empty? | |
# el = queue.shift | |
# queue += @hierarchy.children_for(el) | |
# order = [el] + order | |
# end | |
# order.each { |el| yield el } | |
# end | |
# end | |
module AdGroupAdTextVariationMapHierachy | |
def self.children_for(obj) | |
case obj | |
when AdGroupAdTextVariationMap | |
[obj.ad_text_variation] | |
when AdTextVariation | |
children = obj.ad_headline_variations | |
children += obj.ad_description_variations | |
children += obj.ad_display_url_patterns | |
children += obj.raw_ad_text_var_image_key_values | |
children += obj.raw_ad_text_variation_locales | |
else | |
[] | |
end | |
end | |
end | |
module AccountHierarchy | |
def self.children_for(obj) | |
case obj | |
when Account | |
obj.campaigns.eligible_active_or_paused | |
when Campaign | |
obj.ad_groups.eligible_active_or_paused | |
when AdGroup | |
obj.ad_group_variation.status.in?(ACCEPTABLE_STATUSES) ? [obj.ad_group_variation] : [] | |
else | |
[] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment