class ContributorsStats::Formatter
Takes care of formatting data and saving it
Attributes
content[R]
Public Class Methods
new(data, type = :html, options = {})
click to toggle source
# File lib/contributors_stats/formatter.rb, line 11 def initialize(data, type = :html, options = {}) format(data, type, options) end
Public Instance Methods
save(*targets)
click to toggle source
overwrite target with the formatted content @param targets [Array] list of files to overwrite
# File lib/contributors_stats/formatter.rb, line 17 def save(*targets) targets.flatten.each do |file| File.open(file, 'w') { |f| f.write(content * "\n") } end end
update(*targets, options: {})
click to toggle source
update target with the formatted content @param targets [Array] list of files to update @param options [Hash] options are passed to updater plugin, check
# File lib/contributors_stats/formatter.rb, line 26 def update(*targets, options: {}) targets.flatten.each do |file| plugin = plugins.first_ask!("updater", :handles?, file) update_file(file) do |file_content| plugin.update(file_content, content, options) end end end
Private Instance Methods
format(data, type = :html, options = {})
click to toggle source
# File lib/contributors_stats/formatter.rb, line 41 def format(data, type = :html, options = {}) formatter_plugin = plugins.first_class!("formatter", type).new(options) @content = data.map do |login, user_data| formatter_plugin.format(login, user_data) end end
plugins()
click to toggle source
# File lib/contributors_stats/formatter.rb, line 37 def plugins @plugins ||= Pluginator.find("contributors_stats", extends: %i{first_ask first_class}) end
update_file(file) { |text| ... }
click to toggle source
Allow editing file text in a block @example
update_file('some.txt'){|text| text.gsub(/bla/,'ble')}
# File lib/contributors_stats/formatter.rb, line 51 def update_file(file) text = File.read(file) text = yield text File.open(file, 'w') { |f| f.write(text) } end