Sha256: bda2bd88af18e613573dcca550b8a8ffdd0f4741a2a8e967fc5b0ae908161766

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module DataMapper
  module Mappings
    
    # TODO: There are of course many more options to add here.
    # Ordinal, Length/Size, Nullability are just a few.
    class Column
    
      attr_accessor :name, :type, :options
    
      def initialize(database, name, type, options = {})
        @database = database
        @name, @type, @options = name.to_sym, type, options
      end
    
      def lazy=(value)
        @options[:lazy] = value
      end
    
      # Determines if the field should be lazy loaded.
      # You can set this explicitly, or accept the default,
      # which is false for all but text fields.
      def lazy?
        @options[:lazy] || (type == :text)
      end
      
      def nullable?
        @options[:nullable] || true
      end
      
      def key?
        @options[:key] || false
      end
    
      def to_sym
        @name
      end
      
      def instance_variable_name
        @instance_variable_name || (@instance_variable_name = "@#{@name.to_s.gsub(/\?$/, '')}".freeze)
      end
      
      def to_s
        @name.to_s
      end
      
      def type_cast_value(value)
        @database.type_cast_value(type, value)
      end
      
      def column_name
        @column_name || (@column_name = (@options.has_key?(:column) ? @options[:column].to_s : name.to_s.gsub(/\?$/, '')).freeze)
      end
      
      def to_sql
        @to_sql || (@to_sql = @database.quote_column_name(column_name).freeze)
      end
      
      def size
        @size || begin
          return @size = @options[:size] if @options.has_key?(:size)
          return @size = @options[:length] if @options.has_key?(:length)
          
          @size = case type
            when :integer then 4
            when :string, :class then 50
            else nil
          end
        end
      end
      
      def inspect
        "#<%s:0x%x @name=%s, @type=%s, @options=%s>" % [self.class.name, (object_id * 2), to_sql, type.inspect, options.inspect]
      end
      
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper-0.1.0 lib/data_mapper/mappings/column.rb