Class: ApiSerializer::Serialization
- Inherits:
-
Object
- Object
- ApiSerializer::Serialization
- Extended by:
- Literal::Types
- Defined in:
- lib/api_serializer/serialization.rb
Direct Known Subclasses
Class Method Summary collapse
- .attribute(name, type = _Any, reader: :public, positional: false, default: nil, from: nil, to: nil, transform: nil, queryable: nil, &coercer) ⇒ Object
- .attribute_names ⇒ Object
- .attribute_options ⇒ Object
- .compose(name, type = _Any, from: nil, reader: :public, positional: false, default: nil, &composer) ⇒ Object
- .composed_with ⇒ Object
- .decompose(names, type = _Any, from:, reader: :public, positional: false, default: nil, &decomposer) ⇒ Object
- .filtering_mapped_attributes(depth: 1) ⇒ Object
- .has_many(attribute_name, serializer_variant_resolver, reader: :public, positional: false, default: nil, from: nil, to: nil, virtual: false, queryable: nil, &coercer) ⇒ Object
- .has_one(attribute_name, serializer_variant_resolver, reader: :public, positional: false, default: nil, from: nil, to: nil, virtual: false, queryable: nil, &coercer) ⇒ Object
- .inherited(subclass) ⇒ Object
- .reflect_on(name) ⇒ Object
-
.schema_name ⇒ Object
schema_name and composed_with are overridden by VariantBuilder when it produces a concrete serialization subclass.
- .sorting_mapped_attributes(depth: 1) ⇒ Object
- .target_data_structure ⇒ Object
- .transformer ⇒ Object
- .virtual(name, type = _Any, reader: :public, positional: false, default: nil, from: nil, &coercer) ⇒ Object
Class Method Details
.attribute(name, type = _Any, reader: :public, positional: false, default: nil, from: nil, to: nil, transform: nil, queryable: nil, &coercer) ⇒ Object
15 16 17 18 |
# File 'lib/api_serializer/serialization.rb', line 15 def attribute(name, type = _Any, reader: :public, positional: false, default: nil, from: nil, to: nil, transform: nil, queryable: nil, &coercer) from_or_name = from || to || name.to_s define_schema_attribute(name, type, reader:, positional:, default:, from: from_or_name, transform:, queryable:, &coercer) end |
.attribute_names ⇒ Object
75 76 77 |
# File 'lib/api_serializer/serialization.rb', line 75 def attribute_names target_data_structure.attribute_names end |
.attribute_options ⇒ Object
63 64 65 |
# File 'lib/api_serializer/serialization.rb', line 63 def target_data_structure. end |
.compose(name, type = _Any, from: nil, reader: :public, positional: false, default: nil, &composer) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/api_serializer/serialization.rb', line 20 def compose(name, type = _Any, from: nil, reader: :public, positional: false, default: nil, &composer) raise ArgumentError, "The 'from' option must be an array of attribute names" unless from.is_a?(Array) raise ArgumentError, "You must provide a 'compose' block with signature `|#{from.join(", ")}(, context)|`" unless composer define_schema_attribute(name, type, composed_of: from.map(&:to_s), transform: composer, reader:, positional:, default:) end |
.composed_with ⇒ Object
55 56 57 |
# File 'lib/api_serializer/serialization.rb', line 55 def composed_with raise NoMethodError, "composed_with is set by VariantBuilder on concrete serialization subclasses" end |
.decompose(names, type = _Any, from:, reader: :public, positional: false, default: nil, &decomposer) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/api_serializer/serialization.rb', line 27 def decompose(names, type = _Any, from:, reader: :public, positional: false, default: nil, &decomposer) raise ArgumentError, "The 'names' option must be an array of attribute names" unless names.is_a?(Array) raise ArgumentError, "You must provide a 'decompose' block with signature `|#{from}|` which returns #{names.size} values in an array" unless decomposer names.each do |name| define_schema_attribute(name.to_sym, type, from:, decompose_to: names.map(&:to_sym), transform: decomposer, reader: reader, positional: positional, default: default) end end |
.filtering_mapped_attributes(depth: 1) ⇒ Object
79 80 81 |
# File 'lib/api_serializer/serialization.rb', line 79 def filtering_mapped_attributes(depth: 1) mapped_attribute_paths(:filterable?, depth:) end |
.has_many(attribute_name, serializer_variant_resolver, reader: :public, positional: false, default: nil, from: nil, to: nil, virtual: false, queryable: nil, &coercer) ⇒ Object
44 45 46 |
# File 'lib/api_serializer/serialization.rb', line 44 def has_many(attribute_name, serializer_variant_resolver, reader: :public, positional: false, default: nil, from: nil, to: nil, virtual: false, queryable: nil, &coercer) association_attribute(attribute_name, serializer_variant_resolver, reader:, positional:, default:, from:, to:, virtual:, collection: true, queryable:, &coercer) end |
.has_one(attribute_name, serializer_variant_resolver, reader: :public, positional: false, default: nil, from: nil, to: nil, virtual: false, queryable: nil, &coercer) ⇒ Object
40 41 42 |
# File 'lib/api_serializer/serialization.rb', line 40 def has_one(attribute_name, serializer_variant_resolver, reader: :public, positional: false, default: nil, from: nil, to: nil, virtual: false, queryable: nil, &coercer) association_attribute(attribute_name, serializer_variant_resolver, reader:, positional:, default:, from:, to:, virtual:, collection: false, queryable:, &coercer) end |
.inherited(subclass) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/api_serializer/serialization.rb', line 6 def inherited(subclass) struct = target_data_structure subclass.instance_exec do @target_data_structure = Class.new(struct) @target_data_structure_inherits_from = "(inherited from #{struct.name})" if struct.name end super end |
.reflect_on(name) ⇒ Object
59 60 61 |
# File 'lib/api_serializer/serialization.rb', line 59 def reflect_on(name) [name] end |
.schema_name ⇒ Object
schema_name and composed_with are overridden by VariantBuilder when it produces a concrete serialization subclass. Calling them on the base class is a programmer error — flag it rather than returning nonsense.
51 52 53 |
# File 'lib/api_serializer/serialization.rb', line 51 def schema_name raise NoMethodError, "schema_name is set by VariantBuilder on concrete serialization subclasses" end |
.sorting_mapped_attributes(depth: 1) ⇒ Object
83 84 85 |
# File 'lib/api_serializer/serialization.rb', line 83 def sorting_mapped_attributes(depth: 1) mapped_attribute_paths(:sortable?, depth:) end |
.target_data_structure ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/api_serializer/serialization.rb', line 67 def target_data_structure @target_data_structure ||= begin data_class = ::Class.new(TargetDataStructure) data_class.set_temporary_name("target_data_structure/#{name}") data_class end end |
.transformer ⇒ Object
87 88 89 90 |
# File 'lib/api_serializer/serialization.rb', line 87 def transformer target_data_structure.set_temporary_name("target_data_structure/#{name}#{" (#{@target_data_structure_inherits_from})" if @target_data_structure_inherits_from}") DataTransformer.new(target_data_structure) end |
.virtual(name, type = _Any, reader: :public, positional: false, default: nil, from: nil, &coercer) ⇒ Object
36 37 38 |
# File 'lib/api_serializer/serialization.rb', line 36 def virtual(name, type = _Any, reader: :public, positional: false, default: nil, from: nil, &coercer) define_schema_attribute(name, type, reader:, positional:, default:, from:, transform: coercer, virtual: true) end |