Class: GoonModelGen::Builder::AbstractBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/goon_model_gen/builder/abstract_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, base_package_path) ⇒ AbstractBuilder

Returns a new instance of AbstractBuilder.

Parameters:



15
16
17
18
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 15

def initialize(config, base_package_path)
  @config = config
  @base_package_path = base_package_path
end

Instance Attribute Details

#base_package_pathObject (readonly)

Returns the value of attribute base_package_path.



10
11
12
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 10

def base_package_path
  @base_package_path
end

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 9

def config
  @config
end

#package_suffixObject

Returns the value of attribute package_suffix.



11
12
13
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 11

def package_suffix
  @package_suffix
end

Instance Method Details

#build(context) ⇒ Golang::Packages

Parameters:

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 22

def build(context)
  Golang::Packages.new.tap do |r|
    build_sentences = []
    context.files.each do |f|
      package_path = File.join(base_package_path, f.basename + (package_suffix || ""))
      pkg, procs = build_package(package_path, f.types)
      r << pkg
      build_sentences.concat(procs)
    end
    resolve_type_names(r)
    build_sentences.each(&:call)
  end
end

#build_package(package_path, types) ⇒ Golang::Package, Array<Proc>

Parameters:

Returns:

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 39

def build_package(package_path, types)
  raise NotImplementedError, "#{self.type.name} doesn't implement build_package method"
end

#build_sentences(action, kind, t, go_type) ⇒ Object

Parameters:

  • action (string)

    directory name under templates directory. ex. model, store…

  • kind (string)

    directory name under the directory specified by action. ex. goon, struct, enum, slice

  • t (Source::Struct)
  • go_type (Golang::Type)


52
53
54
55
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 52

def build_sentences(action, kind, t, go_type)
  template_base = File.join(action, kind)
  build_sentences_with(template_base, go_type, t.generators[action])
end

#build_sentences_with(template_base, go_type, generators) ⇒ Object

Parameters:

  • template_base (string)

    template directory path from templates directory. ex. model/enum, store/goon…

  • generators (Hash<string,Object>)
  • go_type (Golang::Type)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 60

def build_sentences_with(template_base, go_type, generators)
  m2t = method_to_template_for(template_base)
  generators ||= m2t.keys.each_with_object({}){|name, d| d[name] = true }

  m2t.each do |name, template|
    suffix = generators[name]
    next if !suffix
    raise "No template found for #{name.inspect}" unless template
    parts = [go_type.name.underscore]
    custom_suffix = false
    if suffix.is_a?(String)
      parts << suffix
      custom_suffix = true
    end
    filename = parts.join('_') << '.go'
    go_type.package.find_or_new_file(filename).tap do |file|
      file.custom_suffix = custom_suffix
      file.new_sentence(File.join(template_base, template), go_type)
    end
  end
end

#method_to_template_for(template_base) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 86

def method_to_template_for(template_base)
  method_to_template_map[template_base] ||=
    begin
      templates_for(template_base).each_with_object({}) do |filename, d|
        m = filename.sub(/\A\d+\_/, '').sub(/\.go\.erb\z/, '')
        d[m] = filename
      end
    end
end

#method_to_template_mapObject



82
83
84
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 82

def method_to_template_map
  @method_to_template_map ||= {}
end

#resolve_type_names(pkgs) ⇒ Object

Parameters:

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 44

def resolve_type_names(pkgs)
  raise NotImplementedError, "#{self.type.name} doesn't implement resolve_type_names method"
end

#templates_for(template_base) ⇒ Object



96
97
98
99
100
101
# File 'lib/goon_model_gen/builder/abstract_builder.rb', line 96

def templates_for(template_base)
  base_dir = File.join(File.expand_path('../../templates', __FILE__), template_base)
  Dir.chdir(base_dir) do
    Dir.glob('*.go.erb')
  end
end