Module: Workarea::DataFile::CsvFields
- Extended by:
- CsvFields
- Included in:
- CsvFields
- Defined in:
- app/models/workarea/data_file/csv_fields.rb
Instance Method Summary collapse
- #deserialize_array(hash, name, model) ⇒ Object
- #deserialize_date(hash, name, model) ⇒ Object
- #deserialize_datetime(hash, name, model) ⇒ Object
- #deserialize_from(hash, field:, model:) ⇒ Object
- #deserialize_hash(hash, name, model) ⇒ Object
- #deserialize_time(hash, name, model) ⇒ Object
- #serialize_activemerchant_billing_response(result, name, value) ⇒ Object
-
#serialize_array(result, name, value) ⇒ Object
Type based serialization/deserialization.
- #serialize_country(result, name, value) ⇒ Object
- #serialize_hash(result, name, value) ⇒ Object
- #serialize_to(result, field:, value:) ⇒ Object
Instance Method Details
#deserialize_array(hash, name, model) ⇒ Object
36 37 38 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 36 def deserialize_array(hash, name, model) hash[name].to_s.split(',') end |
#deserialize_date(hash, name, model) ⇒ Object
79 80 81 82 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 79 def deserialize_date(hash, name, model) return unless (hash[name]) Date.parse(hash[name]) end |
#deserialize_datetime(hash, name, model) ⇒ Object
74 75 76 77 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 74 def deserialize_datetime(hash, name, model) return unless (hash[name]) DateTime.parse(hash[name]) end |
#deserialize_from(hash, field:, model:) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 18 def deserialize_from(hash, field:, model:) deserialize_method = "deserialize_#{field.type.name.parameterize.underscore}" if respond_to?(deserialize_method) send(deserialize_method, hash, field.name, model) else hash[field.name] end end |
#deserialize_hash(hash, name, model) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 46 def deserialize_hash(hash, name, model) current_value = model.send(name) || {} parsed = Hash[ hash.select { |k, v| k =~ /^#{name}_.*/ }.map do |key, value| destination_key = key.gsub(/^#{name}_/, '') if value.present? [destination_key, value.include?(',') ? value.split(',') : value] end end.compact ] current_value.merge(parsed) end |
#deserialize_time(hash, name, model) ⇒ Object
69 70 71 72 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 69 def deserialize_time(hash, name, model) return unless (hash[name]) Time.parse(hash[name]) end |
#serialize_activemerchant_billing_response(result, name, value) ⇒ Object
65 66 67 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 65 def serialize_activemerchant_billing_response(result, name, value) serialize_hash(result, name, flatten_hash(value.as_json)) end |
#serialize_array(result, name, value) ⇒ Object
Type based serialization/deserialization
32 33 34 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 32 def serialize_array(result, name, value) result[name] = value&.join(',') end |
#serialize_country(result, name, value) ⇒ Object
61 62 63 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 61 def serialize_country(result, name, value) result[name] = value.is_a?(Country) ? value.alpha2 : value end |
#serialize_hash(result, name, value) ⇒ Object
40 41 42 43 44 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 40 def serialize_hash(result, name, value) value&.each do |key, value| result["#{name}_#{key}"] = value.is_a?(Array) ? value.join(',') : value end end |
#serialize_to(result, field:, value:) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'app/models/workarea/data_file/csv_fields.rb', line 6 def serialize_to(result, field:, value:) serialize_method = "serialize_#{field.type.name.parameterize.underscore}" if respond_to?(serialize_method) send(serialize_method, result, field.name, value) else result[field.name] = value end result end |