Module: Sequent::Core::Helpers::AttributeSupport
- Included in:
- BaseCommand, Event, ValueObject
- Defined in:
- lib/sequent/core/helpers/attribute_support.rb
Overview
Provides functionality for defining attributes with their types
Since our Commands and ValueObjects are not backed by a database like e.g. rails we can not infer their types. We need the types to be able to parse from and to json. We could have stored te type information in the json, but we didn’t.
You typically do not need to include this module in your classes. If you extend from Sequent::Core::ValueObject, Sequent::Core::Event or Sequent::Core::BaseCommand you will get this functionality for free.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(host_class) ⇒ Object
extend host class with class methods when we’re included.
Instance Method Summary collapse
-
#attributes ⇒ Object
needed for active module JSON serialization.
-
#valid_date ⇒ Object
If you have a Date object validate it with this method when the unparsed input is a String This scenario is typically when a date is posted from the web.
- #validation_errors(prefix = nil) ⇒ Object
Class Method Details
.included(host_class) ⇒ Object
extend host class with class methods when we’re included
130 131 132 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 130 def self.included(host_class) host_class.extend(ClassMethods) end |
Instance Method Details
#attributes ⇒ Object
needed for active module JSON serialization
136 137 138 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 136 def attributes self.class.types end |
#valid_date ⇒ Object
If you have a Date object validate it with this method when the unparsed input is a String This scenario is typically when a date is posted from the web.
Example
class Person < Sequent::Core::ValueObject
attrs date_of_birth: Date
validate :valid_date
end
If the date_of_birth is a valid date it will be parsed into a proper Date object. This implementation currently only support dd-mm-yyyy format.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 164 def valid_date self.class.types.each do |name, clazz| if clazz == Date return if self.instance_variable_get("@#{name}").kind_of? Date unless self.instance_variable_get("@#{name}").blank? if (/\d{2}-\d{2}-\d{4}/ =~ self.instance_variable_get("@#{name}")).nil? @errors.add(name.to_s, :invalid_date) if (/\d{2}-\d{2}-\d{4}/ =~ self.instance_variable_get("@#{name}")).nil? else begin self.instance_variable_set "@#{name}", Date.strptime(self.instance_variable_get("@#{name}"), "%d-%m-%Y") rescue @errors.add(name.to_s, :invalid_date) end end end end end end |
#validation_errors(prefix = nil) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/sequent/core/helpers/attribute_support.rb', line 140 def validation_errors(prefix = nil) result = errors.to_hash self.class.types.each do |field| value = self.instance_variable_get("@#{field[0]}") if value.respond_to? :validation_errors value.validation_errors.each { |k, v| result["#{field[0].to_s}_#{k.to_s}".to_sym] = v } end end prefix ? HashWithIndifferentAccess[result.map { |k, v| ["#{prefix}_#{k}", v] }] : result end |