Skip to content

Commit

Permalink
Merge pull request rails#123 from basecamp/more-efficient-boolean-cas…
Browse files Browse the repository at this point in the history
…ting

Make Boolean storage more efficient
  • Loading branch information
guilleiguaran authored Jul 13, 2023
2 parents 3ffa935 + a686d2d commit 916f6ef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/kredis/type/boolean.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions lib/kredis/type_casting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions test/types/flag_test.rb
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
16 changes: 16 additions & 0 deletions test/types/scalar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 916f6ef

Please sign in to comment.