-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a9fc35
commit a8a3ba5
Showing
5 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
class Literal::Rails::FlagsType < ActiveModel::Type::Value | ||
def initialize(flags_class) | ||
@flags_class = flags_class | ||
super() | ||
end | ||
|
||
def cast(value) | ||
case value | ||
when @flags_class | ||
value | ||
else | ||
deserialize(value) | ||
end | ||
end | ||
|
||
def serialize(value) | ||
case value | ||
when nil | ||
nil | ||
when @flags_class | ||
value.to_bit_string | ||
else | ||
raise Literal::ArgumentError.new( | ||
"Invalid value: #{value.inspect}. Expected an #{@flags_class.inspect}.", | ||
) | ||
end | ||
end | ||
|
||
def deserialize(value) | ||
case value | ||
when nil | ||
nil | ||
else | ||
@flags_class.from_bit_string(value) || raise( | ||
ArgumentError.new("Invalid value: #{value.inspect} for #{@flags_class}"), | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters