lib/mutils/serialization/serialization_methods.rb in mutils-0.2.25 vs lib/mutils/serialization/serialization_methods.rb in mutils-0.2.27

- old
+ new

@@ -4,53 +4,57 @@ module Mutils module Serialization # Module SerializationCore module SerializationMethods extend ActiveSupport::Concern + # Module ClassMethods module ClassMethods + def name_tag(name_tag, root = nil) + self.serializer_name = name_tag + self.include_root = root + end + def attributes(*attributes_list) self.attributes_to_serialize = {} if attributes_to_serialize.nil? attributes_list&.each { |attr| attributes_to_serialize[attr] = attr } end def custom_methods(*method_list) self.method_to_serialize = {} if method_to_serialize.nil? method_list&.each { |attr| method_to_serialize[attr] = attr } end - def belongs_to(relationship_name, options = {}) - if options[:serializer].nil? - raise "Serializer is Required for belongs_to :#{relationship_name}." \ - "\nDefine it like:\nbelongs_to :#{relationship_name}, " \ - 'serializer: SERIALIZER_CLASS' - end - unless class_exists? options[:serializer] - raise 'Serializer class not defined' - end - + def belongs_to(relationship_name, options = {}, option_name = 'belongs_to') + options = prepare_options(relationship_name, options, option_name) self.belongs_to_relationships = {} if belongs_to_relationships.nil? belongs_to_relationships[relationship_name] = options end alias has_one belongs_to - def has_many(relationship_name, options = {}) - if options[:serializer].nil? - raise "Serializer is Required for has_many :#{relationship_name}." \ - "\nDefine it like:\nbelongs_to :#{relationship_name}, " \ + def has_many(relationship_name, options = {}, option_name = 'has_many') + options = prepare_options(relationship_name, options, option_name) + self.has_many_relationships = {} if has_many_relationships.nil? + has_many_relationships[relationship_name] = options + end + + def prepare_options(relationship_name, options, option_name) + class_name = options[:serializer] + if class_name.nil? + raise "Serializer is Required for belongs_to :#{relationship_name}." \ + "\nDefine it like:\n#{option_name} :#{relationship_name}, " \ 'serializer: SERIALIZER_CLASS' end - unless class_exists? options[:serializer] - raise 'Serializer class not defined' - end + raise "Serializer class not defined for relationship: #{relationship_name}" unless class_exists? class_name - self.has_many_relationships = {} if has_many_relationships.nil? - has_many_relationships[relationship_name] = options + options[:serializer] = class_name.to_s.constantize + options end def class_exists?(class_name) - eval("defined?(#{class_name}) && #{class_name}.is_a?(Class)") == true + klass = class_name.to_s.constantize rescue nil + klass && defined?(klass) && klass.is_a?(Class) end end end end -end \ No newline at end of file +end