Class: Depository::Serialize::Unpacker
- Inherits:
-
Object
- Object
- Depository::Serialize::Unpacker
- Defined in:
- lib/depository/serialize.rb
Instance Method Summary collapse
- #call ⇒ Object
- #columns ⇒ Object
- #drop_usecs(time) ⇒ Object
-
#initialize(config, attrs) ⇒ Unpacker
constructor
A new instance of Unpacker.
Constructor Details
#initialize(config, attrs) ⇒ Unpacker
Returns a new instance of Unpacker.
61 62 63 64 |
# File 'lib/depository/serialize.rb', line 61 def initialize(config, attrs) self.config = config self.attrs = attrs end |
Instance Method Details
#call ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/depository/serialize.rb', line 66 def call columns.select { |column, opts| opts[:type] == :datetime && ![nil, 0].include?(attrs[column]) }.each { |column, opts| attrs[column] = drop_usecs(attrs[column]) } columns.select { |column, opts| opts[:type] == :date && ![nil, 0].include?(attrs[column]) }.each { |column, opts| attrs[column] = Date.parse(attrs[column]) if attrs[column].is_a?(String) } columns.select { |column, opts| !attrs[column].nil? && opts[:type] == :boolean }.each { |column, opts| attrs[column] = [1, true].include?(attrs[column]) } config.unpacker.call(attrs) attrs end |
#columns ⇒ Object
95 96 97 |
# File 'lib/depository/serialize.rb', line 95 def columns @columns ||= Depository::Database.db.schema(config.db) end |
#drop_usecs(time) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/depository/serialize.rb', line 85 def drop_usecs(time) if time.is_a?(String) Time.at(Time.parse(time).to_i) elsif time.is_a?(DateTime) Time.at(time.to_time.to_i) else Time.at(time.to_i) end end |