Module: ReeDto::DtoInstanceMethods
- Includes:
- Ree::Contracts::ArgContracts, Ree::Contracts::Core
- Defined in:
- lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb
Constant Summary collapse
- FieldNotSetError =
Class.new(ArgumentError)
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attrs ⇒ Object
- #changed_fields ⇒ Object
- #each_field(&proc) ⇒ Object
- #get_meta(name) ⇒ Object
- #get_value(name) ⇒ Object
- #has_value?(name) ⇒ Boolean
- #initialize(attrs = nil, **kwargs) ⇒ Object
- #initialize_clone(_other) ⇒ Object
- #initialize_copy(_other) ⇒ Object
- #initialize_dup(_other) ⇒ Object
- #inspect ⇒ Object
- #reset_changes ⇒ Object
- #set_as_changed(name) ⇒ Object
- #set_attr(name, val) ⇒ Object
- #set_value(name, val) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#==(other) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 131 def ==(other) return false unless other.is_a?(self.class) each_field.all? do |name, value| other.get_value(name) == value end end |
#attrs ⇒ Object
57 58 59 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 57 def attrs @_attrs end |
#changed_fields ⇒ Object
89 90 91 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 89 def changed_fields @changed_fields.to_a end |
#each_field(&proc) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 101 def each_field(&proc) return enum_for(:each_field) unless block_given? self.class.fields.select { has_value?(_1.name) }.each do |field| proc.call(field.name, get_value(field.name)) end end |
#get_meta(name) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 36 def (name) self .class .fields .find { _1.name == name} || (raise ArgumentError.new("field :#{name} not defined for :#{self.class}")) end |
#get_value(name) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 44 def get_value(name) @_attrs.fetch(name) do = (name) if !.has_default? raise FieldNotSetError.new("field `#{name}` not set for: #{self}") else @_attrs[name] = .default end end end |
#has_value?(name) ⇒ Boolean
84 85 86 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 84 def has_value?(name) @_attrs.key?(name) || (name).has_default? end |
#initialize(attrs = nil, **kwargs) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 14 def initialize(attrs = nil, **kwargs) @_attrs = attrs || kwargs list = self.class.fields.map(&:name) extra = attrs.keys - list if !extra.empty? puts("WARNING: #{self.class}.new does not have definition for #{extra.inspect} fields") end end |
#initialize_clone(_other) ⇒ Object
150 151 152 153 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 150 def initialize_clone(_other) super @changed_fields = @changed_fields.dup if defined?(@changed_fields) end |
#initialize_copy(_other) ⇒ Object
139 140 141 142 143 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 139 def initialize_copy(_other) deep_dupper = ReeObject::DeepDup.new @_attrs = deep_dupper.call(@_attrs) @collections = deep_dupper.call(@collections) if defined?(@collections) end |
#initialize_dup(_other) ⇒ Object
145 146 147 148 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 145 def initialize_dup(_other) super @changed_fields = nil end |
#inspect ⇒ Object
126 127 128 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 126 def inspect to_s end |
#reset_changes ⇒ Object
31 32 33 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 31 def reset_changes @changed_fields = nil end |
#set_as_changed(name) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 93 def set_as_changed(name) if has_value?(name) @changed_fields ||= Set.new @changed_fields << name end end |
#set_attr(name, val) ⇒ Object
67 68 69 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 67 def set_attr(name, val) @_attrs[name] = val end |
#set_value(name, val) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 72 def set_value(name, val) if has_value?(name) old = get_value(name) return old if old == val end @changed_fields ||= Set.new @changed_fields << name @_attrs[name] = val end |
#to_h ⇒ Object
62 63 64 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 62 def to_h each_field.to_h end |
#to_s ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 110 def to_s result = "#<dto #{self.class} " data = each_field.map do |name, value| "#{name}=#{inspect_value(value)}" end data += self.class.collections.select { send(_1.name).size > 0 }.map do |col| "#{col.name}=#{send(col.name).inspect}" end result << data.join(", ") result << ">" end |