Class: ContributorsStats::Updater::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/contributors_stats/updater/html.rb

Overview

Update a target file with content

Constant Summary collapse

DEFAULT_SEARCH =

Default search pattern

/<span class="contributors">.*?<\/span>/m
DEFAULT_REPLACE =

Default replace template

%q{%Q{<span class="contributors">\n#{replace_content.join("\n")}\n</span>}}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_content, replace_content, options = {}) ⇒ Html

set initial parameters, set default options



24
25
26
27
28
29
# File 'lib/plugins/contributors_stats/updater/html.rb', line 24

def initialize(file_content, replace_content, options = {})
  @search  = options[:search]  || DEFAULT_SEARCH
  @replace = options[:replace] || DEFAULT_REPLACE
  @file_content    = file_content
  @replace_content = replace_content
end

Instance Attribute Details

#file_contentObject (readonly)

Returns the value of attribute file_content.



11
12
13
# File 'lib/plugins/contributors_stats/updater/html.rb', line 11

def file_content
  @file_content
end

#replaceObject

Returns the value of attribute replace.



10
11
12
# File 'lib/plugins/contributors_stats/updater/html.rb', line 10

def replace
  @replace
end

#replace_contentObject (readonly)

Returns the value of attribute replace_content.



11
12
13
# File 'lib/plugins/contributors_stats/updater/html.rb', line 11

def replace_content
  @replace_content
end

#searchObject

Returns the value of attribute search.



10
11
12
# File 'lib/plugins/contributors_stats/updater/html.rb', line 10

def search
  @search
end

Class Method Details

.handles?(file) ⇒ Boolean

check if given file is supported by this updater

Returns:

  • (Boolean)


14
15
16
# File 'lib/plugins/contributors_stats/updater/html.rb', line 14

def self.handles?(file)
  %w{ .html .html.erb }.any?{|ext| file.end_with?(ext) }
end

.update(file_content, replace_content, options = {}) ⇒ Object

perform the content update



19
20
21
# File 'lib/plugins/contributors_stats/updater/html.rb', line 19

def self.update(file_content, replace_content, options = {})
  new(file_content, replace_content, options).to_s
end

Instance Method Details

#to_sObject

perform the replace operation



32
33
34
# File 'lib/plugins/contributors_stats/updater/html.rb', line 32

def to_s
  file_content.sub( search, eval(replace) )
end