Class: Rails::Generator::GeneratedAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/migratory/generated_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



4
5
6
# File 'lib/migratory/generated_attribute.rb', line 4

def default
  @default
end

#is_indexedObject

Returns the value of attribute is_indexed.



4
5
6
# File 'lib/migratory/generated_attribute.rb', line 4

def is_indexed
  @is_indexed
end

#limitObject

Returns the value of attribute limit.



4
5
6
# File 'lib/migratory/generated_attribute.rb', line 4

def limit
  @limit
end

#precisionObject

Returns the value of attribute precision.



4
5
6
# File 'lib/migratory/generated_attribute.rb', line 4

def precision
  @precision
end

#scaleObject

Returns the value of attribute scale.



4
5
6
# File 'lib/migratory/generated_attribute.rb', line 4

def scale
  @scale
end

Instance Method Details

#initialize_with_migratory(name, type, default = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/migratory/generated_attribute.rb', line 6

def initialize_with_migratory(name, type, default = nil)
  @default = default
  @is_indexed = (name =~ /^\*/) && true
  if type =~ /\[.*?\]/
    details = type.match(/\[(.*?)\]/)[1]
    if details =~ /,/
      @precision, @scale = details.split(/,/).map(&:to_i)
    else
      case type.gsub(/\[.*?\]/, '').to_sym
        when :string, :text, :binary, :integer then @limit = details.to_i
        when :decimal, :float then @precision = details.to_i
      end
    end
  end
  initialize_without_migratory(name.gsub(/^\*/, ''), type.gsub(/\[.*?\]/, ''))
end

#optionsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/migratory/generated_attribute.rb', line 23

def options
  options = ''
  options << ", :limit => #{@limit}" if @limit
  options << ", :precision => #{@precision}" if @precision
  options << ", :scale => #{@scale}" if @scale
  if @default
    if @default == ''
      options << ", :default => nil"
    else
      options << case @type
        when :string, :text, :date, :datetime, :time, :timestamp then ', :default => "'+@default+'"'
        else ", :default => #{@default}"
      end
    end
  end
  options
end