Class: AtCoderFriends::Generator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/at_coder_friends/generator/base.rb

Overview

common behavior of generators

Direct Known Subclasses

CxxBuiltin, RubyBuiltin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg = nil) ⇒ Base

Returns a new instance of Base.



13
14
15
# File 'lib/at_coder_friends/generator/base.rb', line 13

def initialize(cfg = nil)
  @cfg = cfg || {}
end

Instance Attribute Details

#cfgObject (readonly)

Returns the value of attribute cfg.



11
12
13
# File 'lib/at_coder_friends/generator/base.rb', line 11

def cfg
  @cfg
end

#pbmObject (readonly)

Returns the value of attribute pbm.



11
12
13
# File 'lib/at_coder_friends/generator/base.rb', line 11

def pbm
  @pbm
end

Instance Method Details

#embed_lines(src, pat, lines) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/at_coder_friends/generator/base.rb', line 33

def embed_lines(src, pat, lines)
  re = Regexp.escape(pat)
  src.gsub(
    /^(.*)#{re}(.*)$\n/,
    lines.compact.map { |s| "\\1#{s}\\2\n" }.join
  )
end

#generate(pbm) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/at_coder_friends/generator/base.rb', line 21

def generate(pbm)
  @pbm = pbm
  src = File.read(select_template)
  src = ERB.new(src, safe_level = nil, trim_mode = '-').result(binding)
  src = render(src) if respond_to?(:render)
  src
end

#process(pbm) ⇒ Object



17
18
19
# File 'lib/at_coder_friends/generator/base.rb', line 17

def process(pbm)
  pbm.add_src(attrs.file_ext, generate(pbm))
end

#select_templateObject



29
30
31
# File 'lib/at_coder_friends/generator/base.rb', line 29

def select_template
  cfg['default_template'] || attrs.default_template
end