Class: GOBL::Struct
- Inherits:
-
Object
- Object
- GOBL::Struct
- Defined in:
- lib/gobl/struct.rb
Overview
Base class for any structure present in the GOBL Schema
Direct Known Subclasses
Class Method Summary collapse
-
.from_data(data) ⇒ GOBL::Struct
Returns a new GOBL struct from a hash of GOBL data.
-
.from_json!(json) ⇒ GOBL::Struct
Deserializes a GOBL struct from a JSON string.
Instance Method Summary collapse
- #as_json ⇒ Object private
-
#to_json ⇒ String
Serializes the current GOBL struct into a JSON string.
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.
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
32 33 34 |
# File 'lib/gobl/struct.rb', line 32 def self.from_json!(json) new JSON.parse(json) end |
Instance Method Details
#as_json ⇒ Object
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.
44 45 46 |
# File 'lib/gobl/struct.rb', line 44 def as_json raise NotImplementedError, 'Subclasses are expected to overload' end |
#to_json ⇒ String
Serializes the current GOBL struct into a JSON string
39 40 41 |
# File 'lib/gobl/struct.rb', line 39 def to_json(...) as_json.to_json(...) end |