Class: RDL::Rails

Inherits:
Object show all
Defined in:
lib/types/rails-5.x/_helpers.rb

Class Method Summary collapse

Class Method Details

.attribute_types(model) ⇒ Object

+ model +

is an ActiveRecord::Base subclass that has been loaded.

Gets the columns_hash of the model and returns a String that can serve as the paramter list to a method that accepts any number of the model’s attributes keyed by the attribute names.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/types/rails-5.x/_helpers.rb', line 38

def self.attribute_types(model)
  args = []

  model.columns_hash.each { |name, col|
    t = column_to_rdl(col.type)
    if col.null
      args << "#{name}: ?#{t}"
    else
      args << "#{name}: ?!#{t}"
    end
  }
  return args.join ','
end

.column_to_rdl(rails_type) ⇒ Object

+ rails_type +

is a Rails column type (:string, :integer, etc)

returns a String containing an RDL type



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/types/rails-5.x/_helpers.rb', line 11

def self.column_to_rdl(rails_type)
  case rails_type
  when :string, :text, :binary
    return 'String'
  when :integer
    return 'Fixnum'
  when :float
    return 'Float'
  when :decimal
    return 'BigDecimal'
  when :boolean
    return '%bool'
  when :date
    return 'Date'
  when :time
    return 'Time'
  when :datetime
    return 'DateTime'
  else
    raise RuntimeError, "Unrecoganized column type #{rails_type}"
  end
end