lib/lockbox/model.rb in lockbox-0.2.2 vs lib/lockbox/model.rb in lockbox-0.2.3

- old
+ new

@@ -48,11 +48,11 @@ # options[:type] = :integer # when Float # options[:type] = :float # end - raise ArgumentError, "Unknown type: #{options[:type]}" unless [nil, :string, :boolean, :date, :datetime, :integer, :float, :binary, :json, :hash].include?(options[:type]) + raise ArgumentError, "Unknown type: #{options[:type]}" unless [nil, :string, :boolean, :date, :datetime, :time, :integer, :float, :binary, :json, :hash].include?(options[:type]) attribute_type = case options[:type] when nil, :json, :hash :string @@ -154,10 +154,15 @@ message = message.strftime("%Y-%m-%d") unless message.nil? when :datetime message = ActiveRecord::Type::DateTime.new.serialize(message) message = nil unless message.respond_to?(:iso8601) # for Active Record < 5.2 message = message.iso8601(9) unless message.nil? + when :time + message = ActiveRecord::Type::Time.new.serialize(message) + message = nil unless message.respond_to?(:strftime) + message = message.strftime("%H:%M:%S.%N") unless message.nil? + message when :integer message = ActiveRecord::Type::Integer.new(limit: 8).serialize(message) message = 0 if message.nil? # signed 64-bit integer, big endian message = [message].pack("q>") @@ -214,22 +219,27 @@ message = message == "t" when :date message = ActiveRecord::Type::Date.new.deserialize(message) when :datetime message = ActiveRecord::Type::DateTime.new.deserialize(message) + when :time + message = ActiveRecord::Type::Time.new.deserialize(message) when :integer message = ActiveRecord::Type::Integer.new(limit: 8).deserialize(message.unpack("q>").first) when :float message = ActiveRecord::Type::Float.new.deserialize(message.unpack("G").first) when :string - message = message.encode(Encoding::UTF_8) + message.force_encoding(Encoding::UTF_8) when :binary # do nothing # decrypt returns binary string else type = self.class.attribute_types[name.to_s] if type.is_a?(ActiveRecord::Type::Serialized) message = type.deserialize(message) + else + # default to string if not serialized + message.force_encoding(Encoding::UTF_8) end end end # set previous attribute on first decrypt