Class: Mumukit::Directives::Interpolations

Inherits:
Directive
  • Object
show all
Defined in:
lib/mumukit/directives/interpolations.rb

Instance Attribute Summary

Attributes inherited from Directive

#comment_type

Instance Method Summary collapse

Methods inherited from Directive

#comment_regexp

Constructor Details

#initialize(key = nil) ⇒ Interpolations

Returns a new instance of Interpolations.



2
3
4
# File 'lib/mumukit/directives/interpolations.rb', line 2

def initialize(key=nil)
  @key = key
end

Instance Method Details

#interpolate(code, sections) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mumukit/directives/interpolations.rb', line 14

def interpolate(code, sections)
  interpolated = []

  var = code.captures(comment_regexp) do
    substitution = sections[$1]
    if substitution
      interpolated << $1
      substitution
    else
      $&
    end
  end

  [var, interpolated.uniq]
end

#interpolations?(code) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/mumukit/directives/interpolations.rb', line 10

def interpolations?(code)
  (code =~ comment_regexp).present?
end

#regexpObject



6
7
8
# File 'lib/mumukit/directives/interpolations.rb', line 6

def regexp
  /\.\.\.(.+?)\.\.\./
end

#transform(sections) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/mumukit/directives/interpolations.rb', line 30

def transform(sections)
  raise 'Missing key. Build the interpolations with a key in order to use this method' unless @key
  code = sections[@key]
  if interpolations? code
    interpolation, interpolated = interpolate code, sections.except(@key)
    sections.merge(@key => interpolation).except(*interpolated)
  else
    sections
  end
end