Sha256: 23d06146d836eb09cb2084be4e8f02cf8933e76282ab774ed0b2c18e84d05617

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

class ActiveRecordSchemaScrapper
  class Attributes

    class << self
      def register_type(name:, klass:)
        registered_types << [name, klass]
      end

      def registered_types
        @registered_types ||= []
      end

      def register_default(name:, klass:)
        registered_defaults << [name, klass]
      end

      def registered_defaults
        @registered_defaults ||= []
      end
    end

    attr_reader :errors, :model
    private :model

    def initialize(model:)
      @model  = model
      @errors = []
    end

    include Enumerable

    def each
      call.each { |attr| yield(attr) }
    end

    def to_a
      @to_a ||= map { |v| v }
    end

    private

    def call
      @attributes ||= model.columns_hash.map do |k, v|
        ActiveRecordSchemaScrapper::Attribute.new(
          name:      k,
          type:      v.type,
          precision: v.precision,
          limit:     v.limit,
          scale:     v.scale,
          default:   v.default,
          null:      v.null,
        )
      end
    rescue NoMethodError => e
      @errors << ErrorObject.new(class_name:     model.name,
                                message:        "#{model.name} is not a valid ActiveRecord model.",
                                original_error: e,
                                level:          :error,
                                type:           :invalid_model)
      []
    rescue ActiveRecord::StatementInvalid => e
      level   = model.abstract_class? ? :warn : :error
      message = model.abstract_class? ? "#{model.name} is an abstract class and has no associated table." : e.message
      @errors << ErrorObject.new(class_name:     model.name,
                                message:        message,
                                original_error: e,
                                level:          level,
                                type:           :no_table)
      []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_schema_scrapper-0.2.2 lib/active_record_schema_scrapper/attributes.rb