Class: GOBL::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/gobl/struct.rb

Overview

Base class for any structure present in the GOBL Schema

Direct Known Subclasses

Map, Num::Amount, Object

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_data(data) ⇒ GOBL::Struct

Returns a new GOBL struct from a hash of GOBL data. The type of the returned struct is determined from the ‘$schema` attribute.

Parameters:

  • data (Hash)

    the hash of GOBL data

Returns:

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gobl/struct.rb', line 12

def self.from_data(data)
  raise ArgumentError, 'Schema not present in the given data' unless data&.key?('$schema')

  schema = GOBL::ID.new(data['$schema'])

  # This could become more sophisticated in the future. For the moment, any schema not
  # being an envelope is considered to be a document as these are the only two structures
  # that are required to specify its schema.
  if schema.name == 'envelope'
    GOBL::Envelope.new data
  else
    GOBL::Schema::Object.new data
  end
end

.from_json!(json) ⇒ GOBL::Struct

Deserializes a GOBL struct from a JSON string

Parameters:

  • json (String)

    the JSON string representing the GOBL struct

Returns:



32
33
34
# File 'lib/gobl/struct.rb', line 32

def self.from_json!(json)
  new JSON.parse(json)
end

Instance Method Details

#as_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/gobl/struct.rb', line 44

def as_json
  raise NotImplementedError, 'Subclasses are expected to overload'
end

#to_jsonString

Serializes the current GOBL struct into a JSON string

Returns:

  • (String)

    the JSON string representing the struct



39
40
41
# File 'lib/gobl/struct.rb', line 39

def to_json(...)
  as_json.to_json(...)
end