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

AnyBuiltin, CBuiltin, RubyBuiltin

Constant Summary collapse

ACF_HOME =
File.realpath(File.join(__dir__, '..', '..', '..'))
TMPL_DIR =
File.join(ACF_HOME, 'templates')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg = nil) ⇒ Base

Returns a new instance of Base.



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

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

Instance Attribute Details

#cfgObject (readonly)

Returns the value of attribute cfg.



15
16
17
# File 'lib/at_coder_friends/generator/base.rb', line 15

def cfg
  @cfg
end

#pbmObject (readonly)

Returns the value of attribute pbm.



15
16
17
# File 'lib/at_coder_friends/generator/base.rb', line 15

def pbm
  @pbm
end

Instance Method Details

#config_file_extObject



37
38
39
# File 'lib/at_coder_friends/generator/base.rb', line 37

def config_file_ext
  cfg['file_ext']&.to_sym || attrs.file_ext
end

#config_fragmentsObject



46
47
48
49
# File 'lib/at_coder_friends/generator/base.rb', line 46

def config_fragments
  fragments = cfg['fragments'] || attrs.fragments
  fragments.sub(/\A@/, TMPL_DIR)
end

#config_templateObject



41
42
43
44
# File 'lib/at_coder_friends/generator/base.rb', line 41

def config_template
  template = cfg['template'] || cfg['default_template'] || attrs.template
  template.sub(/\A@/, TMPL_DIR)
end

#embed_lines(src, pat, lines) ⇒ Object

deprecated, use ERB syntax



52
53
54
55
56
57
58
# File 'lib/at_coder_friends/generator/base.rb', line 52

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

#fragmentsObject



33
34
35
# File 'lib/at_coder_friends/generator/base.rb', line 33

def fragments
  @fragments ||= YAML.load_file(config_fragments)
end

#generate(pbm) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/at_coder_friends/generator/base.rb', line 25

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

#process(pbm) ⇒ Object



21
22
23
# File 'lib/at_coder_friends/generator/base.rb', line 21

def process(pbm)
  pbm.add_src(config_file_ext, generate(pbm))
end