module BrightSerializer::Serializer::ClassMethods

Attributes

attributes_to_serialize[R]
transform_method[R]

Public Instance Methods

attribute(*attributes, **options, &block)
Alias for: attributes
attributes(*attributes, **options, &block) click to toggle source
# File lib/bright_serializer/serializer.rb, line 61
def attributes(*attributes, **options, &block)
  attributes.each do |key|
    attribute = Attribute.new(key, options[:if], options[:entity], &block)
    attribute.transformed_key = run_transform_key(key)
    @attributes_to_serialize << attribute
  end
end
Also aliased as: attribute
entity() click to toggle source
# File lib/bright_serializer/serializer.rb, line 87
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() click to toggle source
# File lib/bright_serializer/serializer.rb, line 97
def entity_name
  name.split('::').last.downcase
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/bright_serializer/serializer.rb, line 54
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) click to toggle source
# File lib/bright_serializer/serializer.rb, line 79
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) click to toggle source
# File lib/bright_serializer/serializer.rb, line 71
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