Class: JsonStructMapper::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/json_struct_mapper/converter.rb

Overview

Converter class handles the conversion between JSON/Hash and Struct objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, key = nil) ⇒ Converter

Initialize with a file path and optional key to extract from JSON

Parameters:

  • file_path (String)

    Path to the JSON file

  • key (String, nil) (defaults to: nil)

    Optional key to extract from the JSON root

Raises:



17
18
19
# File 'lib/json_struct_mapper/converter.rb', line 17

def initialize(file_path, key = nil)
  @object = load_from_file(file_path, key)
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/json_struct_mapper/converter.rb', line 8

def object
  @object
end

Class Method Details

.from_hash(hash) ⇒ Converter

Create a Converter from a hash directly

Parameters:

  • hash (Hash)

    Hash to convert to struct

Returns:



25
26
27
28
29
# File 'lib/json_struct_mapper/converter.rb', line 25

def self.from_hash(hash)
  instance = allocate
  instance.instance_variable_set(:@object, instance.send(:hash_to_struct, hash))
  instance
end

.from_json(json_string, key = nil) ⇒ Converter

Create a Converter from a JSON string

Parameters:

  • json_string (String)

    JSON string to parse and convert

  • key (String, nil) (defaults to: nil)

    Optional key to extract from the JSON root

Returns:



36
37
38
39
40
41
42
43
44
# File 'lib/json_struct_mapper/converter.rb', line 36

def self.from_json(json_string, key = nil)
  data_hash = JSON.parse(json_string)
  hash = key ? data_hash[key] : data_hash
  raise InvalidKeyError, "Key '#{key}' not found in JSON" if key && hash.nil?

  from_hash(hash)
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse JSON: #{e.message}"
end

.from_json_file_to_template(file_path, key = nil) ⇒ Converter

Create a Converter from a JSON file, initialising with nil values

Parameters:

  • file_path (String)

    Path to the JSON file

Returns:

Raises:



51
52
53
54
55
56
# File 'lib/json_struct_mapper/converter.rb', line 51

def self.from_json_file_to_template(file_path, key = nil)
  instance = new(file_path, key) # Create an instance of the class
  struct = instance.send(:load_from_file, file_path, key) # Call the instance method
  instance.instance_variable_set(:@object, instance.send(:reset_struct_values, struct))
  instance
end

Instance Method Details

#reset_struct_values(struct) ⇒ Struct

Function to remove all values from a struct and initialize with nil

Parameters:

  • struct (Struct)

    to convert

Returns:

  • (Struct)

    struct with nil values



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/json_struct_mapper/converter.rb', line 62

def reset_struct_values(struct)
  struct.members.each do |member|
    value = struct[member]
    if value.is_a?(Struct)
      reset_struct_values(value) # Recursively reset nested structs
    elsif value.is_a?(Array)
      if value.any? { |item| item.is_a?(Struct) }
        value.each { |item| reset_struct_values(item) if item.is_a?(Struct) } # Reset structs inside arrays
      else
        struct[member] = nil # Set the array to nil if it doesn't contain structs
      end
    else
      struct[member] = nil
    end
  end
  struct
end

#to_hash(compact: true) ⇒ Hash

Convert the struct object back to a hash

Parameters:

  • compact (Boolean) (defaults to: true)

    whether to remove nil values

Returns:

  • (Hash)

    converted hash



84
85
86
87
# File 'lib/json_struct_mapper/converter.rb', line 84

def to_hash(compact: true)
  hash = struct_to_hash(@object)
  compact ? compact_hash(hash) : hash
end

#to_json(pretty: false) ⇒ String

Convert the struct object to JSON

Parameters:

  • pretty (Boolean) (defaults to: false)

    whether to format with indentation

Returns:

  • (String)

    JSON string



93
94
95
96
# File 'lib/json_struct_mapper/converter.rb', line 93

def to_json(pretty: false)
  hash = to_hash
  pretty ? JSON.pretty_generate(hash) : JSON.generate(hash)
end