Class: JsonStructMapper::Converter
- Inherits:
-
Object
- Object
- JsonStructMapper::Converter
- Defined in:
- lib/json_struct_mapper/converter.rb
Overview
Converter class handles the conversion between JSON/Hash and Struct objects
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Converter
Create a Converter from a hash directly.
-
.from_json(json_string, key = nil) ⇒ Converter
Create a Converter from a JSON string.
-
.from_json_file_to_template(file_path, key = nil) ⇒ Converter
Create a Converter from a JSON file, initialising with nil values.
Instance Method Summary collapse
-
#initialize(file_path, key = nil) ⇒ Converter
constructor
Initialize with a file path and optional key to extract from JSON.
-
#reset_struct_values(struct) ⇒ Struct
Function to remove all values from a struct and initialize with nil.
-
#to_hash(compact: true) ⇒ Hash
Convert the struct object back to a hash.
-
#to_json(pretty: false) ⇒ String
Convert the struct object to JSON.
Constructor Details
#initialize(file_path, key = nil) ⇒ Converter
Initialize with a file path and optional key to extract from JSON
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
#object ⇒ Object (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
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
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.}" end |
.from_json_file_to_template(file_path, key = nil) ⇒ Converter
Create a Converter from a JSON file, initialising with nil values
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
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
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
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 |