Class: ActiveRecord::Properties::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_properties/property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name, type, options = {}) ⇒ Property

Returns a new instance of Property.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ar_properties/property.rb', line 4

def initialize(base, name, type, options={})
  @name, @type, @base = name, type, base

  native = base.native_database_types
  @limit = options.fetch(:limit) do
    native[type][:limit] if native[type].is_a?(Hash)
  end

  @precision = options[:precision]
  @scale     = options[:scale]
  @default   = options[:default]
  @null      = options[:null]

  unless base.native_database_types.keys.include?(type.try(:to_sym))
    raise PropertyError, "unknown type: #{type.inspect}"
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def base
  @base
end

#defaultObject (readonly)

Returns the value of attribute default.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def default
  @default
end

#limitObject (readonly)

Returns the value of attribute limit.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def null
  @null
end

#precisionObject (readonly)

Returns the value of attribute precision.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def precision
  @precision
end

#scaleObject (readonly)

Returns the value of attribute scale.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def scale
  @scale
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/ar_properties/property.rb', line 22

def type
  @type
end

Instance Method Details

#primary?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ar_properties/property.rb', line 34

def primary?
  type.to_sym == :primary_key
end

#sql_typeObject



30
31
32
# File 'lib/ar_properties/property.rb', line 30

def sql_type
  base.type_to_sql(type.to_sym, limit, precision, scale)
end

#to_columnObject



24
25
26
27
28
# File 'lib/ar_properties/property.rb', line 24

def to_column
  ConnectionAdapters::Column.new(name.to_s, default.try(:to_s), sql_type, null).tap do |column|
    column.primary = primary?
  end
end