Module: Mutils::Serialization::Results::Main

Included in:
SerializationResults
Defined in:
lib/mutils/serialization/results/main.rb

Overview

Module Attributes

Instance Method Summary collapse

Instance Method Details

#check_if_included(s_options, key) ⇒ Object



31
32
33
34
35
# File 'lib/mutils/serialization/results/main.rb', line 31

def check_if_included(s_options, key)
  return s_options[:if].call(scope, options[:params] || {}) unless s_options[:if].nil? || scope_is_collection?

  s_options[:always_include] || options[:includes]&.include?(key)
end

#class_nameObject



41
42
43
44
45
46
47
# File 'lib/mutils/serialization/results/main.rb', line 41

def class_name
  if scope_is_collection?
    Lib::Helper.instance.pluralize(format_class_name(scope[0]))
  else
    format_class_name(scope)
  end
end

#format_class_name(object) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/mutils/serialization/results/main.rb', line 49

def format_class_name(object)
  if self.class.serializer_name&.length&.positive?
    self.class.serializer_name
  else
    object.class.to_s.downcase
  end
end

#generate_hashObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mutils/serialization/results/main.rb', line 10

def generate_hash
  if scope
    if scope_is_collection?
      options[:child] = true
      scope.map { |inner_scope| self.class.new(inner_scope, options).generate_hash }
    else
      hashed_result(Lib::ResultHash.new)
    end
  else
    {}
  end
end

#hashed_result(result_hash) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/mutils/serialization/results/main.rb', line 23

def hashed_result(result_hash)
  fetch_block_attributes(self.class.attributes_to_serialize_blocks, result_hash)
  fetch_attributes(self.class.attributes_to_serialize, result_hash)
  hash_relationships(self.class.relationships, result_hash)

  result_hash.hash
end

#scope_is_collection?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/mutils/serialization/results/main.rb', line 37

def scope_is_collection?
  Lib::Helper.instance.collection? scope
end