Skip to content

Make Boolean storage more efficient #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,7 +1,8 @@
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 @@ -11,7 +12,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,4 +1,5 @@
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 @@ -38,6 +38,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