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(*args, &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.
56 57 58 |
# File 'lib/bright_serializer/serializer.rb', line 56 def attributes_to_serialize @attributes_to_serialize end |
#transform_method ⇒ Object (readonly)
Returns the value of attribute transform_method.
56 57 58 |
# File 'lib/bright_serializer/serializer.rb', line 56 def transform_method @transform_method end |
Instance Method Details
#attributes(*args, &block) ⇒ Object Also known as: attribute
65 66 67 68 69 70 71 72 |
# File 'lib/bright_serializer/serializer.rb', line 65 def attributes(*args, &block) = args. args.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
103 104 105 106 107 108 109 110 111 |
# File 'lib/bright_serializer/serializer.rb', line 103 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
113 114 115 |
# File 'lib/bright_serializer/serializer.rb', line 113 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
76 77 78 79 80 81 82 |
# File 'lib/bright_serializer/serializer.rb', line 76 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
58 59 60 61 62 63 |
# File 'lib/bright_serializer/serializer.rb', line 58 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
95 96 97 98 99 100 101 |
# File 'lib/bright_serializer/serializer.rb', line 95 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
87 88 89 90 91 92 93 |
# File 'lib/bright_serializer/serializer.rb', line 87 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 |