Class: PMLCode::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/pmlcode/updater.rb

Direct Known Subclasses

FullUpdater, SparseUpdater

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Updater

Returns a new instance of Updater.



44
45
46
47
48
49
# File 'lib/pmlcode/updater.rb', line 44

def initialize(options)
  @pml = options.pml
  @options = options
  @current_prefix = nil
  @wrote = {}
end

Class Method Details

.find(criteria) ⇒ Object



31
32
33
34
35
36
# File 'lib/pmlcode/updater.rb', line 31

def find(criteria)
  load_plugins!
  plugins.find do |plugin|
    plugin.handles?(criteria)
  end
end

.handle_checkObject



19
20
21
# File 'lib/pmlcode/updater.rb', line 19

def handle_check
  @handle_check ||= ->(x) { false }
end

.handles(&check) ⇒ Object



23
24
25
# File 'lib/pmlcode/updater.rb', line 23

def handles(&check)
  @handle_check = check
end

.handles?(criteria) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/pmlcode/updater.rb', line 27

def handles?(criteria)
  handle_check.(criteria)
end

.inherited(plugin) ⇒ Object



5
6
7
# File 'lib/pmlcode/updater.rb', line 5

def inherited(plugin)
  plugins << plugin
end

.load_plugins!Object



13
14
15
16
17
# File 'lib/pmlcode/updater.rb', line 13

def load_plugins!
  Dir.glob(File.expand_path("../updaters/*.rb", __FILE__)) do |filename|
    require filename
  end
end

.pluginsObject



9
10
11
# File 'lib/pmlcode/updater.rb', line 9

def plugins
  @plugins ||= []
end

.run(options) ⇒ Object



38
39
40
# File 'lib/pmlcode/updater.rb', line 38

def run(options)
  new(options).run
end

Instance Method Details

#dedup(location, match, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pmlcode/updater.rb', line 74

def dedup(location, match, &block)
  id = generate_update_id(match)
  if @wrote[id]
    log location, "SKIP #{id} (WROTE by #{@wrote[id][0..-2]})"
  else
    if block.()
      @wrote[id] = location
      log location, "WROTE #{id}"
    else
      log location, "INVALID #{id}"
    end
  end
end

#directory(match) ⇒ Object



92
93
94
# File 'lib/pmlcode/updater.rb', line 92

def directory(match)
  File.expand_path(File.join(@options.output, match[:coderoot], match[:chapter], match[:snapshot]))
end

#embedsObject



51
52
53
54
55
56
# File 'lib/pmlcode/updater.rb', line 51

def embeds
  @embeds ||= begin
    doc = Nokogiri::XML(File.read(@pml))
    doc.css('embed')
  end
end

#generate_update_id(match) ⇒ Object

Raises:

  • (NotImplemented)


88
89
90
# File 'lib/pmlcode/updater.rb', line 88

def generate_update_id(match)
  raise NotImplemented, "Override #{self.class}#generate_update_id"
end

#log(location, text) ⇒ Object



58
59
60
# File 'lib/pmlcode/updater.rb', line 58

def log(location, text)
  $stderr.puts "#{location}#{text}"
end

#runObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pmlcode/updater.rb', line 62

def run
  embeds.each do |embed|
    location = File.basename(@pml) + ":#{embed.line}:"
    match = @options.pattern.match(embed[:file])
    if match
      dedup(location, match) { update(match) }
    else
      log location, "NOMATCH #{embed[:file]}"
    end
  end
end