lib/esse/cli/generate.rb in esse-0.2.0 vs lib/esse/cli/generate.rb in esse-0.2.2

- old
+ new

@@ -12,36 +12,77 @@ def self.source_root File.dirname(__FILE__) end desc 'index NAME *TYPES', 'Creates a new index' + option :settings, type: :boolean, default: false, desc: 'Generate settings' + option :mappings, type: :boolean, default: false, desc: 'Generate mappings' + option :serializers, type: :boolean, default: false, desc: 'Generate serializers' + option :collections, type: :boolean, default: false, desc: 'Generate collections' + option :active_record, type: :boolean, default: false, desc: 'Generate ActiveRecord models' + option :cluster_id, type: :string, desc: 'Elasticsearch cluster ID' def index(name, *types) 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) @types = types.map { |type| Hstring.new(type) } @base_class = base_index_class(*ns_path) + if options[:cluster_id] + @base_class += format('(:%s)', options[:cluster_id]) + end + @cli_options = options - base_dir = Esse.config.indices_directory.join(*ns_path) + 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"), ) - @types.each do |type| - @type = Hstring.new(type).underscore + + if options[:settings] copy_file( - 'templates/type_mappings.json', - base_dir.join(index_name, 'templates', "#{@type}_mapping.json"), + 'templates/settings.json', + base_dir.join(index_name, 'templates', 'settings.json'), ) - template( - 'templates/type_serializer.rb.erb', - base_dir.join(index_name, 'serializers', "#{@type}_serializer.rb"), + end + + if options[:mappings] + copy_file( + 'templates/mappings.json', + base_dir.join(index_name, 'templates', 'mappings.json'), ) - template( - 'templates/type_collection.rb.erb', - base_dir.join(index_name, 'collections', "#{@type}_collection.rb"), - ) + end + + if @types.empty? + if options[:serializers] + template( + 'templates/serializer.rb.erb', + base_dir.join(index_name, 'serializers', 'serializer.rb'), + ) + end + if options[:collections] && !options[:active_record] + template( + 'templates/collection.rb.erb', + base_dir.join(index_name, 'collections', 'collection.rb'), + ) + end + end + + @types.each do |type| + @type = Hstring.new(type).underscore + + if options[:serializers] + template( + 'templates/serializer.rb.erb', + base_dir.join(index_name, 'serializers', "#{@type}_serializer.rb"), + ) + end + if options[:collections] && !options[:active_record] + template( + 'templates/collection.rb.erb', + base_dir.join(index_name, 'collections', "#{@type}_collection.rb"), + ) + end end end protected