Class: Typelizer::SerializerPlugins::Alba::BlockAttributeCollector
- Inherits:
-
Object
- Object
- Typelizer::SerializerPlugins::Alba::BlockAttributeCollector
- Defined in:
- lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb
Defined Under Namespace
Classes: BlockAssociation, BlockNestedAttribute
Instance Attribute Summary collapse
-
#collected_attributes ⇒ Object
readonly
Returns the value of attribute collected_attributes.
-
#collected_typelizes ⇒ Object
readonly
Returns the value of attribute collected_typelizes.
Instance Method Summary collapse
- #attribute(name, **options, &block) ⇒ Object
- #attributes(*names, **options) ⇒ Object
-
#initialize ⇒ BlockAttributeCollector
constructor
A new instance of BlockAttributeCollector.
- #many(name, **options, &block) ⇒ Object (also: #has_many)
-
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
Ignore other DSL methods that might be called.
- #nested_attribute(name, **options, &block) ⇒ Object (also: #nested)
-
#one(name, **options, &block) ⇒ Object
(also: #has_one, #association)
Support association methods that might be used in traits.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#typelize(type_or_hash = nil, **options) ⇒ Object
Capture typelize calls - they apply to the next attribute Handles both: typelize :string, nullable: true (type with options, applies to next attribute) typelize attr_name: [:string, nullable: true] (hash-style, applies to specific attribute).
Constructor Details
#initialize ⇒ BlockAttributeCollector
Returns a new instance of BlockAttributeCollector.
8 9 10 11 12 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 8 def initialize @collected_attributes = {} @collected_typelizes = {} @pending_typelize = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
Ignore other DSL methods that might be called
103 104 105 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 103 def method_missing(method_name, *args, **kwargs, &block) # Silently ignore unknown methods end |
Instance Attribute Details
#collected_attributes ⇒ Object (readonly)
Returns the value of attribute collected_attributes.
6 7 8 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 6 def collected_attributes @collected_attributes end |
#collected_typelizes ⇒ Object (readonly)
Returns the value of attribute collected_typelizes.
6 7 8 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 6 def collected_typelizes @collected_typelizes end |
Instance Method Details
#attribute(name, **options, &block) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 20 def attribute(name, **, &block) @collected_attributes[name] = block || name # Apply pending typelize to this attribute if @pending_typelize @collected_typelizes[name] = @pending_typelize @pending_typelize = nil end end |
#attributes(*names, **options) ⇒ Object
14 15 16 17 18 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 14 def attributes(*names, **) names.each do |name| @collected_attributes[name] = name end end |
#many(name, **options, &block) ⇒ Object Also known as: has_many
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 87 def many(name, **, &block) resource = [:resource] || [:serializer] with_traits = [:with_traits] key = [:key] || name @collected_attributes[key] = BlockAssociation.new( name: name, resource: resource, with_traits: with_traits, multi: true, key: key ) end |
#nested_attribute(name, **options, &block) ⇒ Object Also known as: nested
62 63 64 65 66 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 62 def nested_attribute(name, **, &block) raise ArgumentError, "Block is required for nested_attribute" unless block @collected_attributes[name] = BlockNestedAttribute.new(name: name, block: block) end |
#one(name, **options, &block) ⇒ Object Also known as: has_one, association
Support association methods that might be used in traits
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 71 def one(name, **, &block) resource = [:resource] || [:serializer] with_traits = [:with_traits] key = [:key] || name @collected_attributes[key] = BlockAssociation.new( name: name, resource: resource, with_traits: with_traits, multi: false, key: key ) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
107 108 109 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 107 def respond_to_missing?(method_name, include_private = false) true end |
#typelize(type_or_hash = nil, **options) ⇒ Object
Capture typelize calls - they apply to the next attribute Handles both:
typelize :string, nullable: true (type with options, applies to next attribute)
typelize attr_name: [:string, nullable: true] (hash-style, applies to specific attribute)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb', line 33 def typelize(type_or_hash = nil, **) if type_or_hash.is_a?(Hash) # typelize({name: [:string, nullable: true]}) - explicit hash type_or_hash.each do |attr_name, type_def| @collected_typelizes[attr_name] = normalize_typelize(type_def) end elsif type_or_hash.nil? && .any? # typelize name: [:string, nullable: true] - Ruby passes as kwargs # Check if this looks like attribute definitions (values are arrays or have type-like keys) if .values.first.is_a?(Array) || .values.first.is_a?(Symbol) || .values.first.is_a?(String) .each do |attr_name, type_def| @collected_typelizes[attr_name] = normalize_typelize(type_def) end else # typelize :string, nullable: true - type with options @pending_typelize = normalize_typelize(nil, **) end else # typelize :string - applies to the next attribute @pending_typelize = normalize_typelize(type_or_hash, **) end end |