Module: T::Props::CustomType
- Includes:
- Kernel
- Defined in:
- lib/types/props/custom_type.rb
Class Method Summary collapse
- .included(_base) ⇒ Object
- .scalar_type?(val) ⇒ Boolean
-
.valid_serialization?(val, type = nil) ⇒ Boolean
We allow custom types to serialize to Arrays, so that we can implement set-like fields that store a unique-array, but forbid hashes; Custom hash types should be implemented via an emebdded T::Struct (or a subclass like Chalk::ODM::Document) or via T.
Instance Method Summary collapse
-
#deserialize(_mongo_scalar) ⇒ Object
Given the serialized form of your type, this returns an instance of that custom type representing that value.
-
#instance?(_value) ⇒ Boolean
Returns true if the given Ruby value can be assigned to a T::Props field of this type.
-
#serialize(_instance) ⇒ Object
Given an instance of this type, serialize that into a scalar type supported by T::Props.
-
#valid?(value) ⇒ Boolean
Alias for consistent interface with T::Types::Base.
Class Method Details
.included(_base) ⇒ Object
40 41 42 43 44 |
# File 'lib/types/props/custom_type.rb', line 40 def self.included(_base) super raise 'Please use "extend", not "include" to attach this module' end |
.scalar_type?(val) ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/types/props/custom_type.rb', line 46 def self.scalar_type?(val) # We don't need to check for val's included modules in # T::Configuration.scalar_types, because T::Configuration.scalar_types # are all classes. klass = val.class until klass.nil? return true if T::Configuration.scalar_types.include?(klass.to_s) klass = klass.superclass end false end |
.valid_serialization?(val, type = nil) ⇒ Boolean
We allow custom types to serialize to Arrays, so that we can implement set-like fields that store a unique-array, but forbid hashes; Custom hash types should be implemented via an emebdded T::Struct (or a subclass like Chalk::ODM::Document) or via T.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/types/props/custom_type.rb', line 62 def self.valid_serialization?(val, type=nil) if type&.name == 'Chalk::ODM::BsonTypes::BsonObject' # Special case we allow for backwards compatibility with props formerly # typed as "Object" or "Hash", which contain arbitrarily-nested BSON # data (e.g. parsed API request bodies). In general, we aren't pushing # to convert these to Chalk::ODM::BsonTypes - we'd rather delurk them - # but this lets us convert events with these types to Proto. return true end case val when Array val.each do |v| return false unless scalar_type?(v) end true else scalar_type?(val) end end |
Instance Method Details
#deserialize(_mongo_scalar) ⇒ Object
Given the serialized form of your type, this returns an instance of that custom type representing that value.
36 37 38 |
# File 'lib/types/props/custom_type.rb', line 36 def deserialize(_mongo_scalar) raise NotImplementedError.new('Must override in included class') end |
#instance?(_value) ⇒ Boolean
Returns true if the given Ruby value can be assigned to a T::Props field of this type.
13 14 15 |
# File 'lib/types/props/custom_type.rb', line 13 def instance?(_value) raise NotImplementedError.new('Must override in included class') end |
#serialize(_instance) ⇒ Object
Given an instance of this type, serialize that into a scalar type supported by T::Props.
27 28 29 |
# File 'lib/types/props/custom_type.rb', line 27 def serialize(_instance) raise NotImplementedError.new('Must override in included class') end |
#valid?(value) ⇒ Boolean
Alias for consistent interface with T::Types::Base
18 19 20 |
# File 'lib/types/props/custom_type.rb', line 18 def valid?(value) instance?(value) end |