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

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object

extend host class with class methods when we’re included



107
108
109
# File 'lib/sequent/core/helpers/attribute_support.rb', line 107

def self.included(host_class)
  host_class.extend(ClassMethods)
end

Instance Method Details

#attributesObject

needed for active module JSON serialization



113
114
115
# File 'lib/sequent/core/helpers/attribute_support.rb', line 113

def attributes
  self.class.types
end

#validation_errors(prefix = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/sequent/core/helpers/attribute_support.rb', line 117

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