Module: BrightSerializer::Serializer::ClassMethods
- Defined in:
- lib/bright_serializer/serializer.rb
Instance Attribute Summary collapse
-
#attributes_to_serialize ⇒ Object
readonly
Returns the value of attribute attributes_to_serialize.
-
#transform_method ⇒ Object
readonly
Returns the value of attribute transform_method.
Instance Method Summary collapse
- #attributes(*attributes, **options, &block) ⇒ Object (also: #attribute)
- #entity ⇒ Object
- #entity_name ⇒ Object
-
#has_one(key, serializer:, **options, &block) ⇒ Object
(also: #has_many, #belongs_to)
rubocop:disable Naming/PredicateName.
- #inherited(subclass) ⇒ Object
- #run_transform_key(input) ⇒ Object
-
#set_key_transform(transform_name) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
Instance Attribute Details
#attributes_to_serialize ⇒ Object (readonly)
Returns the value of attribute attributes_to_serialize.
62 63 64 |
# File 'lib/bright_serializer/serializer.rb', line 62 def attributes_to_serialize @attributes_to_serialize end |
#transform_method ⇒ Object (readonly)
Returns the value of attribute transform_method.
62 63 64 |
# File 'lib/bright_serializer/serializer.rb', line 62 def transform_method @transform_method end |
Instance Method Details
#attributes(*attributes, **options, &block) ⇒ Object Also known as: attribute
71 72 73 74 75 76 77 |
# File 'lib/bright_serializer/serializer.rb', line 71 def attributes(*attributes, **, &block) attributes.each do |key| attribute = Attribute.new(key, [:if], [:entity], &block) attribute.transformed_key = run_transform_key(key) @attributes_to_serialize << attribute end end |
#entity ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/bright_serializer/serializer.rb', line 108 def entity {}.tap do |result| @attributes_to_serialize.each do |attribute| entity_value = attribute.entity&.to_h || BrightSerializer::Entity::Base::DEFAULT_DEFINITION result.merge!(attribute.transformed_key => entity_value) end end end |
#entity_name ⇒ Object
118 119 120 |
# File 'lib/bright_serializer/serializer.rb', line 118 def entity_name name.split('::').last.downcase end |
#has_one(key, serializer:, **options, &block) ⇒ Object Also known as: has_many, belongs_to
rubocop:disable Naming/PredicateName
81 82 83 84 85 86 87 |
# File 'lib/bright_serializer/serializer.rb', line 81 def has_one(key, serializer:, **, &block) # rubocop:disable Naming/PredicateName attribute = AttributeRelation.new( key, serializer, .delete(:if), .delete(:entity), , &block ) attribute.transformed_key = run_transform_key(key) @attributes_to_serialize << attribute end |
#inherited(subclass) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/bright_serializer/serializer.rb', line 64 def inherited(subclass) super subclass.instance_variable_set(:@attributes_to_serialize, []) unless subclass.attributes_to_serialize subclass.attributes_to_serialize.concat(@attributes_to_serialize) subclass.instance_variable_set(:@transform_method, @transform_method) unless subclass.transform_method end |
#run_transform_key(input) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/bright_serializer/serializer.rb', line 100 def run_transform_key(input) if transform_method Inflector.send(@transform_method, input.to_s).to_sym else input.to_sym end end |
#set_key_transform(transform_name) ⇒ Object
rubocop:disable Naming/AccessorMethodName
92 93 94 95 96 97 98 |
# File 'lib/bright_serializer/serializer.rb', line 92 def set_key_transform(transform_name) # rubocop:disable Naming/AccessorMethodName unless SUPPORTED_TRANSFORMATION.include?(transform_name) raise ArgumentError, "Invalid transformation: #{SUPPORTED_TRANSFORMATION}" end @transform_method = transform_name end |