Sha256: ff37d775d08685d955b6ed3f29067c74b257fe48895d2955900c0988ec551ba6

Contents?: true

Size: 1.84 KB

Versions: 96

Compression:

Stored size: 1.84 KB

Contents

module Souls
  module Api
    module Update
      class << self
        def type(class_name: "user")
          singularized_class_name = class_name.singularize.underscore
          new_cols = Souls.get_columns_num(class_name: singularized_class_name)
          dir_name = "./app/graphql/types"
          new_file_path = "tmp/create_type.rb"
          file_path = "#{dir_name}/#{singularized_class_name}_type.rb"
          argument = false
          File.open(file_path) do |f|
            File.open(new_file_path, "w") do |new_line|
              f.each_line do |line|
                new_line.write(line)
                next unless line.include?("field") && !argument

                new_cols.each do |col|
                  type = Souls::Api::Generate.get_type(col[:type])
                  type = "[#{type}]" if col[:array]
                  args = check_type_argument(class_name: class_name)
                  unless args.include?(col[:column_name])
                    new_line.write("    field :#{col[:column_name]}, #{type}, null: true\n")
                  end
                end
                argument = true
              end
            end
          end
          FileUtils.rm(file_path)
          FileUtils.mv(new_file_path, file_path)
          puts(Paint % ["Updated file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
        end

        def check_type_argument(class_name: "user")
          singularized_class_name = class_name.singularize.underscore
          dir_name = "./app/graphql/types"
          file_path = "#{dir_name}/#{singularized_class_name}_type.rb"
          args = []
          File.open(file_path) do |f|
            f.each_line do |line|
              args << line.split(",")[0].gsub("field :", "").strip if line.include?(" field :")
            end
          end
          args
        end
      end
    end
  end
end

Version data entries

96 entries across 96 versions & 1 rubygems

Version Path
souls-0.32.5 lib/souls/api/update/type.rb
souls-0.32.4 lib/souls/api/update/type.rb
souls-0.32.3 lib/souls/api/update/type.rb
souls-0.32.2 lib/souls/api/update/type.rb
souls-0.32.1 lib/souls/api/update/type.rb
souls-0.32.0 lib/souls/api/update/type.rb
souls-0.31.1 lib/souls/api/update/type.rb
souls-0.31.0 lib/souls/api/update/type.rb
souls-0.30.11 lib/souls/api/update/type.rb
souls-0.30.10 lib/souls/api/update/type.rb
souls-0.30.9 lib/souls/api/update/type.rb
souls-0.30.8 lib/souls/api/update/type.rb
souls-0.30.7 lib/souls/api/update/type.rb
souls-0.30.6 lib/souls/api/update/type.rb
souls-0.30.5 lib/souls/api/update/type.rb
souls-0.30.4 lib/souls/api/update/type.rb