Module: Forem::Util

Defined in:
lib/forem/util.rb

Overview

Utility helpers used internally by the forem-ruby library.

These methods are not part of the public API surface for end users but are documented here for contributors and library maintainers.

Class Method Summary collapse

Class Method Details

.convert_to_forem_object(value) ⇒ ForemObject, ...

Recursively convert a raw Ruby value into the appropriate Forem type.

The conversion rules are:

This method is called during attribute assignment in ForemObject so that nested API response hashes automatically become ForemObject instances with dynamic attribute access.

Examples:

Converting a hash

Forem::Util.convert_to_forem_object({ "id" => 1, "name" => "Alice" })
#=> #<Forem::ForemObject ...>

Converting an array of hashes

Forem::Util.convert_to_forem_object([{ "id" => 1 }, { "id" => 2 }])
#=> [#<Forem::ForemObject ...>, #<Forem::ForemObject ...>]

Passing through a scalar value

Forem::Util.convert_to_forem_object("hello")  #=> "hello"
Forem::Util.convert_to_forem_object(42)       #=> 42

Parameters:

  • value (Object)

    the raw value to convert.

Returns:

  • (ForemObject, Array, Object)

    the converted value.



32
33
34
35
36
37
38
39
40
41
# File 'lib/forem/util.rb', line 32

def self.convert_to_forem_object(value)
  case value
  when Hash
    ForemObject.construct_from(value)
  when Array
    value.map { |v| convert_to_forem_object(v) }
  else
    value
  end
end