module Sequent::Core::Helpers::AttributeSupport::ClassMethods
module containing class methods to be added
Attributes
types[R]
Public Instance Methods
array(type)
click to toggle source
Allows you to define something is an array of a type Example:
attrs trainees: array(Person)
# File lib/sequent/core/helpers/attribute_support.rb, line 104 def array(type) ArrayWithType.new(type) end
attrs(args)
click to toggle source
# File lib/sequent/core/helpers/attribute_support.rb, line 54 def attrs(args) validate_attrs!(args) @types.merge!(args) associations = [] args.each do |attribute, type| attr_accessor attribute if included_modules.include?(Sequent::Core::Helpers::TypeConversionSupport) Sequent::Core::Helpers::DefaultValidators.for(type).add_validations_for(self, attribute) end if type.instance_of?(Sequent::Core::Helpers::ArrayWithType) associations << attribute elsif included_modules.include?(ActiveModel::Validations) && type.included_modules.include?(Sequent::Core::Helpers::AttributeSupport) associations << attribute end end if included_modules.include?(ActiveModel::Validations) && associations.present? validates_with Sequent::Core::Helpers::AssociationValidator, associations: associations end # Generate method that sets all defined attributes based on the attrs hash. class_eval <<EOS def update_all_attributes(attrs) super if defined?(super) ensure_known_attributes(attrs) #{@types.map do |attribute, _| "@#{attribute} = attrs[:#{attribute}]" end.join("\n ")} self end EOS class_eval <<EOS def update_all_attributes_from_json(attrs) super if defined?(super) #{@types.map do |attribute, type| "@#{attribute} = #{type}.deserialize_from_json(attrs['#{attribute}'])" end.join("\n ")} end EOS end
deserialize_from_json(args)
click to toggle source
# File lib/sequent/core/helpers/attribute_support.rb, line 108 def deserialize_from_json(args) unless args.nil? obj = allocate upcast!(args) obj.update_all_attributes_from_json(args) obj end end
initialize_types()
click to toggle source
Called when this module is included or when a class which includes this module is inherited from.
All declared attrs are merged into @types in order to prevent superfluous calculation of types in a class hierarchy.
# File lib/sequent/core/helpers/attribute_support.rb, line 50 def initialize_types @types = inherited_types end
numeric?(object)
click to toggle source
# File lib/sequent/core/helpers/attribute_support.rb, line 119 def numeric?(object) true if Float(object) rescue StandardError false end
upcast(&block)
click to toggle source
# File lib/sequent/core/helpers/attribute_support.rb, line 125 def upcast(&block) @upcasters ||= [] @upcasters.push(block) end
Private Instance Methods
inherited(subclass)
click to toggle source
Calls superclass method
# File lib/sequent/core/helpers/attribute_support.rb, line 156 def inherited(subclass) super subclass.initialize_types end
inherited_types()
click to toggle source
# File lib/sequent/core/helpers/attribute_support.rb, line 132 def inherited_types merged_types = is_a?(Class) && superclass.respond_to?(:types) ? superclass.types.dup : {} included_modules .select { |m| m.include? Sequent::Core::Helpers::AttributeSupport } .reduce(merged_types) do |memo, mod| memo.merge(mod.types) end end
upcast!(hash)
click to toggle source
# File lib/sequent/core/helpers/attribute_support.rb, line 142 def upcast!(hash) return if @upcasters.nil? @upcasters.each do |upcaster| upcaster.call(hash) end end
validate_attrs!(args)
click to toggle source
# File lib/sequent/core/helpers/attribute_support.rb, line 150 def validate_attrs!(args) duplicate_attrs = types.keys & args.keys fail ArgumentError, "Attributes already defined: #{duplicate_attrs.join(', ')}" if duplicate_attrs.any? end