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

Instance Method Details

#==(other) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 124

def ==(other)
  return false unless other.is_a?(self.class)

  each_field.all? do |name, value|
    other.get_value(name) == value
  end
end

#attrsObject



50
51
52
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 50

def attrs
  @_attrs
end

#changed_fieldsObject



82
83
84
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 82

def changed_fields
  @changed_fields.to_a
end

#each_field(&proc) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 94

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 get_meta(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
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 44

def get_value(name)
  return @_attrs[name] unless @_attrs[name].nil?
  get_nil_or_raise(name)
end

#has_value?(name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 77

def has_value?(name)
  @_attrs.key?(name)
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



143
144
145
146
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 143

def initialize_clone(_other)
  super
  @changed_fields = @changed_fields.dup if defined?(@changed_fields)
end

#initialize_copy(_other) ⇒ Object



132
133
134
135
136
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 132

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



138
139
140
141
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 138

def initialize_dup(_other)
  super
  @changed_fields = nil
end

#inspectObject



119
120
121
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 119

def inspect
  to_s
end

#reset_changesObject



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



86
87
88
89
90
91
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 86

def set_as_changed(name)
  if has_value?(name)
    @changed_fields ||= Set.new
    @changed_fields << name
  end
end

#set_attr(name, val) ⇒ Object



60
61
62
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 60

def set_attr(name, val)
  @_attrs[name] = val
end

#set_value(name, val) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 65

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_hObject



55
56
57
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 55

def to_h
  each_field.to_h
end

#to_sObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/dto/dto_instance_methods.rb', line 103

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