Class: Optify::FromHashable

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/optify_from_hash/from_hashable.rb

Overview

A base class for classes that can be created from a hash.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(hash) ⇒ Object

Create a new immutable instance of the class from a hash.

: (Hash[untyped, untyped]) -> instance

Parameters:

  • hash

    The hash to create the instance from.

Returns:

  • The new instance.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/optify_from_hash/from_hashable.rb', line 21

def self.from_hash(hash)
  instance = new

  hash.each do |key, value|
    begin
      method = instance_method(key)
    rescue StandardError
      raise ArgumentError,
            "Error converting hash to `#{name}` because of key \"#{key}\". Perhaps \"#{key}\" is not a valid attribute for `#{name}`."
    end

    sig = T::Utils.signature_for_method(method)
    raise "A Sorbet signature is required for `#{name}.#{key}`." if sig.nil?

    sig_return_type = sig.return_type
    value = _convert_value(value, sig_return_type)
    instance.instance_variable_set("@#{key}", value)
  end

  instance.freeze
end

Instance Method Details

#==(other) ⇒ Boolean

Compare this object with another object for equality. : (untyped) -> bool

Parameters:

  • other

    The object to compare.

Returns:

  • (Boolean)

    true if the objects are equal; otherwise, false.



128
129
130
131
132
133
134
135
# File 'lib/optify_from_hash/from_hashable.rb', line 128

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

  instance_variables.all? do |name|
    instance_variable_get(name) == other.instance_variable_get(name)
  end
end

#eql?(other) ⇒ Boolean

Support equality by value so that instances can be used in Sets and as Hash keys. : (untyped) -> bool

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
# File 'lib/optify_from_hash/from_hashable.rb', line 139

def eql?(other)
  return true if other.equal?(self)
  return false if self.class != other.class

  instance_variables.all? do |name|
    instance_variable_get(name).eql?(other.instance_variable_get(name))
  end
end

#hashInteger

: () -> Integer

Returns:

  • (Integer)

    a hash value based on the object’s class and instance variables.



150
151
152
# File 'lib/optify_from_hash/from_hashable.rb', line 150

def hash
  [self.class, *instance_variables.sort.map { |name| instance_variable_get(name) }].hash
end

#to_hHash

Convert this object to a Hash recursively. This is mostly the reverse operation of ‘from_hash`, as keys will be symbols and `from_hash` will convert strings to symbols if that’s how the attribute is declared. : () -> Hash[Symbol, untyped]

Returns:

  • (Hash)

    The hash representation of this object.



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/optify_from_hash/from_hashable.rb', line 166

def to_h
  result = Hash.new(instance_variables.size)

  instance_variables.each do |var_name|
    # Remove the @ prefix to get the method name
    method_name = var_name.to_s[1..] #: as !nil
    value = instance_variable_get(var_name)
    result[method_name.to_sym] = self.class.send(:_convert_value_for_to_h, value)
  end

  result
end

#to_json(state = nil) ⇒ Object

Convert this object to a JSON string. : (?JSON::State?) -> String



156
157
158
# File 'lib/optify_from_hash/from_hashable.rb', line 156

def to_json(state = nil)
  to_h.to_json(state)
end