Class: FolderTemplate::TemplateString

Inherits:
Object
  • Object
show all
Defined in:
lib/folder_template/template_string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ TemplateString

Returns a new instance of TemplateString.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/folder_template/template_string.rb', line 14

def initialize( template )
  case template
  when String
    @content = _parse( template )
  when Array
    @content = template.select do |fragment|
      !fragment.nil? && !fragment.to_s.empty?
    end
  when TemplateString
    @content = template.content
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/folder_template/template_string.rb', line 12

def content
  @content
end

Instance Method Details

#expand(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/folder_template/template_string.rb', line 31

def expand( env )
  fragments = content.map do |fragment|
    case fragment
    when Symbol
      env[fragment] || (yield fragment if block_given?)|| fragment
    else
      fragment
    end
  end.select { |fragment| !fragment.nil? && !fragment.to_s.empty? }
  TemplateString.new( fragments )
end

#to_sObject



43
44
45
46
47
# File 'lib/folder_template/template_string.rb', line 43

def to_s
  @content.map do |fragment|
    ("{{#{fragment}}}" if Symbol === fragment) || fragment
  end.join
end

#variablesObject



27
28
29
# File 'lib/folder_template/template_string.rb', line 27

def variables
  @variables ||= _extract_variables( @content )
end