diff --git a/lib/kredis/type/boolean.rb b/lib/kredis/type/boolean.rb new file mode 100644 index 0000000..526c305 --- /dev/null +++ b/lib/kredis/type/boolean.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Kredis + module Type + class Boolean < ActiveModel::Type::Boolean + def serialize(value) + super ? 1 : 0 + end + end + end +end diff --git a/lib/kredis/type_casting.rb b/lib/kredis/type_casting.rb index aa61c5f..269a38e 100644 --- a/lib/kredis/type_casting.rb +++ b/lib/kredis/type_casting.rb @@ -2,8 +2,9 @@ require "json" require "active_model/type" -require "kredis/type/json" +require "kredis/type/boolean" require "kredis/type/datetime" +require "kredis/type/json" module Kredis::TypeCasting class InvalidType < StandardError; end @@ -13,7 +14,7 @@ class InvalidType < StandardError; end integer: ActiveModel::Type::Integer.new, decimal: ActiveModel::Type::Decimal.new, float: ActiveModel::Type::Float.new, - boolean: ActiveModel::Type::Boolean.new, + boolean: Kredis::Type::Boolean.new, datetime: Kredis::Type::DateTime.new, json: Kredis::Type::Json.new } diff --git a/test/types/flag_test.rb b/test/types/flag_test.rb index 2178f2e..a51696c 100644 --- a/test/types/flag_test.rb +++ b/test/types/flag_test.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "test_helper" +require "active_support/core_ext/integer" class FlagTest < ActiveSupport::TestCase setup { @flag = Kredis.flag "myflag" } diff --git a/test/types/scalar_test.rb b/test/types/scalar_test.rb index e7de529..5bd3bb9 100644 --- a/test/types/scalar_test.rb +++ b/test/types/scalar_test.rb @@ -40,6 +40,22 @@ class ScalarTest < ActiveSupport::TestCase assert_equal false, boolean.value end + test "boolean casting" do + boolean = Kredis.boolean "myscalar" + + boolean.value = true + assert_equal "1", boolean.get + + boolean.value = false + assert_equal "0", boolean.get + + boolean.set "true" + assert_equal true, boolean.value + + boolean.set "false" + assert_equal false, boolean.value + end + test "datetime" do datetime = Kredis.datetime "myscalar" datetime.value = 5.days.from_now.midnight