Module: SmoothOperator::Serialization::HelperMethods

Extended by:
HelperMethods
Included in:
HelperMethods
Defined in:
lib/smooth_operator/serialization.rb

Instance Method Summary collapse

Instance Method Details

#attribute_names(object, options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/smooth_operator/serialization.rb', line 45

def attribute_names(object, options)
  attribute_names = object.internal_data.keys.sort

  if only = options[:only]
    white_list = [*only].map(&:to_s)

    attribute_names &= white_list
  elsif except = options[:except]
    black_list = [*except].map(&:to_s)

    attribute_names -= black_list
  end

  attribute_names
end

#attribute_to_hash(object, options = nil) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/smooth_operator/serialization.rb', line 112

def attribute_to_hash(object, options = nil)
  if object.respond_to?(:serializable_hash)
    Helpers.symbolyze_keys(object.serializable_hash(options))
  else
    object
  end
end

#method_names(object, options) ⇒ Object



61
62
63
# File 'lib/smooth_operator/serialization.rb', line 61

def method_names(object, options)
  [*options[:methods]].select { |n| object.respond_to?(n) }
end

#serialize_has_many_attribute(parent_object, reflection, attribute_name, options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/smooth_operator/serialization.rb', line 84

def serialize_has_many_attribute(parent_object, reflection, attribute_name, options)
  return nil unless reflection.has_many?

  object = parent_object.read_attribute_for_serialization(attribute_name)

  object.reduce({}) do |hash, array_entry|
    id = Helpers.present?(array_entry.id) ? array_entry.id : Helpers.generated_id

    hash[id.to_s] = attribute_to_hash(array_entry, options)

    hash
  end
end

#serialize_normal_attribute(parent_object, attribute_name, options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/smooth_operator/serialization.rb', line 98

def serialize_normal_attribute(parent_object, attribute_name, options)
  if parent_object.respond_to?(:read_attribute_for_serialization)
    object = parent_object.read_attribute_for_serialization(attribute_name)
  else
    object = parent_object
  end

  if object.is_a?(Array)
    object.map { |array_entry| attribute_to_hash(array_entry, options) }
  else
    attribute_to_hash(object, options)
  end
end

#serialize_normal_or_rails_way(object, attribute_name, options) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/smooth_operator/serialization.rb', line 65

def serialize_normal_or_rails_way(object, attribute_name, options)
  _attribute_name, attribute_sym = attribute_name, attribute_name.to_sym

  reflection = object.class.respond_to?(:reflect_on_association) &&
    object.class.reflect_on_association(attribute_sym)

  attribute_options = options[attribute_sym]

  if reflection && reflection.rails_serialization?
    attribute_value = serialize_has_many_attribute(object, reflection, attribute_name, attribute_options)

    _attribute_name = "#{attribute_name}_attributes"
  end

  attribute_value ||= serialize_normal_attribute(object, attribute_name, attribute_options)

  [_attribute_name, attribute_value]
end