Class: Fortnox::Struct

Inherits:
Dry::Struct
  • Object
show all
Includes:
RailsSerialisation::StructMethods, Fortnox::Serialisation::StructJSON
Defined in:
lib/fortnox/struct.rb

Class Method Summary collapse

Methods included from RailsSerialisation::StructMethods

#as_json

Methods included from Fortnox::Serialisation::StructJSON

#to_json

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

Returns:

  • (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_namesObject

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_attributesObject



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