class PMLCode::Updater
Public Class Methods
find(criteria)
click to toggle source
# File lib/pmlcode/updater.rb, line 31 def find(criteria) load_plugins! plugins.find do |plugin| plugin.handles?(criteria) end end
handle_check()
click to toggle source
# File lib/pmlcode/updater.rb, line 19 def handle_check @handle_check ||= ->(x) { false } end
handles(&check)
click to toggle source
# File lib/pmlcode/updater.rb, line 23 def handles(&check) @handle_check = check end
handles?(criteria)
click to toggle source
# File lib/pmlcode/updater.rb, line 27 def handles?(criteria) handle_check.(criteria) end
inherited(plugin)
click to toggle source
# File lib/pmlcode/updater.rb, line 5 def inherited(plugin) plugins << plugin end
load_plugins!()
click to toggle source
# File lib/pmlcode/updater.rb, line 13 def load_plugins! Dir.glob(File.expand_path("../updaters/*.rb", __FILE__)) do |filename| require filename end end
new(options)
click to toggle source
# File lib/pmlcode/updater.rb, line 44 def initialize(options) @source = options.source @options = options @current_prefix = nil @wrote = {} @files = {} end
plugins()
click to toggle source
# File lib/pmlcode/updater.rb, line 9 def plugins @plugins ||= [] end
run(options)
click to toggle source
# File lib/pmlcode/updater.rb, line 38 def run(options) new(options).run end
Public Instance Methods
check_part!(text, part)
click to toggle source
# File lib/pmlcode/updater.rb, line 95 def check_part!(text, part) content = PMLCode::Content.parse(text) if part if content.has_part?(part) print Rainbow("OK").green else print Rainbow("MISSING").red end else print Rainbow("--").gray end puts " : PART #{part}" puts "\n" if @options.content puts PMLCode::Display.new(content, part, @options) end end
dedup(match, &block)
click to toggle source
# File lib/pmlcode/updater.rb, line 87 def dedup(match, &block) update_id = generate_update_id(match) content_id = generate_content_id(match) @files[content_id] ||= block.(@wrote[update_id]) @wrote[update_id] = true @files[content_id] end
directory(match)
click to toggle source
# File lib/pmlcode/updater.rb, line 129 def directory(match) File.expand_path(File.join(@options.output, match[:coderoot], match[:chapter], match[:snapshot])) end
embeds()
click to toggle source
# File lib/pmlcode/updater.rb, line 52 def embeds @embeds ||= begin doc = Nokogiri::XML(File.read(@source.path)) doc.css('embed').select do |embed| if @source.line embed.line == @source.line else true end end end end
generate_content_id(match)
click to toggle source
# File lib/pmlcode/updater.rb, line 121 def generate_content_id(match) match.string end
generate_update_id(match)
click to toggle source
# File lib/pmlcode/updater.rb, line 125 def generate_update_id(match) raise NotImplemented, "Override #{self.class}#generate_update_id" end
run()
click to toggle source
# File lib/pmlcode/updater.rb, line 65 def run embeds.each do |embed| puts Rainbow(File.basename(@source.path) + ":#{embed.line} ").bold.underline match = @options.pattern.match(embed[:file]) if match text = dedup(match) { |already_wrote| update(match, already_wrote) } if text print Rainbow("OK").green puts " : FILE #{embed[:file]} #{write_flag}" check_part!(text, embed[:part]) else print Rainbow("ERROR").red puts " : FILE #{embed[:file]}" end else print Rainbow("BAD MATCH").red puts " : FILE #{embed[:file]}" end puts end end
write_flag()
click to toggle source
# File lib/pmlcode/updater.rb, line 113 def write_flag if @options.dry_run Rainbow(" DRY RUN ").green.inverse else Rainbow(" WRITTEN ").yellow.inverse end end