Module: SmoothOperator::Serialization::HelperMethods
- Extended by:
- HelperMethods
- Included in:
- HelperMethods
- Defined in:
- lib/smooth_operator/serialization.rb
Instance Method Summary collapse
- #attribute_names(object, options) ⇒ Object
- #attribute_to_hash(object, options = nil) ⇒ Object
- #method_names(object, options) ⇒ Object
- #serialize_has_many_attribute(parent_object, reflection, attribute_name, options) ⇒ Object
- #serialize_normal_attribute(parent_object, attribute_name, options) ⇒ Object
- #serialize_normal_or_rails_way(object, attribute_name, options) ⇒ Object
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, ) attribute_names = object.internal_data.keys.sort if only = [:only] white_list = [*only].map(&:to_s) attribute_names &= white_list elsif except = [: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, = nil) if object.respond_to?(:serializable_hash) Helpers.symbolyze_keys(object.serializable_hash()) else object end end |
#method_names(object, options) ⇒ Object
61 62 63 |
# File 'lib/smooth_operator/serialization.rb', line 61 def method_names(object, ) [*[: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, ) 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, ) 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, ) 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, ) } else attribute_to_hash(object, ) 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, ) _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_sym] if reflection && reflection.rails_serialization? attribute_value = serialize_has_many_attribute(object, reflection, attribute_name, ) _attribute_name = "#{attribute_name}_attributes" end attribute_value ||= serialize_normal_attribute(object, attribute_name, ) [_attribute_name, attribute_value] end |