Class: RDL::Rails

Inherits:
Object show all
Defined in:
lib/types/rails/_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.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/types/rails/_helpers.rb', line 43

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/types/rails/_helpers.rb', line 10

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