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) ⇒ Interpolations

Returns a new instance of Interpolations.



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

def initialize(key)
  @key = key
end

Instance Method Details

#interpolate(code, sections) ⇒ Object



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

def interpolate(code, sections)
  interpolated = []

  var = code.captures(comment_regexp) do
    interpolated << $1
    sections[$1]
  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



25
26
27
28
29
30
31
32
33
# File 'lib/mumukit/directives/interpolations.rb', line 25

def transform(sections)
  code = sections[@key]
  if interpolations? code
    interpolation, interpolated = interpolate code, sections.except(@key)
    sections.merge(@key => interpolation).except(*interpolated)
  else
    sections
  end
end