Skip to content

Instantly share code, notes, and snippets.

@wesmaldonado
Created January 28, 2010 21:50
Show Gist options
  • Save wesmaldonado/289178 to your computer and use it in GitHub Desktop.
Save wesmaldonado/289178 to your computer and use it in GitHub Desktop.
require 'yaml'
ads = YAML.load_file 'config/page_ads.yml'
class PageAds
attr :anchors
attr :positions
class Anchor
attr :name
attr :positions
def self.from_yaml_line(line)
#"anchor : &ad_set_30 [\"rich_media\", \"top_rail\", \"right_rail\"]",
line =~ /:\W+&(\w+)\W(.*)$/
pos = self.instance_eval $2
Anchor.new($1, pos)
end
def initialize(name, positions)
@name = name
@positions = positions
end
def inspect
%Q{<#{self.class.to_s.inspect} name=#{@name.inspect} positions=#{@positions.inspect}>}
end
end
class Position
attr :name
attr :positions
attr :anchor_name
def initialize(name, anchor_reference, optional)
@name = name
@anchor_name = anchor_reference.strip! if anchor_reference
@positions = []
@optional = optional
end
def optional?
@optional
end
def to_s
inspect
end
def inspect
"<#{self.class.to_s}> name=#{@name.inspect} positions=#{@positions.inspect} anchor_ref=#{@anchor_name.inspect} optional=#{@optional.inspect}>"
end
end
def initialize(io)
@io = io
@positions = []
@anchors = []
crack_that_whip
@positions.reject!{|p| p == ""}
end
def crack_that_whip
position_definition = ""
@io.readlines.each do |line|
case line
when /^anchor/ then
@anchors << Anchor.from_yaml_line(line.chomp)
#puts "ANCHOR: #{line}"
when /^[A-Z0-9_]+:/ then
if position_definition.size == 3
if position_definition.detect{|e| e.nil?}
puts "ERROR NIL POS: #{position_definition.inspect}"
next
end
anchor_ref = position_definition.detect{|i| i.include?("position")}.split(":")[1]
#puts "ERROR ANCH POS: #{position_definition.inspect}" unless anchor_ref
optional = position_definition.detect{|i| i.include?("optional")}.split(":")[1]
@positions << Position.new(position_definition[0].strip.chop, anchor_ref, optional)
puts "ERROR OPTS POS: #{position_definition.inspect}" unless optional
#puts position_definition.inspect
end
position_definition = []
position_definition << line.chomp
when /^\s{2}/ then
position_definition << line.chomp
else
#puts "OTHER: #{line}"
end
end
end
end
pa = PageAds.new(File.new(ARGV[0],'r'))
target_for_change = pa.positions.detect{|p| p.name == ARGV[1] }
shared_anchor = pa.anchors.detect{|a| target_for_change.anchor_name.include?(a.name) }
also_affected = pa.positions.select{|p| p.anchor_name == target_for_change.anchor_name}
puts "Target for change is:"
puts "\t\t#{target_for_change.inspect}"
puts "Changing it affects:"
puts also_affected.map{ |p| "\t#{p.inspect}\n" }.join("")
puts "The shared anchor has the following positions:\n\t#{shared_anchor.inspect}\n"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment