Module: BrightSerializer::Serializer::ClassMethods

Defined in:
lib/bright_serializer/serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributes_to_serializeObject (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_methodObject (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)
  options = args.extract_options!
  args.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

#entityObject



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_nameObject



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:, **options, &block) # rubocop:disable Naming/PredicateName
  attribute = AttributeRelation.new(
    key, serializer, options.delete(:if), options.delete(:entity), options, &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