Class: SoberSwag::Serializer::Hash
- Defined in:
- lib/sober_swag/serializer/hash.rb
Overview
Serialize via hash lookup. This is used to speed up serialization of views, but it may be useful elsewhere.
Instance Attribute Summary collapse
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#key_proc ⇒ Object
readonly
Returns the value of attribute key_proc.
Instance Method Summary collapse
- #finalize_lazy_type! ⇒ Object
-
#initialize(choices, default, key_proc) ⇒ Hash
constructor
A new instance of Hash.
- #lazy_type ⇒ Object
- #lazy_type? ⇒ Boolean
- #possible_serializers ⇒ Set<SoberSwag::Serializer::Base>
- #serialize(object, options = {}) ⇒ Object
- #type ⇒ Object
Methods inherited from Base
#array, #identifier, #meta, #optional, #serializer, #via_map
Constructor Details
#initialize(choices, default, key_proc) ⇒ Hash
Returns a new instance of Hash.
16 17 18 19 20 |
# File 'lib/sober_swag/serializer/hash.rb', line 16 def initialize(choices, default, key_proc) @choices = choices @default = default @key_proc = key_proc end |
Instance Attribute Details
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
22 23 24 |
# File 'lib/sober_swag/serializer/hash.rb', line 22 def choices @choices end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
22 23 24 |
# File 'lib/sober_swag/serializer/hash.rb', line 22 def default @default end |
#key_proc ⇒ Object (readonly)
Returns the value of attribute key_proc.
22 23 24 |
# File 'lib/sober_swag/serializer/hash.rb', line 22 def key_proc @key_proc end |
Instance Method Details
#finalize_lazy_type! ⇒ Object
40 41 42 |
# File 'lib/sober_swag/serializer/hash.rb', line 40 def finalize_lazy_type! possible_serializers.each(&:finalize_lazy_type!) end |
#lazy_type ⇒ Object
44 45 46 |
# File 'lib/sober_swag/serializer/hash.rb', line 44 def lazy_type @lazy_type ||= possible_serializers.map(&:lazy_type).reduce(:|) end |
#lazy_type? ⇒ Boolean
36 37 38 |
# File 'lib/sober_swag/serializer/hash.rb', line 36 def lazy_type? possible_serializers.any?(&:lazy_type?) end |
#possible_serializers ⇒ Set<SoberSwag::Serializer::Base>
32 33 34 |
# File 'lib/sober_swag/serializer/hash.rb', line 32 def possible_serializers @possible_serializers ||= (choices.values + [default]).to_set end |
#serialize(object, options = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/sober_swag/serializer/hash.rb', line 24 def serialize(object, = {}) key = key_proc.call(object, ) choices.fetch(key) { default }.serialize(object, ) end |
#type ⇒ Object
48 49 50 |
# File 'lib/sober_swag/serializer/hash.rb', line 48 def type @type ||= possible_serializers.map(&:type).reduce(:|) end |