Module: SerializationHelper::Utils
- Defined in:
- lib/serialization_helper.rb
Class Method Summary collapse
- .boolean_columns(table) ⇒ Object
- .convert_boolean(value) ⇒ Object
- .convert_booleans(records, columns) ⇒ Object
- .is_boolean(value) ⇒ Object
- .quote_table(table) ⇒ Object
- .unhash(hash, keys) ⇒ Object
- .unhash_records(records, keys) ⇒ Object
Class Method Details
.boolean_columns(table) ⇒ Object
142 143 144 145 |
# File 'lib/serialization_helper.rb', line 142 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
138 139 140 |
# File 'lib/serialization_helper.rb', line 138 def self.convert_boolean(value) ['t', '1', true, 1].include?(value) end |
.convert_booleans(records, columns) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/serialization_helper.rb', line 128 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
147 148 149 |
# File 'lib/serialization_helper.rb', line 147 def self.is_boolean(value) value.kind_of?(TrueClass) or value.kind_of?(FalseClass) end |
.quote_table(table) ⇒ Object
151 152 153 |
# File 'lib/serialization_helper.rb', line 151 def self.quote_table(table) ActiveRecord::Base.connection.quote_table_name(table) end |
.unhash(hash, keys) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/serialization_helper.rb', line 107 def self.unhash(hash, keys) column_hash = keys.dup keys = column_hash.keys keys.map { |key| if hash[key].blank? && !column_hash[key] '0' else hash[key] end } end |
.unhash_records(records, keys) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/serialization_helper.rb', line 120 def self.unhash_records(records, keys) records.each_with_index do |record, index| records[index] = unhash(record, keys) end records end |