lib/mutils/serialization/serialization_methods.rb in mutils-0.2.35 vs lib/mutils/serialization/serialization_methods.rb in mutils-0.2.36

- old
+ new

@@ -3,11 +3,13 @@ # Module Mutils module Mutils module Serialization # Module SerializationCore module SerializationMethods - extend ActiveSupport::Concern + def self.included(base) + base.extend ClassMethods + end # Module ClassMethods module ClassMethods def name_tag(name_tag, root = nil) self.serializer_name = name_tag self.include_root = root @@ -28,26 +30,28 @@ attributes_to_serialize[attr] = value end end def attribute(method_name, options = {}) + raise "if: should be a Proc object for attribute #{method_name}" if options[:if] && (options[:if].class.to_s != 'Proc') + add_single_attribute(method_name, options, 'attribute') end def custom_method(method_name, options = {}) add_single_attribute(method_name, options, 'method') end def add_single_attribute(method_name, options, type) self.attributes_to_serialize = {} if attributes_to_serialize.nil? always_include = options[:always_include].nil? ? false : options[:always_include] - value = { method: type == 'method', always_include: always_include } + value = { method: type == 'method', always_include: always_include, if: options[:if] } attributes_to_serialize[method_name] = value end - def relationship(relationship_name, options = {}, option_name = 'belongs_to') - options = prepare_options(relationship_name, options, option_name) + def relationship(relationship_name, options = {}) + options = prepare_options(relationship_name, options, __callee__) self.relationships = {} if relationships.nil? relationships[relationship_name] = options end alias belongs_to relationship @@ -60,21 +64,21 @@ raise "Serializer is Required for belongs_to :#{relationship_name}." \ "\nDefine it like:\n#{option_name} :#{relationship_name}, " \ 'serializer: SERIALIZER_CLASS' end raise "Serializer class not defined for relationship: #{relationship_name}" unless class_exists? class_name + raise "if: should be a Proc object for attribute #{relationship_name}" if options[:if] && (options[:if].class.to_s != 'Proc') - options[:serializer] = class_name.to_s.constantize + options[:serializer] = Object.const_get class_name.to_s options[:option_name] = option_name options[:label] = options[:label] options end def class_exists?(class_name) - klass = class_name.to_s.constantize rescue nil + klass = Object.const_get class_name.to_s rescue nil klass && defined?(klass) && klass.is_a?(Class) end - end end end end