Class: Awestruct::Extensions::Gsub

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/extensions/gsub.rb

Instance Method Summary collapse

Constructor Details

#initialize(pattern, replacement, options = {}) ⇒ Gsub

Returns a new instance of Gsub.



5
6
7
8
9
# File 'lib/awestruct/extensions/gsub.rb', line 5

def initialize(pattern, replacement, options = {})
  @pattern = pattern
  @replacement = replacement.is_a?(Proc) ? replacement : lambda { |site, page| replacement }
  @gsub_required = lambdaize(options[:gsub_required])
end

Instance Method Details

#transform(site, page, rendered) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/awestruct/extensions/gsub.rb', line 11

def transform(site, page, rendered)
  if (@gsub_required.call(site, page))
    replacement = @replacement.call(site, page).to_s
    rendered = rendered.gsub(@pattern, replacement)
  end
  rendered
end