Sha256: a9ba741daf83d409f44e88f6a4d57bf4620bb621ebf813a8f8c763da1d575b65

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module HoboFields

  class FieldSpec

    class UnknownSqlTypeError < RuntimeError; end

    def initialize(model, name, type, options={})
      raise ArgumentError, "you cannot provide a field spec for the primary key" if name == model.primary_key
      self.model = model
      self.name = name.to_sym
      self.type = type.is_a?(String) ? type.to_sym : type
      self.options = options
      self.position = model.field_specs.length
    end

    attr_accessor :model, :name, :type, :position, :options

    def sql_type
      options[:sql_type] or begin
                              if native_type?(type)
                                type
                              else
                                field_class = HoboFields.to_class(type)
                                field_class && field_class::COLUMN_TYPE or raise UnknownSqlTypeError, "#{type.inspect} for #{model}.#{name}"
                              end
                            end
    end

    def limit
      options[:limit] || native_types[sql_type][:limit]
    end

    def precision
      options[:precision]
    end

    def scale
      options[:scale]
    end

    def null
      :null.in?(options) ? options[:null] : true
    end

    def default
      options[:default]
    end

    def different_to?(col_spec)
      sql_type != col_spec.type ||
        begin
          check_cols = [:null, :default]
          check_cols += [:precision, :scale] if sql_type == :decimal
          check_cols << :limit if sql_type.in?([:string, :text, :binary, :integer])
          check_cols.any? { |k| col_spec.send(k) != self.send(k) }
        end
    end


    private

    def native_type?(type)
      type.in?(native_types.keys - [:primary_key])
    end

    def native_types
      MigrationGenerator.native_types
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hobofields-0.8.2 lib/hobo_fields/field_spec.rb
hobofields-0.8.1 lib/hobo_fields/field_spec.rb
hobofields-0.8.3 lib/hobo_fields/field_spec.rb
hobofields-0.8 lib/hobo_fields/field_spec.rb