Class: Typelizer::SerializerPlugins::Alba
- Defined in:
- lib/typelizer/serializer_plugins/alba.rb
Defined Under Namespace
Classes: BlockAttributeCollector, TraitInterface
Constant Summary collapse
- ALBA_TS_MAPPER =
{ "String" => {type: :string}, "Integer" => {type: :number}, "Boolean" => {type: :boolean}, "ArrayOfString" => {type: :string, multi: true}, "ArrayOfInteger" => {type: :number, multi: true} }
Instance Method Summary collapse
- #build_collected_property(name, attr) ⇒ Object
- #infer_resource_from_name(name) ⇒ Object
- #meta_fields ⇒ Object
- #properties ⇒ Object
- #root_key ⇒ Object
- #trait_interfaces ⇒ Object
- #trait_properties(trait_name) ⇒ Object
- #traits ⇒ Object
Methods inherited from Base
Constructor Details
This class inherits a constructor from Typelizer::SerializerPlugins::Base
Instance Method Details
#build_collected_property(name, attr) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 60 def build_collected_property(name, attr) case attr when BlockAttributeCollector::BlockAssociation prop_name = has_transform_key?(serializer) ? fetch_key(serializer, name) : name with_traits = Array(attr.with_traits) if attr.with_traits resource = attr.resource || infer_resource_from_name(name) Property.new( name: prop_name, type: resource ? context.interface_for(resource) : nil, optional: false, nullable: false, multi: attr.multi, column_name: name, with_traits: with_traits ) when BlockAttributeCollector::BlockNestedAttribute prop_name = has_transform_key?(serializer) ? fetch_key(serializer, name) : name Property.new( name: prop_name, type: Shape.new(properties: collect_nested_block(attr.block)), optional: false, nullable: false, multi: false, column_name: name ) else build_property(name, attr) end end |
#infer_resource_from_name(name) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 91 def infer_resource_from_name(name) class_name = name.to_s.classify # Try common serializer naming conventions ["#{class_name}Resource", "#{class_name}Serializer"].each do |resource_name| return serializer.const_get(resource_name, false) rescue NameError # Try in parent namespace begin return Object.const_get("#{serializer.module_parent}::#{resource_name}") rescue NameError # Not found in this namespace end end nil end |
#meta_fields ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 29 def return nil unless serializer. name = serializer..first return nil unless name [ build_property(name, name) ] end |
#properties ⇒ Object
14 15 16 17 18 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 14 def properties serializer._attributes.map do |name, attr| build_property(name.is_a?(Symbol) ? name.name : name, attr) end end |
#root_key ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 20 def root_key root = serializer.new({}).send(:_key) if !root.nil? && has_transform_key?(serializer) && should_transform_root_key?(serializer) fetch_key(serializer, root) else root end end |
#trait_interfaces ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 107 def trait_interfaces @trait_interfaces ||= traits.map do |trait_name, _| TraitInterface.new( serializer: serializer, trait_name: trait_name, context: context, plugin: self ) end end |
#trait_properties(trait_name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 46 def trait_properties(trait_name) trait_block = traits[trait_name] return [], {} unless trait_block collector = BlockAttributeCollector.new collector.instance_exec(&trait_block) props = collector.collected_attributes.map do |name, attr| build_collected_property(name.is_a?(Symbol) ? name.name : name, attr) end [props, collector.collected_typelizes] end |
#traits ⇒ Object
40 41 42 43 44 |
# File 'lib/typelizer/serializer_plugins/alba.rb', line 40 def traits return {} unless serializer.instance_variable_defined?(:@_traits) serializer.instance_variable_get(:@_traits) || {} end |