23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 
     | 
    
      # File 'lib/esse/cli/generate.rb', line 23
def index(name, *repos)
  ns_path = name.split(NAMESPACE_PATTERN_RE).tap(&:pop)
  @index_name = Hstring.new(name.to_s).modulize.sub(/Index$/, '') + 'Index'
  @index_name = Hstring.new(@index_name)
  @repos = repos.map { |repo| Hstring.new(repo) }
  @base_class = base_index_class(*ns_path)
  @index_cluster_id = options[:cluster_id]
  @cli_options = options
  base_dir = Esse.config.indices_directory.join(*ns_path.map { |n| Hstring.new(n).underscore.to_s })
  index_name = @index_name.demodulize.underscore.to_s
  template(
    'templates/index.rb.erb',
    base_dir.join("#{index_name}.rb"),
  )
  if options[:settings]
    copy_file(
      'templates/settings.json',
      base_dir.join(index_name, 'templates', 'settings.json'),
    )
  end
  if options[:mappings]
    copy_file(
      'templates/mappings.json',
      base_dir.join(index_name, 'templates', 'mappings.json'),
    )
  end
  if @repos.empty?
    if options[:documents]
      template(
        'templates/document.rb.erb',
        base_dir.join(index_name, 'documents', 'document.rb'),
      )
    end
    if options[:collections] && !options[:active_record]
      template(
        'templates/collection.rb.erb',
        base_dir.join(index_name, 'collections', 'collection.rb'),
      )
    end
  end
  @repos.each do |type|
    @repo = Hstring.new(type).underscore
    if options[:documents]
      template(
        'templates/document.rb.erb',
        base_dir.join(index_name, 'documents', "#{@repo}_document.rb"),
      )
    end
    if options[:collections] && !options[:active_record]
      template(
        'templates/collection.rb.erb',
        base_dir.join(index_name, 'collections', "#{@repo}_collection.rb"),
      )
    end
  end
end
     |