Sha256: 38edf504946876cb9a4d240c0adceb4046c4962666b7953dc8416143cee5b181

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

# frozen_string_literal: true

require "speek/export/base"
require "speek/application"

module Speek
  module Export
    # for generate RBS code
    class Rbs < Base
      def export
        <<~RUBY
          class #{class_name}
            #{@app.schema_data.columns.map { |column| generate_attr_accessor(column) }.join("\n  ")}
          end
        RUBY
      end

      private

      def class_name = @app.schema_data.table_name.singularize.camelize

      def generate_attr_accessor(column_data)
        type = convert_type(column_data.type)
        type = "(#{type} | nil)" if column_data.nullable

        "attr_accessor #{column_data.name}: #{type}"
      end

      def convert_type(type)
        case type
        when :integer, :bigint
          "Integer"
        when :string, :text
          "String"
        when :datetime
          "Time"
        when :boolean
          "Boolean"
        else
          raise UnknownColumnTypeError, "Unknown column type: #{type}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
speek-0.1.1 lib/speek/export/rbs.rb
speek-0.1.0 lib/speek/export/rbs.rb