Module: Mutils::Serialization::Methods::Attributes

Defined in:
lib/mutils/serialization/methods/attributes.rb

Overview

Module Attributes

Instance Method Summary collapse

Instance Method Details

#add_single_attribute(method_name, options, is_method) ⇒ Object



43
44
45
46
47
48
# File 'lib/mutils/serialization/methods/attributes.rb', line 43

def add_single_attribute(method_name, options, is_method)
  self.attributes_to_serialize = {} if attributes_to_serialize.nil?
  always_include = options[:always_include].nil? ? false : options[:always_include]
  value = { method: is_method, always_include: always_include, if: options[:if] }
  attributes_to_serialize[method_name] = value
end

#attribute(method_name, options = {}, &proc) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mutils/serialization/methods/attributes.rb', line 27

def attribute(method_name, options = {}, &proc)
  raise "if: should be a Proc object for attribute #{method_name}" if options[:if] && !options[:if].instance_of?(Proc)

  if proc.instance_of? Proc
    self.attributes_to_serialize_blocks = {} if attributes_to_serialize_blocks.nil?
    options[:block] = proc
    attributes_to_serialize_blocks[method_name] = options
  else
    add_single_attribute(method_name, options, false)
  end
end

#attributes(*attributes_list) ⇒ Object



11
12
13
# File 'lib/mutils/serialization/methods/attributes.rb', line 11

def attributes(*attributes_list)
  parse_attributes_methods(attributes_list, false)
end

#custom_method(method_name, options = {}) ⇒ Object



39
40
41
# File 'lib/mutils/serialization/methods/attributes.rb', line 39

def custom_method(method_name, options = {})
  add_single_attribute(method_name, options, true)
end

#custom_methods(*attributes_list) ⇒ Object



15
16
17
# File 'lib/mutils/serialization/methods/attributes.rb', line 15

def custom_methods(*attributes_list)
  parse_attributes_methods(attributes_list, true)
end

#parse_attributes_methods(list, is_method) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/mutils/serialization/methods/attributes.rb', line 19

def parse_attributes_methods(list, is_method)
  self.attributes_to_serialize = {} if attributes_to_serialize.nil?
  list&.each do |attr|
    value = { method: is_method, always_include: true }
    attributes_to_serialize[attr] = value
  end
end