Module: SerializationHelper::Utils

Defined in:
lib/serialization_helper.rb

Class Method Summary collapse

Class Method Details

.boolean_columns(table) ⇒ Object



128
129
130
131
# File 'lib/serialization_helper.rb', line 128

def self.boolean_columns(table)
  columns = ActiveRecord::Base.connection.columns(table).reject { |c| silence_warnings { c.type != :boolean } }
  columns.map { |c| c.name }
end

.convert_boolean(value) ⇒ Object



124
125
126
# File 'lib/serialization_helper.rb', line 124

def self.convert_boolean(value)
  ['t', '1', true, 1].include?(value)
end

.convert_booleans(records, columns) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/serialization_helper.rb', line 114

def self.convert_booleans(records, columns)
  records.each do |record|
    columns.each do |column|
      next if is_boolean(record[column])
      record[column] = convert_boolean(record[column])
    end
  end
  records
end

.is_boolean(value) ⇒ Object



133
134
135
# File 'lib/serialization_helper.rb', line 133

def self.is_boolean(value)
  value.kind_of?(TrueClass) or value.kind_of?(FalseClass)
end

.quote_table(table) ⇒ Object



137
138
139
# File 'lib/serialization_helper.rb', line 137

def self.quote_table(table)
  ActiveRecord::Base.connection.quote_table_name(table)
end

.unhash(hash, keys) ⇒ Object



102
103
104
# File 'lib/serialization_helper.rb', line 102

def self.unhash(hash, keys)
  keys.map { |key| hash[key] }
end

.unhash_records(records, keys) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/serialization_helper.rb', line 106

def self.unhash_records(records, keys)
  records.each_with_index do |record, index|
    records[index] = unhash(record, keys)
  end

  records
end