Class: WolfCore::DomainObject
- Inherits:
-
Object
- Object
- WolfCore::DomainObject
- Includes:
- ActiveModel::Callbacks, ActiveModel::Dirty, ActiveModel::Model
- Defined in:
- lib/wolf_core/domain/domain_object.rb
Direct Known Subclasses
Class Method Summary collapse
- .cast(object) ⇒ Object
- .cast_all(objects) ⇒ Object
- .create(params = {}, **kwargs) ⇒ Object
- .create_all(objects) ⇒ Object
- .define_attributes(*attributes) ⇒ Object
- .extract_success_key ⇒ Object
- .parse_params(params = {}, **kwargs) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(attrs) ⇒ Object
-
#initialize(attributes = {}) ⇒ DomainObject
constructor
A new instance of DomainObject.
- #parse_attributes ⇒ Object
- #reset_changes ⇒ Object
- #valid?(*args) ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ DomainObject
Returns a new instance of DomainObject.
92 93 94 95 96 |
# File 'lib/wolf_core/domain/domain_object.rb', line 92 def initialize(attributes = {}) run_callbacks :initialize do super end end |
Class Method Details
.cast(object) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/wolf_core/domain/domain_object.rb', line 45 def self.cast(object) if object.is_a?(self) object else result = self.create(object) success_key = extract_success_key result.success? ? result.data.send(success_key) : object end end |
.cast_all(objects) ⇒ Object
41 42 43 |
# File 'lib/wolf_core/domain/domain_object.rb', line 41 def self.cast_all(objects) objects.map { |object| cast(object) } end |
.create(params = {}, **kwargs) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/wolf_core/domain/domain_object.rb', line 69 def self.create(params = {}, **kwargs) object = self.new(parse_params(params, **kwargs)) object.reset_changes if object.valid? success_key = extract_success_key Result.success(data: { success_key => object }) else Result.failure(error: { message: object.errors..to_sentence }) end rescue ActiveModel::UnknownAttributeError => e Result.failure(error: { message: e., error_type: e.class.to_s }) end |
.create_all(objects) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/wolf_core/domain/domain_object.rb', line 55 def self.create_all(objects) success_key = extract_success_key final_array = [] objects.each do |object| result = create(object) if result.failure? return result else final_array << result.data[success_key.to_sym] end end Result.success(data: { success_key.pluralize.to_s => final_array }) end |
.define_attributes(*attributes) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/wolf_core/domain/domain_object.rb', line 23 def self.define_attributes(*attributes) self.fields += attributes attr_accessor(*attributes) define_attribute_methods(*attributes) attributes.each do |attribute| define_method(:"#{attribute}=") do |value| send(:"#{attribute}_will_change!") unless value == send(attribute) run_callbacks :attribute_change do instance_variable_set(:"@#{attribute}", value) end value end end end |
.extract_success_key ⇒ Object
82 83 84 |
# File 'lib/wolf_core/domain/domain_object.rb', line 82 def self.extract_success_key self.to_s.split('::').last.underscore end |
.parse_params(params = {}, **kwargs) ⇒ Object
86 87 88 89 90 |
# File 'lib/wolf_core/domain/domain_object.rb', line 86 def self.parse_params(params = {}, **kwargs) params = {} unless params.is_a?(Hash) params = params.merge(kwargs) (params.to_h).with_indifferent_access end |
Instance Method Details
#attributes ⇒ Object
105 106 107 |
# File 'lib/wolf_core/domain/domain_object.rb', line 105 def attributes instance_values.slice(*self.class.fields.map(&:to_s)).with_indifferent_access end |
#attributes=(attrs) ⇒ Object
109 110 111 112 113 |
# File 'lib/wolf_core/domain/domain_object.rb', line 109 def attributes=(attrs) run_callbacks :attribute_change do super end end |
#parse_attributes ⇒ Object
119 |
# File 'lib/wolf_core/domain/domain_object.rb', line 119 def parse_attributes; end |
#reset_changes ⇒ Object
115 116 117 |
# File 'lib/wolf_core/domain/domain_object.rb', line 115 def reset_changes self.changes_applied end |
#valid?(*args) ⇒ Boolean
98 99 100 101 102 103 |
# File 'lib/wolf_core/domain/domain_object.rb', line 98 def valid?(*args) run_callbacks(:validation) do super end errors.empty? end |