Module: DataClass::InstanceMethods

Defined in:
lib/data_class/instance_methods.rb

Overview

An internal module for providing instance methods for ‘Data.define`.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean



97
98
99
# File 'lib/data_class/instance_methods.rb', line 97

def ==(other)
  hash_for_comparation == other.hash_for_comparation
end

#deconstructObject



23
24
25
# File 'lib/data_class/instance_methods.rb', line 23

def deconstruct
  @data.values
end

#deconstruct_keys(array_of_names_or_nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/data_class/instance_methods.rb', line 28

def deconstruct_keys(array_of_names_or_nil)
  return to_h if array_of_names_or_nil.nil?

  unless array_of_names_or_nil.is_a?(Enumerable)
    raise TypeError, "wrong argument type #{array_of_names_or_nil.class} (expected Array or nil)"
  end

  array_of_names_or_nil.each_with_object({}) do |key, h|
    h[key] = @data[key] if @data[key]
  end
end

#eql?(other) ⇒ Boolean



41
42
43
# File 'lib/data_class/instance_methods.rb', line 41

def eql?(other)
  hash_for_comparation.eql?(other.hash_for_comparation)
end

#hashInteger



46
47
48
# File 'lib/data_class/instance_methods.rb', line 46

def hash
  hash_for_comparation.hash
end

#initialize(**kwargs) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/data_class/instance_methods.rb', line 6

def initialize(**kwargs)
  definition = DataClass::Definition.new(self.class.members)
  definition.validate(kwargs)

  @data = kwargs.each_with_object({}) do |entry, h|
    key, value = entry
    h[key] = value
  end.freeze
  freeze
end

#initialize_copy(other) ⇒ Object



17
18
19
20
21
# File 'lib/data_class/instance_methods.rb', line 17

def initialize_copy(other)
  @data = other.instance_variable_get(:@data).dup
  @data.freeze
  freeze
end

#inspectString



51
52
53
54
55
56
57
# File 'lib/data_class/instance_methods.rb', line 51

def inspect
  if self.class.name
    "#<data #{self.class.name} #{inspect_members}>"
  else
    "#<data #{inspect_members}>"
  end
end

#marshal_dumpHash



60
61
62
# File 'lib/data_class/instance_methods.rb', line 60

def marshal_dump
  @data
end

#marshal_load(dump) ⇒ Object

Raises:

  • (TypeError)


65
66
67
68
69
# File 'lib/data_class/instance_methods.rb', line 65

def marshal_load(dump)
  raise TypeError, 'dump must be a Hash' unless dump.is_a?(Hash)

  initialize(**dump)
end

#membersObject



71
72
73
# File 'lib/data_class/instance_methods.rb', line 71

def members
  self.class.members
end

#to_h(&block) ⇒ Object



75
76
77
78
79
80
# File 'lib/data_class/instance_methods.rb', line 75

def to_h(&block)
  @data.each_with_object({}) do |key_and_value, h|
    key, value = block ? block.call(*key_and_value) : key_and_value
    h[key] = value
  end.to_h
end

#to_sObject



82
83
84
# File 'lib/data_class/instance_methods.rb', line 82

def to_s
  inspect
end

#with(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
# File 'lib/data_class/instance_methods.rb', line 86

def with(**kwargs)
  return self if kwargs.empty?

  unknown_keywords = kwargs.keys - @data.keys
  raise ArgumentError, "unknown keywords: #{unknown_keywords.join(', ')}" unless unknown_keywords.empty?

  new_data = @data.merge(kwargs)
  self.class.new(**new_data)
end