Class: Fortnox::Struct
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- Fortnox::Struct
- Defined in:
- lib/fortnox/struct.rb
Direct Known Subclasses
Fortnox::Structs::DefaultDeliveryTypes, Fortnox::Structs::DefaultTemplates, Fortnox::Structs::DocumentRow, Fortnox::Structs::EDIInformation, Fortnox::Structs::EmailInformation
Class Method Summary collapse
- .attribute(name, type, *flags) ⇒ Object
- .attribute?(name, type, *flags) ⇒ Boolean
-
.attribute_names ⇒ Object
Attribute names this struct accepts.
-
.new(attributes = NO_ATTRIBUTES, safe = false) ⇒ Object
Dry::Struct signals a bad value with Dry::Struct::Error, a TypeError that sits outside the Fortnox::Error hierarchy — so a caller's
rescue Fortnox::AttributeErrormisses it and the failure surfaces as an unhandled crash. - .read_only_attributes ⇒ Object
Methods included from RailsSerialisation::StructMethods
Methods included from Fortnox::Serialisation::StructJSON
Class Method Details
.attribute(name, type, *flags) ⇒ Object
31 32 33 34 |
# File 'lib/fortnox/struct.rb', line 31 def attribute(name, type, *flags) read_only_attributes << name if flags.include?(:read_only) super(name, type) end |
.attribute?(name, type, *flags) ⇒ Boolean
36 37 38 39 |
# File 'lib/fortnox/struct.rb', line 36 def attribute?(name, type, *flags) read_only_attributes << name if flags.include?(:read_only) super(name, type) end |
.attribute_names ⇒ Object
Attribute names this struct accepts.
61 62 63 |
# File 'lib/fortnox/struct.rb', line 61 def attribute_names schema.keys.map(&:name) end |
.new(attributes = NO_ATTRIBUTES, safe = false) ⇒ Object
Dry::Struct signals a bad value with Dry::Struct::Error, a TypeError
that sits outside the Fortnox::Error hierarchy — so a caller's
rescue Fortnox::AttributeError misses it and the failure surfaces as
an unhandled crash. Structs are reachable through the same public
entry points as resources (Resource.stub coerces nested hashes into
structs), so they translate their errors the same way.
rubocop:disable Style/OptionalBooleanParameter
safe is Dry::Struct's own second positional parameter — it picks the
non-raising coercion path — so it has to stay positional here. We only
pass it through.
51 52 53 54 55 56 57 |
# File 'lib/fortnox/struct.rb', line 51 def new(attributes = NO_ATTRIBUTES, safe = false, &) return super() if NO_ATTRIBUTES.equal?(attributes) super rescue Dry::Struct::Error => e raise translated_error(e, attributes) end |
.read_only_attributes ⇒ Object
26 27 28 29 |
# File 'lib/fortnox/struct.rb', line 26 def read_only_attributes @read_only_attributes ||= (superclass.respond_to?(:read_only_attributes) ? superclass.read_only_attributes.dup : []) end |