Class: Optify::FromHashable

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

Overview

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.key_to_typeObject (readonly)

: Hash[Symbol, T::Types::Base]



20
21
22
# File 'lib/optify_from_hash/from_hashable.rb', line 20

def key_to_type
  @key_to_type
end

Class Method Details

.from_hash(hash) ⇒ Object

Create a new instance of the class from a hash.

Parameters:

  • hash

    The hash to create the instance from.

Returns:

  • The new instance.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/optify_from_hash/from_hashable.rb', line 56

def self.from_hash(hash)
  instance = new

  hash.each do |key, value|
    value_type = _get_value_type(key)
    value = _convert_value(value, value_type)
    instance.instance_variable_set("@#{key}", value)
  end

  instance.freeze
end

.inherited(subclass) ⇒ Object

: [T < Optify::FromHashable] (Class) -> void



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/optify_from_hash/from_hashable.rb', line 24

def self.inherited(subclass)
  super

  # Trace the execution after the subclass finishes loading to capture its methods.
  TracePoint.trace(:end) do |tp|
    if tp.self == subclass
      subclass.instance_variable_set(:@key_to_type, _build_key_to_type(subclass))
      tp.disable
    end
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Compare this object with another object for equality.

Parameters:

  • other

    The object to compare.

Returns:

  • (Boolean)

    true if the objects are equal; otherwise, false.



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

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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
# File 'lib/optify_from_hash/from_hashable.rb', line 180

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

#hashObject

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

Returns:

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



191
192
193
# File 'lib/optify_from_hash/from_hashable.rb', line 191

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

#to_hObject

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.

Returns:

  • The hash representation of this object.



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/optify_from_hash/from_hashable.rb', line 207

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) ⇒ String

Convert this object to a JSON string.

Parameters:

  • state (JSON::State, nil) (defaults to: nil)

Returns:

  • (String)


197
198
199
# File 'lib/optify_from_hash/from_hashable.rb', line 197

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