Class: PMLCode::Updater
- Inherits:
-
Object
show all
- Defined in:
- lib/pmlcode/updater.rb
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
50
|
# File 'lib/pmlcode/updater.rb', line 44
def initialize(options)
@source = options.source
@options = options
@current_prefix = nil
@wrote = {}
@files = {}
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_check ⇒ Object
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
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
|
.plugins ⇒ Object
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
#check_part!(text, part) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# 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) ⇒ Object
87
88
89
90
91
92
93
|
# 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) ⇒ Object
129
130
131
|
# 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 ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
|
# 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) ⇒ Object
121
122
123
|
# File 'lib/pmlcode/updater.rb', line 121
def generate_content_id(match)
match.string
end
|
#generate_update_id(match) ⇒ Object
125
126
127
|
# File 'lib/pmlcode/updater.rb', line 125
def generate_update_id(match)
raise NotImplemented, "Override #{self.class}#generate_update_id"
end
|
#run ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# 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 ⇒ Object
113
114
115
116
117
118
119
|
# 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
|