Module: Sequent::Core::Helpers::AttributeSupport::ClassMethods
- Defined in:
- lib/sequent/core/helpers/attribute_support.rb
Overview
module containing class methods to be added
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#array(type) ⇒ Object
Allows you to define something is an array of a type Example:.
- #attrs(args) ⇒ Object
- #deserialize_from_json(args) ⇒ Object
-
#initialize_types ⇒ Object
Called when this module is included or when a class which includes this module is inherited from.
- #numeric?(object) ⇒ Boolean
- #upcast(&block) ⇒ Object
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
44 45 46 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 44 def types @types end |
Instance Method Details
#array(type) ⇒ Object
Allows you to define something is an array of a type Example:
attrs trainees: array(Person)
104 105 106 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 104 def array(type) ArrayWithType.new(type) end |
#attrs(args) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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 is_array = type.instance_of?(Sequent::Core::Helpers::ArrayWithType) needs_validation = !is_array && included_modules.include?(ActiveModel::Validations) && type.included_modules.include?(Sequent::Core::Helpers::AttributeSupport) associations << attribute if is_array || needs_validation 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) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# 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 ⇒ Object
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.
50 51 52 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 50 def initialize_types @types = inherited_types end |
#numeric?(object) ⇒ Boolean
119 120 121 122 123 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 119 def numeric?(object) true if Float(object) rescue StandardError false end |
#upcast(&block) ⇒ Object
125 126 127 128 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 125 def upcast(&block) @upcasters ||= [] @upcasters.push(block) end |